-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandala.js
133 lines (110 loc) · 2.94 KB
/
mandala.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const canvas = document.getElementById('root');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// canvas.style.backgroundColor = 'rgba(5, 12, 0, 1)';
const centerX = canvas.width / 2;
const centery = canvas.height / 2;
const NUM = 300;
const RADIUS = 100;
const settings = {
hsl: false,
fill: true,
radius: 200,
period: 15,
amp: 22,
numberOfCircles: 7,
speed: 100,
color: [24, 128, 255],
};
const gui = new dat.GUI();
gui.add(settings, 'fill');
gui.add(settings, 'hsl');
gui.add(settings, 'amp', 0, 40).step(1);
gui.add(settings, 'period', 0, 40).step(1);
gui.add(settings, 'radius', 100, 400).step(1);
gui.add(settings, 'numberOfCircles', 1, 25).step(1);
gui.add(settings, 'speed', 0.5, 400);
gui.addColor(settings, 'color');
function drawCircle(radius, color, offset) {
ctx.fillStyle = color;
ctx.beginPath();
for (let i = 0; i <= NUM; i++) {
const teta = (i * 2 * Math.PI) / NUM;
const varRadius = radius + settings.amp * Math.sin(teta * settings.period + offset);
const x = centerX + varRadius * Math.cos(teta);
const y = centery + varRadius * Math.sin(teta);
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
ctx.strokeStyle = '#ffffff';
if (settings.fill) {
ctx.fill();
} else {
ctx.stroke();
}
}
let time = 0;
function draw() {
time++;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < settings.numberOfCircles; i++) {
let color = i % 2 ? 'black' : 'white';
if (settings.hsl) {
// const [r, g, b] = settings.color;
// const { h, l, s } = R_G_BtoHSL(r, g, b);
// color = `hsl(${h * i},${s}%, ${l}%)`;
color = 'hsl(' + i * settings.color[0] + ', 70%, 66%)';
}
drawCircle(
settings.radius - i * 10,
color,
settings.speed*time/(1000+50*i)
);
}
}
function render() {
draw();
requestAnimationFrame(render);
}
render();
function R_G_BtoHSL(red, green, blue) {
red = red < 0 ? 0 : red > 255 ? 255 : red;
green = green < 0 ? 0 : green > 255 ? 255 : green;
blue = blue < 0 ? 0 : blue > 255 ? 255 : blue;
var r = red / 255,
g = green / 255,
b = blue / 255,
min = Math.min(r, g, b),
max = Math.max(r, g, b),
delta = max - min,
h,
s,
l;
if (max == min) {
h = 0;
} else if (r == max) {
h = (g - b) / delta;
} else if (g == max) {
h = 2 + (b - r) / delta;
} else if (b == max) {
h = 4 + (r - g) / delta;
}
h = Math.min(h * 60, 360);
if (h < 0) h += 360;
l = (min + max) / 2;
if (max == min) s = 0;
else if (l <= 0.5) s = delta / (max + min);
else s = delta / (2 - max - min);
return {
h: Math.round(h),
s: Math.round(s * 100),
l: Math.round(l * 100),
};
}