-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathindex.js
116 lines (98 loc) · 2.46 KB
/
index.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
var max = 19,
xScale = 6,
zScale = 2.5,
yScale = 16,
startFrom = 0,
dz = 640,
// I actually want it to be slower then 60fps
requestAnimationFrame = function(callback) {
window.setTimeout(callback, 1000 / 24);
};
function run() {
var ctx = document.getElementById('scene').getContext('2d'),
redSpiralShadow = createSpiral({
foreground: "#660000",
background: "#330000",
isLeft: true,
yLocalScale: 1.01
}),
redSpiral = createSpiral({
foreground: "#ff0000",
background: "#440000",
isLeft: true,
yLocalScale: 1
}),
cyanSpiralShadow = createSpiral({
foreground: "#003300",
background: "#000000",
isLeft: false,
yLocalScale: 1.01
}),
cyanSpiral = createSpiral({
foreground: "#00ffcc",
background: "#005633",
isLeft: false,
yLocalScale: 1
});
animationLoop();
function animationLoop() {
renderFrame();
if (startFrom > 1) {
startFrom = 0;
} else {
startFrom += 0.1;
}
requestAnimationFrame(animationLoop);
}
function renderFrame() {
ctx.clearRect(0, 0, 480, 640);
ctx.beginPath();
xScale *= 0.93;
forEachStep(redSpiralShadow);
forEachStep(cyanSpiralShadow);
xScale /= 0.93;
forEachStep(redSpiral);
forEachStep(cyanSpiral);
}
function forEachStep(callback) {
for (var i = -startFrom; i < max + startFrom; i += 0.08) {
if (i < 0 || i > max) continue;
callback(i);
}
}
function createSpiral(config) {
var sign = config.isLeft ? -1 : 1,
background = config.background,
foreground = config.foreground,
yLocalScale = config.yLocalScale || 1;
if (!config.isLeft) {
background = foreground;
foreground = config.background;
}
return function(i) {
var zoff = i * Math.sin(i),
z = dz / (dz - sign * zoff * zScale),
x = getX(i, z, sign),
y = getY(i * yLocalScale, z);
if (zoff + sign * Math.PI / 4 < 0) {
switchColor(foreground);
} else {
switchColor(background);
}
ctx.moveTo(x, y);
ctx.lineTo(getX(i + 0.03, z, sign), getY((i + 0.01) * yLocalScale, z));
};
}
function switchColor(color) {
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = color;
ctx.beginPath();
}
function getX(i, z, sign) {
return sign * i * Math.cos(i) * z * xScale + 255;
}
function getY(i, z) {
return i * z * yScale + 50;
}
}