-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
82 lines (58 loc) · 2.09 KB
/
sketch.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
77
78
79
80
81
82
const canvas = document.getElementById('canvas');
const canvasSketch = require('canvas-sketch');
const math = require('canvas-sketch-util/math');
const random = require('canvas-sketch-util/random');
const color = require('canvas-sketch-util/color');
const settings = {
dimensions: [1080, 1080]
};
/* const degToRad = (degrees) => {
return degrees / 180 * Math.PI;
}
const randomRange = (min, max) => {
return Math.random() * (max - min) + min;
} */
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r},${g},${b})`;
}
const sketch = () => {
return ({ context, width, height }) => {
context.fillStyle = getRandomColor();
context.fillRect(0, 0, width, height);
context.fillStyle = getRandomColor();
const cx = width * 0.5;
const cy = height * 0.5;
const w = width * 0.01;
const h = height * 0.1;
let x, y;
const num = 40;
for (let i = 0; i < num; i++) {
const slice = math.degToRad(360 / num);
const angle = slice * i;
const radius = width * 0.3;
x = cx + radius * Math.sin(angle);
y = cy + radius * Math.cos(angle);
context.save();
context.translate(x, y);
context.rotate(-angle);
context.scale(random.range(0.1, 2), random.range(0.5, 0.5));
context.beginPath();
context.rect(-w * 0.5, random.range(0, -h * 0.5), w, h);
context.fill();
context.restore();
context.save();
context.translate(cx, cy);
context.rotate(-angle);
context.lineWidth = random.range(5, 20)
context.strokeStyle = getRandomColor()
context.beginPath();
context.arc(0, 0, radius * random.range(0.7, 1.3), slice * random.range(0, -8), slice * random.range(0, 5));
context.stroke();
context.restore();
}
};
}
canvasSketch(sketch, settings);