Add existing to tracked

This commit is contained in:
Jay
2026-08-11 09:53:42 -04:00
parent afe07f3055
commit ffd6e3d73c
8531 changed files with 4396230 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"rules": {
"strict": 2,
"indent": 0,
"linebreak-style": 0,
"quotes": 0,
"semi": 0,
"no-cond-assign": 1,
"no-constant-condition": 1,
"no-duplicate-case": 1,
"no-empty": 1,
"no-ex-assign": 1,
"no-extra-boolean-cast": 1,
"no-extra-semi": 1,
"no-fallthrough": 1,
"no-func-assign": 1,
"no-global-assign": 1,
"no-implicit-globals": 2,
"no-inner-declarations": ["error", "functions"],
"no-irregular-whitespace": 2,
"no-loop-func": 1,
"no-magic-numbers": ["warn", { "ignore": [1, 0, -1], "ignoreArrayIndexes": true}],
"no-multi-str": 1,
"no-mixed-spaces-and-tabs": 1,
"no-proto": 1,
"no-sequences": 1,
"no-throw-literal": 1,
"no-unmodified-loop-condition": 1,
"no-useless-call": 1,
"no-void": 1,
"no-with": 2,
"wrap-iife": 1,
"no-redeclare": 1,
"no-unused-vars": ["error", { "vars": "all", "args": "none" }],
"no-sparse-arrays": 1
}
}
+2
View File
@@ -0,0 +1,2 @@
test.js linguist-documentation
index.html linguist-generated
+134
View File
@@ -0,0 +1,134 @@
'use strict'
module.exports = calcSDF
var INF = 1e20
function calcSDF(src, options) {
if (!options) options = {}
var cutoff = options.cutoff == null ? 0.25 : options.cutoff
var radius = options.radius == null ? 8 : options.radius
var channel = options.channel || 0
var w, h, size, data, intData, stride, ctx, canvas, imgData, i, l
// handle image container
if (ArrayBuffer.isView(src) || Array.isArray(src)) {
if (!options.width || !options.height) throw Error('For raw data width and height should be provided by options')
w = options.width, h = options.height
data = src
if (!options.stride) stride = Math.floor(src.length / w / h)
else stride = options.stride
}
else {
if (window.HTMLCanvasElement && src instanceof window.HTMLCanvasElement) {
canvas = src
ctx = canvas.getContext('2d')
w = canvas.width, h = canvas.height
imgData = ctx.getImageData(0, 0, w, h)
data = imgData.data
stride = 4
}
else if (window.CanvasRenderingContext2D && src instanceof window.CanvasRenderingContext2D) {
canvas = src.canvas
ctx = src
w = canvas.width, h = canvas.height
imgData = ctx.getImageData(0, 0, w, h)
data = imgData.data
stride = 4
}
else if (window.ImageData && src instanceof window.ImageData) {
imgData = src
w = src.width, h = src.height
data = imgData.data
stride = 4
}
}
size = Math.max(w, h)
//convert int data to floats
if ((window.Uint8ClampedArray && data instanceof window.Uint8ClampedArray) || (window.Uint8Array && data instanceof window.Uint8Array)) {
intData = data
data = Array(w*h)
for (i = 0, l = Math.floor(intData.length / stride); i < l; i++) {
data[i] = intData[i*stride + channel] / 255
}
}
else {
if (stride !== 1) throw Error('Raw data can have only 1 value per pixel')
}
// temporary arrays for the distance transform
var gridOuter = Array(w * h)
var gridInner = Array(w * h)
var f = Array(size)
var d = Array(size)
var z = Array(size + 1)
var v = Array(size)
for (i = 0, l = w * h; i < l; i++) {
var a = data[i]
gridOuter[i] = a === 1 ? 0 : a === 0 ? INF : Math.pow(Math.max(0, 0.5 - a), 2)
gridInner[i] = a === 1 ? INF : a === 0 ? 0 : Math.pow(Math.max(0, a - 0.5), 2)
}
edt(gridOuter, w, h, f, d, v, z)
edt(gridInner, w, h, f, d, v, z)
var dist = window.Float32Array ? new Float32Array(w * h) : new Array(w * h)
for (i = 0, l = w*h; i < l; i++) {
dist[i] = Math.min(Math.max(1 - ( (gridOuter[i] - gridInner[i]) / radius + cutoff), 0), 1)
}
return dist
}
// 2D Euclidean distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/dt/
function edt(data, width, height, f, d, v, z) {
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
f[y] = data[y * width + x]
}
edt1d(f, d, v, z, height)
for (y = 0; y < height; y++) {
data[y * width + x] = d[y]
}
}
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
f[x] = data[y * width + x]
}
edt1d(f, d, v, z, width)
for (x = 0; x < width; x++) {
data[y * width + x] = Math.sqrt(d[x])
}
}
}
// 1D squared distance transform
function edt1d(f, d, v, z, n) {
v[0] = 0;
z[0] = -INF
z[1] = +INF
for (var q = 1, k = 0; q < n; q++) {
var s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k])
while (s <= z[k]) {
k--
s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k])
}
k++
v[k] = q
z[k] = s
z[k + 1] = +INF
}
for (q = 0, k = 0; q < n; q++) {
while (z[k + 1] < q) k++
d[q] = (q - v[k]) * (q - v[k]) + f[v[k]]
}
}
+32
View File
@@ -0,0 +1,32 @@
{
"name": "bitmap-sdf",
"version": "1.0.4",
"description": "Calculate SDF for image/bw-data/array",
"main": "index.js",
"devDependencies": {
"bubleify": "^0.7.0",
"enable-mobile": "^1.0.7"
},
"scripts": {
"test": "budo test",
"build": "browserify test.js -g bubleify | indexhtmlify | metadataify | github-cornerify > index.html"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dfcreative/bitmap-sdf.git"
},
"keywords": [
"sdf",
"signed-distance",
"image-sdf",
"bitmap",
"bwdist",
"tiny-sdf"
],
"author": "Dima Yv <dfcreative@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/dfcreative/bitmap-sdf/issues"
},
"homepage": "https://github.com/dfcreative/bitmap-sdf#readme"
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

+79
View File
@@ -0,0 +1,79 @@
# bitmap-sdf [![unstable](https://img.shields.io/badge/stability-unstable-green.svg)](http://github.com/badges/stability-badges)
Calculate signed distance field for an image / bw-data. Fork of [tiny-sdf](https://github.com/mapbox/tiny-sdf) with reduced API.
![bitmap-sdf](preview.png)
[Demo](https://dy.github.io/bitmap-sdf/)
## Usage
[![npm install bitmap-sdf](https://nodei.co/npm/bitmap-sdf.png?mini=true)](https://npmjs.org/package/bitmap-sdf/)
```js
let calcSdf = requrie('bitmap-sdf')
//draw image
let canvas = document.body.appendChild(document.createElement('canvas'))
let w = canvas.width = 200, h = canvas.height = 200
let ctx = canvas.getContext('2d')
ctx.fillStyle = 'white'
ctx.font = 'bold 30px sans-serif'
ctx.fillText('X', 20, 20)
//calculate distances
let distances = calcSdf(canvas)
//show distances
let imgArr = new Uint8ClampedArray(w*h*4)
for (let i = 0; i < w; i++) {
for (let j = 0; j < h; j++) {
imgArr[j*w*4 + i*4 + 0] = arr[j*w+i]*255
imgArr[j*w*4 + i*4 + 1] = arr[j*w+i]*255
imgArr[j*w*4 + i*4 + 2] = arr[j*w+i]*255
imgArr[j*w*4 + i*4 + 3] = 255
}
}
var data = new ImageData(imgArr, w, h)
ctx.putImageData(data, 0, 0)
```
### dist = calcSdf(source, options?)
Calculate distance field for the input `source` data, based on `options`. Returns 1-channel array with distance values from `0..1` range.
#### Source:
Type | Meaning
---|---
_Canvas_, _Context2D_ | Calculates sdf for the full canvas image data based on `options.channel`, by default `0`, ie. red channel.
_ImageData_ | Calculates sdf for the image data based on `options.channel`
_Uint8ClampedArray_, _Uint8Array_ | Handles raw pixel data, requires `options.width` and `options.height`. Stride is detected from `width` and `height`.
_Float32Array_, _Array_ | Handles raw numbers from `0..1` range, requires `options.width` and `options.height`. Stride is detected from `width` and `height`.
#### Options:
Property | Default | Meaning
---|---|---
`cutoff` | `0.25` | Cutoff parameter, balance between SDF inside `1` and outside `0` of glyph
`radius` | `10` | Max length of SDF, ie. the size of SDF around the `cutoff`
`width` | `canvas.width` | Width of input data, if array
`height` | `canvas.height` | Height of input data, if array
`channel` | `0` | Channel number, `0` is red, `1` is green, `2` is blue, `3` is alpha.
`stride` | `null` | Explicitly indicate number of channels per pixel. Not needed if `height` and `width` are provided.
## See also
* [font-atlas-sdf](https://github.com/hughsk/font-atlas-sdf) generate sdf atlas for a font.
* [tiny-sdf](https://github.com/mapbox/tiny-sdf) fast glyph signed distance field generation.
* [optical-properties](https://github.com/dfcreative/optical-properties) glyph optical center and bounding box calculation
## Alternatives
* [disttransform.wat](https://github.com/LingDong-/wasm-fun/blob/master/wat/disttransform.wat)
## License
(c) 2017 Dima Yv. MIT License
Development supported by plot.ly.
+115
View File
@@ -0,0 +1,115 @@
'use strict'
require('enable-mobile')
document.body.style.fontFamily = 'sans-serif'
document.body.style.padding = '2rem'
var calcSDF = require('./')
var canvas = document.body.appendChild(document.createElement('canvas'))
canvas.style.margin = '1rem 1rem 1rem 0'
canvas.width = 165
canvas.height = 150
var ctx = canvas.getContext('2d')
ctx.fillStyle = 'black'
ctx.fillRect(0,0,canvas.width, canvas.height)
ctx.fillStyle = 'white'
ctx.font = 'bold 100px sans-serif'
ctx.fillText('X', 50, 100)
var out = document.body.appendChild(document.createElement('canvas'))
out.style.margin = '1rem 1rem 1rem 0'
out.width = 165
out.height = 150
var outCtx = out.getContext('2d')
outCtx.drawImage(canvas, 0, 0);
var cutoff = 0, radius = 10
update()
function update () {
var idata = ctx.getImageData(0,0,canvas.width, canvas.height).data
var data = Array(canvas.width*canvas.height)
for (var i = 0; i < data.length; i++) {
data[i] = idata[i*4]/255
}
console.time('sdf')
var arr = calcSDF(data, {
cutoff: cutoff,
radius: radius,
width: canvas.width,
height: canvas.height
})
console.timeEnd('sdf')
let imgArr
if (global.Uint8ClampedArray) {
imgArr = new Uint8ClampedArray(165*150*4)
} else {
imgArr = Array(165*150*4)
}
for (let i = 0; i < 165; i++) {
for (let j = 0; j < 150; j++) {
imgArr[j*165*4 + i*4 + 0] = arr[j*165+i]*255
imgArr[j*165*4 + i*4 + 1] = arr[j*165+i]*255
imgArr[j*165*4 + i*4 + 2] = arr[j*165+i]*255
imgArr[j*165*4 + i*4 + 3] = 255
}
}
// IE way
var c = document.createElement('canvas');
var data = c.getContext('2d').createImageData(165, 150);
if (data.data.set) {
data.data.set(imgArr);
}
else {
for (var i = 0; i < imgArr.length; i++) {
data.data[i] = imgArr[i]
}
}
// var data = new ImageData(imgArr, 165, 150)
outCtx.putImageData(data, 0, 0)
}
var cutoffTitle = document.body.appendChild(document.createElement('label'))
cutoffTitle.innerHTML = 'Cutoff'
cutoffTitle.style.display = 'block'
var cutoffEl = document.body.appendChild(document.createElement('input'))
cutoffEl.type = 'range'
cutoffEl.min = 0
cutoffEl.max = 1
cutoffEl.step = 0.001
cutoffEl.value = cutoff
cutoffEl.oninput = e => {
cutoff = parseFloat(cutoffEl.value)
update()
}
var radTitle = document.body.appendChild(document.createElement('label'))
radTitle.innerHTML = 'Radius'
radTitle.style.display = 'block'
var radEl = document.body.appendChild(document.createElement('input'))
radEl.type = 'range'
radEl.min = 0
radEl.max = 100
radEl.step = 0.2
radEl.value = radius
radEl.oninput = e => {
radius = parseFloat(radEl.value)
update()
}