-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
295 lines (251 loc) · 7.08 KB
/
main.js
File metadata and controls
295 lines (251 loc) · 7.08 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* CONSTANTS AND GLOBALS */
const lineCanvas = d3.select("#lineCanvas"),
flowCanvas = d3.select("#fieldCanvas"),
context = lineCanvas.node().getContext("2d"),
flowCxt = flowCanvas.node().getContext("2d"),
controlsIconWidth = 30,
controlsWidth = controlsIconWidth * 3,
controlPanelWidth = 200,
width = (lineCanvas.node().width = flowCanvas.node().width = window.innerWidth),
height = (lineCanvas.node().height = flowCanvas.node().height = window.innerHeight),
opacityAlpha = 0.0075,
ease = d3.easeCubic;
let controlContainer,
canvasControl,
play,
pause,
points = [],
animationTimer = false,
friction = 0.99,
scale = 0.003,
res = 10;
/* INITIAL STATE */
let state = {
controlsOpen: false,
numPoints: 1200,
speed: 0.025,
showFlow: true,
paused: false,
};
// user controls
const controls = {
numPoints: {
type: "text",
display: "number of lines",
},
showFlow: {
type: "checkbox",
display: "show flow fields"
}
};
// run the thang
init()
/* SET STATE FUNCTION */
function setState(nextState, callback = null) {
state = Object.assign({}, state, nextState);
console.log("state update", state);
if(callback) { callback() };
}
function init() {
console.log("state:", state)
lineCanvas
.on("click", () => pauseOrPlay())
controlContainer = d3.select("div#controlsContainer")
.style("width", `${controlsWidth + controlPanelWidth}px`)
.style("right", `-${controlPanelWidth}px`)
// replay
controlContainer
.append("div")
.attr("class", "replay")
.style("right", `-${controlPanelWidth}px`)
.on("click", () => {
drawAll()
})
.append("img")
.attr("src", "./assets/replay.svg")
.style("width", `${controlsIconWidth}px`)
.style("height", `${controlsIconWidth}px`)
// save
controlContainer
.append("div")
.attr("class", "save")
.style("right", `-${controlPanelWidth}px`)
.append("a")
.attr('download', 'flowfields.png')
.on("click", function() {
/* we need to update href to the current canvas at the moment of click so that it saves the current canvas and not the initial blank canvas */
this.href = lineCanvas.node().toDataURL("image/png").replace("image/png", "image/octet-stream")
})
.append("img")
.attr("src", "./assets/save.svg")
.style("width", `${controlsIconWidth}px`)
.style("height", `${controlsIconWidth}px`)
// cog
controlContainer
.append("div")
.attr("class", "cog")
.style("right", `-${controlPanelWidth}px`)
.on("click", () => {
setState({ controlsOpen: !state.controlsOpen }, toggleControlPanel)
}).append("img")
.attr("src", "./assets/cog.svg")
.style("width", `${controlsIconWidth}px`)
.style("height", `${controlsIconWidth}px`)
const controlPanel = controlContainer
.append("div")
.attr("class", "controlPanel")
.style("width", `${controlsWidth + controlPanelWidth}px`)
.style("right", `-${controlPanelWidth}px`)
const controlItems = controlPanel
.selectAll(".controlItem")
.data(Object.entries(controls))
.join("div")
.attr("class", "controlItem")
controlItems
.append("input")
.attr("type", ([_, details]) => details.type)
.attr("class", ([_, details]) => details.type)
.property("checked", ([name, _]) => state[name])
.on("click", ([name, details]) => {
details.type === 'text'
? null
: setState({ [name]: !state[name] }, toggleFields)
})
// specific to the text input
.attr("size", 5)
.on("keypress", function([name, _]) {
if (d3.event.keyCode === 13) setState({ [name]: +this.value}, drawAll())
})
controlItems
.append("div")
.attr("class", ([_, details]) => details.type)
.classed("checked", ([name, _]) => name === state[name])
.text(([_, details]) => details.display)
// canvas play / pause
canvasControl = d3.select("#container").selectAll("img.canvasControl")
.data(["play", "pause"])
.join("img")
.attr("src", d => `./assets/${d}.svg`)
.attr("class", d => `canvasControl ${d}`)
.attr("width", width / 4)
.attr("height", height / 4)
.on("click", function() {
pauseOrPlay()
})
drawAll()
}
function refreshPoints() {
// create points
points = [];
for (let y = 0; y < state.numPoints; y += 1) {
points.push({
x: 0,
y: 0,
vx: Math.random(),
vy: Math.random(),
});
}
noise.seed(Math.random());
}
function resetTimer() {
if (animationTimer) { animationTimer.stop() }
animationTimer = false
}
function pauseOrPlay() {
if (!state.paused) {
animationTimer.stop()
setState({ paused: true })
} else {
animationTimer.restart(animation)
setState({ paused: false })
}
canvasControl.classed("possession", d => d === "pause" ? state.paused : !state.paused)
}
function cleanLines() {
context.clearRect(0, 0, width, height);
}
function toggleControlPanel() {
controlContainer
.transition()
.duration(500)
.style("right", state.controlsOpen ? "0px" : `-${controlPanelWidth}px`)
}
function drawLines() {
resetTimer();
cleanLines();
// get new points
refreshPoints();
// draw timer
animationTimer = d3.timer(animation);
}
function drawAll() {
setState({ paused : false})
resetTimer()
cleanFields()
cleanLines()
drawLines();
if (state.showFlow) drawFlowField()
}
function animation(elapsed) {
// compute how far through the animation we are (0 to 1)
const t = Math.min(1, ease(elapsed / 30000));
drawLineInterval()
// if this animation is over
if (t === 1) {
// stop this animationTimer since we are done animating.
animationTimer.stop();
}
}
function toggleFields() {
state.showFlow ? drawFlowField() : cleanFields()
}
function cleanFields() {
flowCxt.clearRect(0, 0, width, height);
}
function drawFlowField() {
flowCxt.lineWidth = 0.1;
for (var x = 0; x < width; x += res) {
for (var y = 0; y < height; y += res) {
var value = getValue(x, y);
flowCxt.save();
flowCxt.translate(x, y);
flowCxt.rotate(value);
flowCxt.beginPath();
flowCxt.moveTo(0, 0);
flowCxt.lineTo(res, 0);
flowCxt.stroke();
flowCxt.restore();
}
}
}
function drawLineInterval() {
context.lineWidth = 0.15;
context.strokeStyle = 'black'
for (let i = 0; i < points.length; i++) {
// get each point and do what we did before with a single point
const p = points[i];
let value = getValue(p.x, p.y);
p.vx += Math.cos(value) * state.speed;
p.vy += Math.sin(value) * state.speed;
// console.log(p.vx, p.vy)
// move to current position
context.beginPath();
context.moveTo(p.x, p.y);
// add velocity to position and line to new position
p.x += p.vx;
p.y += p.vy;
context.lineTo(p.x, p.y);
context.stroke();
// apply some friction so point doesn't speed up too much
p.vx *= friction;
p.vy *= friction;
// wrap around edges of screen
if (p.x > width) p.x = 0;
if (p.y > height) p.y = 0;
if (p.x < 0) p.x = width;
if (p.y < 0) p.y = height;
}
}
function getValue(x, y) {
return noise.perlin2(x * scale, y * scale) * Math.PI * 2;
}