-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunCanvas.js
190 lines (173 loc) · 5.73 KB
/
runCanvas.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
/*jshint esversion: 6 */
// @ts-check
/**
* Simple version of an auto-update slider to have looping time
*
* Designed for making quick UIs for CS559 demos
*
* Students are welcome to read the code to understand it, but are not
* expected to modify this file.
*
* The simple entry point (that most students will use) is the "runCanvas"
* function that is at the end of the file. The RunCanvas class is the internal
* implementation. Students are welcome to read the code - or to use it directly
* but it isn't necessary for class.
*/
import { insertAfter } from "./inputHelpers.js";
/**
* the main thing is implemented as a class in case you want access to everything
*/
export class RunCanvas {
/**
*
* @param {HTMLCanvasElement|string} canvasNameOrCanvas
* @param {function(HTMLCanvasElement, Number) : any} drawFunc
* @param {*} noLoop
*/
constructor(canvasNameOrCanvas, drawFunc, noLoop = false) {
/* so often, we pass the wrong thing - so make it work either way */
let canvas; // = undefined
let canvasName; // = undefined
if (canvasNameOrCanvas instanceof HTMLCanvasElement) {
canvas = canvasNameOrCanvas;
canvasName = canvas.id;
} else {
canvasName = canvasNameOrCanvas;
canvas = /** @type {HTMLCanvasElement} */ (document.getElementById(
canvasName
));
}
if (!canvas) {
throw "RunCanvas without a Canvas to attach to!";
}
if (!canvasName) {
canvasName = "canvas-" + Math.trunc(performance.now()).toString();
console.log("RunCanvas with an unnamed canvas - naming it " + canvasName);
canvas.id = canvasName;
}
this.canvas = /** @type {HTMLCanvasElement} */ (canvas);
this.canvasName = canvasName;
this.drawFunc = drawFunc;
this.noloop = noLoop;
// some style parameters
this.digits = 2;
// keep track of time - so we can measure step times
this.lastTime = undefined;
// we need to store the value
this.value = 0;
// create the elements
this.br = document.createElement("br");
this.br.id = canvasName + "-br";
this.range = document.createElement("input");
this.range.id = canvasName + "-slider";
this.range.setAttribute("type", "range");
this.range.style.width = String(this.canvas.width - 50 - 20 - 10) + "px";
// give default values for range
this.setupSlider(0, 1, 0.01);
this.text = document.createElement("input");
this.text.id = canvasName + "-text";
this.text.setAttribute("type", "text");
this.text.style.width = "50px";
this.text.setAttribute("readonly", "1");
this.runbutton = document.createElement("input");
this.runbutton.id = canvasName + "-run";
this.runbutton.setAttribute("type", "checkbox");
this.runbutton.style.width = "20px";
this.br2 = document.createElement("br");
this.br2.id = canvasName + "-br2";
insertAfter(this.br, this.canvas);
insertAfter(this.runbutton, this.br);
insertAfter(this.text, this.runbutton);
insertAfter(this.range, this.text);
insertAfter(this.br2, this.range);
let self = this;
this.runbutton.onchange = function() {
if (self.noloop && Number(self.range.value) >= 1) {
self.setValue(0);
}
self.tick(undefined);
};
this.range.oninput = function() {
let val = Number(self.range.value);
self.setValue(val);
};
}
/**
* Setup aspects of the slider - as a function in case you need to change them
* @param {Number} min
* @param {Number} max
* @param {Number} step
*/
setupSlider(min, max, step) {
this.range.setAttribute("min", String(min));
this.range.setAttribute("max", String(max));
this.range.setAttribute("step", String(step));
}
// set the value of the slide - make sure to update everything
setValue(value) {
this.value = value;
this.range.value = String(value);;
this.text.value = value.toFixed(this.digits);
if (this.drawFunc) {
this.drawFunc(this.canvas, value);
}
}
/**
* this function doesn't directly go as a req animation from (it's a method
* not a function) - but it acts as if it was
* It is possible that tick is called without a timestamp (if the button is clicked)
* - if that's the case, assume a delta of 0 - we generate a redraw at the current frame
* @param {DOMHighResTimeStamp} timestamp
*/
tick(timestamp) {
// convert delta to "frames" (at 60fps)
const delta = ((timestamp && this.lastTime) ? timestamp-this.lastTime : 0) * 1.0/60.0;
this.lastTime = timestamp;
let maxV = Number(this.range.max);
let stepV = Number(this.range.step);
let value = this.value + stepV * delta;
if (this.noloop) {
if (value >= maxV) {
this.runbutton.checked = false;
}
value = Math.min(maxV, value);
} else {
value = value % maxV;
}
this.setValue(value);
if (this.runbutton.checked) {
let self = this;
window.requestAnimationFrame(function(timestamp) {
self.tick(timestamp);
});
}
}
}
/**
* simple entry point - give it the name of a canvas, and it guesses the rest
* but it also loses access to all the parameters
*
* Note that the drawing function gets the Canvas element and the slider value
* not the time!
*
* @param {HTMLCanvasElement|string} canvasName
* @param {function(HTMLCanvasElement, Number) : any} [drawFunc]
* @param {*} initial
* @param {*} noloop
* @param {*} min
* @param {Number} [max=1]
* @param {Number} [step=.02] - steps per frame (at 60fps)
*/
export function runCanvas(
canvasName,
drawFunc = undefined,
initial = 0.5,
noloop = false,
min = 0,
max = 1,
step = 0.02
) {
let rc = new RunCanvas(canvasName, drawFunc, noloop);
rc.setupSlider(min, max, step);
rc.setValue(initial);
}