forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas-generative-silhouette.js
76 lines (65 loc) · 2.18 KB
/
canvas-generative-silhouette.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
/**
* A Canvas2D example of generative/algorithmic artwork, sized for print.
* @author Matt DesLauriers (@mattdesl)
*/
const canvasSketch = require('canvas-sketch');
const Random = require('canvas-sketch-util/random');
const { lerp } = require('canvas-sketch-util/math');
// We can force a random seed or a specific string/number
Random.setSeed(Random.getRandomSeed());
const settings = {
pixelsPerInch: 300,
// When exporting, use the seed as the suffix
// This way we can reproduce it more easily later
suffix: Random.getSeed(),
// Standard A4 paper size
dimensions: 'A4',
// We'll work in inches for the rendering
units: 'in'
};
const sketch = ({ width, height }) => {
const margin = 0;
const sliceCount = 50000;
const slices = Array.from(new Array(sliceCount)).map((_, i, list) => {
const t = list.lenth <= 1 ? 0 : i / (list.length - 1);
const noiseAngle = t * Math.PI * 2;
const nx = Math.cos(noiseAngle);
const ny = Math.sin(noiseAngle);
const nf = 0.05 + Random.range(0, 0.5);
const amplitude = 2;
const noise = Random.noise2D(nx * nf, ny * nf);
const noise01 = noise * 0.5 + 0.5;
const tOffset = Random.gaussian(0, 0.01);
const cx = width / 2;
const x = cx + noise * amplitude;
return {
alpha: Random.range(0.75, 1) * (1 - noise01),
color: 'white',
lineWidth: Random.range(0.005, 0.02) * 0.1,
length: Random.gaussian() * noise01 * 0.5,
angle: Random.gaussian(0, 1),
x,
y: lerp(margin, height - margin, t + tOffset)
};
});
return ({ context }) => {
context.globalCompositeOperation = 'source-over';
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
context.globalCompositeOperation = 'lighter';
slices.forEach(slice => {
context.save();
context.beginPath();
context.translate(slice.x, slice.y);
context.rotate(slice.angle);
context.lineTo(slice.length / 2, 0);
context.lineTo(-slice.length / 2, 0);
context.lineWidth = slice.lineWidth;
context.strokeStyle = slice.color;
context.globalAlpha = slice.alpha;
context.stroke();
context.restore();
});
};
};
canvasSketch(sketch, settings);