-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniformly-random-circles.js
58 lines (51 loc) · 1.57 KB
/
uniformly-random-circles.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
const canvasSketch = require('canvas-sketch');
const { lerp } = require('canvas-sketch-util/math');
const random = require('canvas-sketch-util/random');
const palettes = require('nice-color-palettes');
const settings = {
dimensions: [2048, 2048],
};
const sketch = () => {
const colorCount = random.rangeFloor(2, 6);
// const palette = ['purple', 'violet', 'pink', 'lavender'];
const palette = random.shuffle(random.pick(palettes)).slice(0, colorCount);
console.log(palette);
const createGrid = () => {
const points = [];
const count = 25;
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
const u = count < 1 ? 0.5 : i / (count - 1);
const v = count < 1 ? 0.5 : j / (count - 1);
const radius = Math.abs(random.noise2D(u, v)) * 0.025;
points.push({
position: [u, v],
radius,
color: random.pick(palette),
});
}
}
return points;
};
const points = createGrid().filter(() => random.value() > 0.5);
const margin = 400;
return ({ context, width, height }) => {
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
points.forEach((data) => {
const {
position,
radius,
color
} = data;
const [u, v] = position;
const x = lerp(margin, width - margin, u);
const y = lerp(margin, height - margin, v);
context.beginPath();
context.arc(x, y, radius * width, 0, Math.PI * 2, false);
context.fillStyle = color;
context.fill();
});
};
};
canvasSketch(sketch, settings);