-
Notifications
You must be signed in to change notification settings - Fork 0
/
appendcanvases.js
313 lines (258 loc) · 9.33 KB
/
appendcanvases.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//Deliquescent
//the hanhanhan version
//A fork of:
//http://codepen.io/tmrDevelops/pen/OPZKNd
//Tiffany Rayside
//canvas
(function appendCanvases() {
var canvas = document.getElementById('canv-one');
canvas.className = "controlvariables"
var context = canvas.getContext('2d');
canvas.width = 500;
canvas.height = 300;
context.lineWidth = 5
cvCanvasHeight = 275
cvCanvasWidth = 300
//y height (canvas height)
const w = 150; //grid sq width
const h = 150; //grid sq height
var rows = 1//canvas.height/w; //number of rows
var columns = 3//canvas.width/w; //number of columns
var j = 1
var space_w = (canvas.width - (columns - 1) * w) / 2
const KX1 = 0.013; //X axis amplification - multiplier for difference between resting position and pulled point
const KX2 = 0.025; //X axis decay
const KY1 = 0.01; //Y axis amplification - multiplier for difference between resting position and pulled point
const KY2 = 0.035; //decay
var parts; //particles
var colorCycle = 0; //color offset which gets incremented with time
var mouseX = 0; //mouse x
var mouseY = 0; //mouse y
var mouseDown = false; //mouse down flag\
var displacementMax = 0;
var colorScale = 1;
// plot control variables as function of time
var timeStack = [] // time points
var variablesStack = {} // objlit of arrays of control variables over time
var timepoints = 200 //
var controlVariables =
[//'xPull',
'yPull',
//'xShift',
'yShift',
//'displacement',
//'off_dx',
//'off_dy',
//'x',
//'y'
]
var canvases = {}
var contexts = {}
// var yScales = { default: 1}
// var precision = { default: 1 }
// set up each control variable canvas
var canvasSection = document.getElementById('canvases')
for(var i = 0; i < controlVariables.length; i++){
canvases[controlVariables[i]] = document.createElement('canvas')
var div = document.createElement('div')
var currentValue = document.createElement('var')
var br = document.createElement('br')
currentValue.dataset.value = controlVariables[i]
canvases[controlVariables[i]].id = controlVariables[i]
canvases[controlVariables[i]].height = cvCanvasHeight
canvases[controlVariables[i]].width = cvCanvasWidth
contexts[controlVariables[i]] = canvases[controlVariables[i]].getContext('2d')
contexts[controlVariables[i]].lineWidth = 3
canvasSection.appendChild(div)
div.className = "canvas-group"
div.appendChild(currentValue)
// div.appendChild(br)
div.appendChild(canvases[controlVariables[i]])
}
var labels = [...document.querySelectorAll('[data-value]')]
// set up deliquescent demo with few points
var Part = function() {
this.x = 0; //x pos
this.y = 0; //y pos
this.xPull = 0;
this.yPull = 0;
this.xShift = 0; //velocity x
this.yShift = 0; //velocity y
this.ind_x = 0; //index x
this.ind_y = 0; //index y
//this.displacement = 0; //distance from resting position
this.off_dx = 0; //distance along x axis from resting position
this.off_dy = 0; //distance along y axis from resting position
};
Part.prototype.initializeStack = function () {
};
Part.prototype.frame = function frame() {
if (this.ind_x == 0 || this.ind_x == columns - 1) {
//pin edges for stability
this.x = this.ind_x * w + space_w;
this.y = this.ind_y * h;
return;
}
//off_dx, off_dy = offset distance x, y
//distance from resting position
var off_dx = this.ind_x * w + space_w - this.x;
var off_dy = this.ind_y * h - this.y;
this.off_dx = off_dx;
this.off_dy = off_dy;
this.displacement = Math.sqrt(off_dx * off_dx + off_dy * off_dy);
this.xPull = 0;
this.yPull = 0;
this.xPull -= this.x - parts[this.ind_x - 1].x;
this.yPull -= this.y - parts[this.ind_x - 1].y;
this.xPull -= this.x - parts[this.ind_x + 1].x;
this.yPull -= this.y - parts[this.ind_x + 1].y;
//amplification * net pull - decaying damping
this.xShift += KX1 * this.xPull - KX2 * this.xShift;
this.yShift += KY1 * this.yPull - KY2 * this.yShift;
this.ind_x * w + space_w
//this.x += this.xShift;
this.y += this.yShift;
if (mouseDown) {
var dx = 0//this.x - mouseX;
var dy = this.y - mouseY;
var displacement = Math.sqrt(dx * dx + dy * dy);
if (displacement < 100) {
displacement = displacement < 10 ? 10 : displacement;
//this.x -= dx / displacement * 5;
this.y -= dy / displacement * 5;
}
}
};
Part.prototype.displacementStyle = function displacementStyle(){
//note: displacement is always positive,
//off_dx and off_dy are positive and negative
//hue is offset by a cycling color, in a 120 deg window normalized by % max displacement (color)
var hue = colorCycle + 120 * this.displacement / colorScale;
var saturation_offset = 40;
var saturation = saturation_offset + this.displacement / colorScale;
saturation = saturation > 90 ? 90 : saturation;
saturation = saturation < 40 ? 40 : saturation;
saturation = saturation + "%";
var lightness_offset = 60;
var lightness = lightness_offset + this.off_dy / colorScale;
lightness = lightness > 80 ? 80 : lightness;
lightness = lightness < 40 ? 40 : lightness;
lightness = lightness + "%";
var alpha_offset = 0.6;
var alpha = alpha_offset + this.off_dx;
alpha = alpha > 1 ? 1 : alpha;
alpha = alpha < 0.2 ? 0.2 : alpha;
context.fillStyle = 'hsla(' + hue + ',' + saturation + ', ' + lightness + ', ' + alpha +')';
context.strokeStyle = 'hsla(' + hue + ',' + saturation + ', ' + lightness + ', ' + alpha +')';
context.beginPath();
}
function initializeArray() {
parts = []; //particle array
for (var i = 0; i < columns; i++) {
//parts.push([]);
//for (var j = 0; j < rows; j++) {
var p = new Part();
p.ind_x = i;
p.ind_y = j;
p.x = i * w + space_w;
p.y = j * h;
parts[i] = p;
//}
}
}
//draw grid
function draw(i) {
var p = parts[i];
// these commented lines are fun to play with -- change drawing patterns
var pAcross = parts[i+1]; //across row
//var pDown = parts[i + 1][j]; //down column
if(pAcross){
context.moveTo(p.x, p.y);
context.lineTo(pAcross.x, pAcross.y);
}
context.arc(p.x, p.y, 20, 0, 2*Math.PI);
//context.moveTo(p.x, p.y);
//context.lineTo(pDown.x, pDown.y);
// var pUp = parts[i][j - 1];
// var pDownRight = parts[i + 1][j + 1];
// var pDownLeft = parts[i - 1][j + 1];
// context.moveTo(p.x, p.y);
// //context.lineTo(pUp.x, pUp.y);
// context.moveTo(p.x, p.y);
// context.lineTo(pDownRight.x, pDownRight.y);
// context.moveTo(p.x, p.y);
// context.lineTo(pDownLeft.x, pDownLeft.y);
}
//mouse
canvas.onmousedown = () => mouseDown = true;
canvas.onmouseup = () => mouseDown = false;
canvas.onmousemove = function MSMV(e) {
var rect = canvas.getBoundingClientRect();
mouseX = e.clientX - rect.left;
mouseY = e.clientY - rect.top;
}
// doesn't work! assigns string 'item' instead of value
// controlVariables.forEach(item => variablesStack.push({item:[]}))
controlVariables.forEach(item => variablesStack[item] = [])
initializeArray();
run();
function run() {
//wipe canvas
context.fillStyle = "hsla(0, 5%, 5%, .1)";
context.fillRect(0, 0, canvas.width, canvas.height);
context.translate(0,0)
colorCycle -= 1;
displacementMax = 0;
//looping through array of points
for (var i = 0; i < columns; i++) {
var p = parts[i];
context.beginPath();
p.frame();
p.displacementStyle();
// if(i === 0 || i === columns - 1){
// context.fillStyle = 'white'
// context.strokeStyle = 'white'
// }
draw(i);
context.fill()
context.stroke();
colorScale = 100;
// plot each control variable
if(i === 1){
// x axis values
timeStack.push(Date.now())
// y axis values
controlVariables.forEach(value => variablesStack[value].push(p[value]))
// slide the time window
if(timeStack.length > timepoints) {
timeStack.shift()
controlVariables.forEach(value => variablesStack[value].shift())
}
// live time-based plot
controlVariables.forEach(drawCanvas)
}
}
window.requestAnimationFrame(run);
context.translate(-0,-0)
}
// append canvases
var xInt = cvCanvasWidth / timepoints
function drawCanvas(value){
// contexts[value].fillStyle = 'orange'
// contexts[value].fillRect(0,0,canvases[value].width, canvases[value].height)
contexts[value].clearRect(0, 0, canvases[value].width, canvases[value].height)
contexts[value].strokeRect(0, 0, canvases[value].width, canvases[value].height)
contexts[value].translate(0, canvases[value].height/2)
contexts[value].beginPath()
contexts[value].moveTo(0,0)
var label = labels.filter(l => l.dataset.value === value)[0]
for(var t=0; t < timepoints - 1; t++){
var xTime = xInt * t
var yVal = variablesStack[value][t]
contexts[value].lineTo(xTime, yVal)
label.innerHTML = yVal ? `${value}: ${yVal.toFixed()}` : `${value}: `
}
contexts[value].stroke()
contexts[value].translate(0, -canvases[value].height/2)
}
})()