-
Notifications
You must be signed in to change notification settings - Fork 219
/
epicycles-controller.js
165 lines (143 loc) · 5.19 KB
/
epicycles-controller.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import CanvasController from './canvas-controller.js';
import { getFourierData, resample2dData } from '../just-fourier-things.js';
import { slurp, clampedSlurp } from '../util.js';
import { palette } from '../color.js';
export default class EpicyclesController extends CanvasController {
constructor(id, width, height) {
super(id, width, height);
this.animate = true;
// [ {freq, amplitude, phase } ]
this.fourierData = [];
// [ {x, y} ]
this.fourierPath = [];
this.numPoints = 0;
// What percentage of the path to draw
this.pathAmt = 1;
this.animatePathAmt = true;
this.animAmt = 0;
this.niceAnimAmt = 0;
this.period = 5;
this.fourierAmt = 1;
this.pathDirty = false;
}
setPath(path, numPoints=-1, minAmplitude=0.01) {
if (numPoints < 0) {
numPoints = path.length;
}
this.numPoints = numPoints;
this.animAmt = 0;
this.niceAnimAmt = 0;
this.fourierPath = [];
// Get the fourier data, also filter out the really small terms.
this.fourierData = getFourierData(resample2dData(path, this.numPoints)).filter(f => f.amplitude > minAmplitude);
this.fourierData.sort((a, b) => b.amplitude - a.amplitude);
console.log(this.fourierData.length + '/' + numPoints)
}
setFourierAmt(amt) {
this.fourierAmt = amt;
this.pathDirty = true;
}
recalculatePath() {
// then render everything.
for (let i = 0; i < this.numPoints; i ++) {
this.niceAnimAmt += 1 / this.numPoints;
this.addToPath();
}
this.niceAnimAmt -= 1;
}
update(dt, mousePosition) {
if (this.pathDirty) {
this.recalculatePath();
this.pathDirty = false;
}
if (!this.animate) {
return;
}
this.animAmt += (dt / this.period) % 1;
while (this.animAmt > 1) {
this.animAmt --;
this.niceAnimAmt --;
}
if (this.animatePathAmt) {
const transitionFactor = (1 / 10);
const pos = this.getScrollPosition();
let desiredPathAmt = 0;
if (pos < 0.8) {
desiredPathAmt = 1;
}
this.pathAmt += transitionFactor * (desiredPathAmt - this.pathAmt);
if (this.pathAmt >= 0.99) {
this.pathAmt = 1;
}
}
// some max iterations to stop it from hanging
for (let i = 0; i < 20; i ++) {
if (this.niceAnimAmt >= this.animAmt) {
break;
}
this.niceAnimAmt += 1 / this.numPoints;
this.addToPath();
}
}
addToPath() {
if (this.fourierData.length == 0) {
return;
}
let runningX = 0;
let runningY = 0;
const numFouriers = Math.round(slurp(2, this.fourierData.length, this.fourierAmt));
for (let i = 0; i < numFouriers; i ++) {
const amplitude = this.fourierData[i].amplitude;
const angle = 2 * Math.PI * this.fourierData[i].freq * this.niceAnimAmt + this.fourierData[i].phase;
runningX += amplitude * Math.cos(angle);
runningY += amplitude * Math.sin(angle);
}
this.fourierPath.push({x: runningX, y:runningY});
while (this.fourierPath.length > this.numPoints * this.pathAmt && this.fourierPath.length > 0) {
this.fourierPath.shift();
}
}
render() {
this.clear();
this.renderPath(this.fourierPath);
this.renderCircles();
}
renderPath(path) {
for (let i = 0; i < path.length - 1; i ++) {
this.context.beginPath();
this.context.strokeStyle = palette.blue;
this.context.lineWidth = 2;
this.context.moveTo(path[i].x, path[i].y);
this.context.lineTo(path[i+1].x, path[i+1].y);
this.context.stroke();
}
}
renderCircles() {
if (this.fourierData.length == 0) {
return;
}
let runningX = 0;
let runningY = 0;
const numFouriers = Math.round(slurp(2, this.fourierData.length, this.fourierAmt));
for (let i = 0; i < numFouriers; i ++) {
const amplitude = this.fourierData[i].amplitude;
const angle = 2 * Math.PI * this.fourierData[i].freq * this.animAmt + this.fourierData[i].phase;
runningX += amplitude * Math.cos(angle);
runningY += amplitude * Math.sin(angle);
if (i == 0) {
continue; // we skip the first one because we just don't care about rendering the constant term
}
if (amplitude < 0.5) {
continue; // skip the really tiny ones
}
this.context.beginPath();
this.context.strokeStyle = palette.cyan;
this.context.globalAlpha = 0.7;
this.context.lineWidth = 1;
this.context.moveTo(runningX, runningY);
this.context.arc(runningX, runningY, amplitude, angle - Math.PI, angle + Math.PI);
this.context.stroke();
}
this.context.globalAlpha = 1;
}
}