-
Notifications
You must be signed in to change notification settings - Fork 0
/
stipple.js
76 lines (68 loc) · 2.74 KB
/
stipple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
// Copied from https://github.com/pshihn/stipple
import cliProgress from 'cli-progress';
import { Voronoi } from './voronoi.js';
import seedrandom from 'seedrandom';
const rnd = seedrandom('This is the seed');
// Adapted from https://observablehq.com/@mbostock/voronoi-stippling
export function stipple(imageDataBuffer, width, height, pointCount, iterations, invert_colors, callback) {
const n = pointCount || (width * height / 50);
iterations = iterations || [80];
const highestIteration = Math.max(...iterations);
const points = new Float32Array(n * 2);
const rgba = new Uint8ClampedArray(imageDataBuffer);
const c = new Float64Array(n * 2);
const s = new Float64Array(n);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < 60; ++j) {
const x = points[i * 2] = Math.floor(rnd() * width);
const y = points[i * 2 + 1] = Math.floor(rnd() * height);
if (rnd() < grayValue(y * width + x, rgba, invert_colors))
break;
}
}
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
bar1.start(highestIteration, 0);
const voronoi = new Voronoi(points, [0, 0, width, height]);
for (let k = 0; k <= highestIteration; ++k) {
c.fill(0);
s.fill(0);
for (let y = 0, i = 0; y < height; ++y) {
for (let x = 0; x < width; ++x) {
const w = grayValue(y * width + x, rgba, invert_colors);
i = voronoi.find(x + 0.5, y + 0.5, i);
s[i] += w;
c[i * 2] += w * (x + 0.5);
c[i * 2 + 1] += w * (y + 0.5);
}
}
// Relax the diagram by moving points to the weighted centroid.
// Wiggle the points a little bit so they don’t get stuck.
const w = Math.pow(k + 1, -0.8) * 10;
for (let i = 0; i < n; ++i) {
const x0 = points[i * 2], y0 = points[i * 2 + 1];
const x1 = s[i] ? c[i * 2] / s[i] : x0, y1 = s[i] ? c[i * 2 + 1] / s[i] : y0;
points[i * 2] = x0 + (x1 - x0) * 1.8 + (rnd() - 0.5) * w;
points[i * 2 + 1] = y0 + (y1 - y0) * 1.8 + (rnd() - 0.5) * w;
}
voronoi.update();
if (callback && (iterations.indexOf(k) >= 0)) {
callback({ points, width, height, iteration: k });
}
bar1.update(k);
}
bar1.stop();
return { points, width, height, iteration: highestIteration };
}
function grayValue(i, rgba, invert_colors) {
const offset = i * 4;
let gray = 1 - (0.299 * rgba[offset] + 0.587 * rgba[offset + 1] + 0.114 * rgba[offset + 2]) / 254;
if (invert_colors) {
gray = 1 - gray;
}
// check alpha
if (rgba[offset + 3] == 0) {
return 0;
}
return gray;
}