-
Notifications
You must be signed in to change notification settings - Fork 246
/
index.js
147 lines (127 loc) · 4.08 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var thetaMin = 0;
var thetaMax = 6 * Math.PI;
var period = 5;
var lineSpacing = 1 / 30;
var lineLength = lineSpacing / 2;
var yScreenOffset = 300;
var xScreenOffset = 260;
var xScreenScale = 360;
var yScreenScale = 360;
var yCamera = 2;
var zCamera = -3;
var rate = 1 / (2 * Math.PI); // every rotation y gets one bigger
var factor = rate / 3;
function run() {
var ctx = document.getElementById('scene').getContext('2d'),
spirals = [
new Spiral({
foreground: "#220000", // Second shadow for red spiral
angleOffset: Math.PI * 0.92,
factor: 0.90 * factor
}),
new Spiral({
foreground: "#002211", // Second shadow for cyan spiral
angleOffset: -Math.PI * 0.08,
factor: 0.90 * factor
}),
new Spiral({
foreground: "#660000", // red spiral shadow
angleOffset: Math.PI * 0.95,
factor: 0.93 * factor
}),
new Spiral({
foreground: "#003322", // cyan spiral shadow
angleOffset: -Math.PI * 0.05,
factor: 0.93 * factor
}),
new Spiral({
foreground: "#ff0000", // red Spiral
angleOffset: Math.PI,
factor: factor
}),
new Spiral({
foreground: "#00ffcc", // cyan spiral
angleOffset: 0,
factor: factor
})];
renderFrame(); // animation loop starts here
function renderFrame() {
requestAnimationFrame(renderFrame);
ctx.clearRect(0, 0, 500, 500);
ctx.beginPath();
spirals.forEach(renderSpiral);
}
function renderSpiral(spiral) {
spiral.render(ctx);
}
function Spiral(config) {
var offset = 0;
var lineSegments = computeLineSegments();
this.render = function(ctx) {
offset -= 1;
if (offset <= -period) {
offset += period;
}
lineSegments[offset].forEach(drawLineSegment);
};
function drawLineSegment(segment) {
stroke(config.foreground, segment.start.alpha);
ctx.moveTo(segment.start.x, segment.start.y);
ctx.lineTo(segment.end.x, segment.end.y);
}
function computeLineSegments() {
var lineSegments = {};
var factor = config.factor;
var thetaNew, thetaOld;
for (var offset = 0; offset > -period; offset--) {
lineSegments[offset] = lines = [];
for (
var theta = thetaMin + getThetaChangeRate(thetaMin, offset * lineSpacing / period, rate, factor);
theta < thetaMax;
theta += getThetaChangeRate(theta, lineSpacing, rate, factor)
) {
thetaOld = (theta >= thetaMin) ? theta : thetaMin;
thetaNew = theta + getThetaChangeRate(theta, lineLength, rate, factor);
if (thetaNew <= thetaMin) {
continue;
}
lines.push({
start: getPointByAngle(thetaOld, factor, config.angleOffset, rate),
end: getPointByAngle(thetaNew, factor, config.angleOffset, rate)
});
}
}
return lineSegments;
}
}
function stroke(color, alpha) {
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = color;
ctx.globalAlpha = alpha;
ctx.beginPath();
}
function getPointByAngle(theta, factor, angleOffset, rate) {
var x = theta * factor * Math.cos(theta + angleOffset);
var z = - theta * factor * Math.sin(theta + angleOffset);
var y = rate * theta;
// now that we have 3d coordinates, project them into 2d space:
var point = projectTo2d(x, y, z);
// calculate point's color alpha level:
point.alpha = Math.atan((y * factor / rate * 0.1 + 0.02 - z) * 40) * 0.35 + 0.65;
return point;
}
function getThetaChangeRate(theta, lineLength, rate, factor) {
return lineLength / Math.sqrt(rate * rate + factor * factor * theta * theta);
}
function projectTo2d(x, y, z) {
return {
x: xScreenOffset + xScreenScale * (x / (z - zCamera)),
y: yScreenOffset + yScreenScale * ((y - yCamera) / (z - zCamera))
};
}
// I actually want it to be slower then 60fps
function requestAnimationFrame(callback) {
window.setTimeout(callback, 1000 / 24);
}
}