-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdweetdiv.js
233 lines (202 loc) · 6.98 KB
/
dweetdiv.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
"use strict";
/**
* @typedef {Object} Credits
* @property {string} title
* @property {string} author
* @property {string} link
*/
/**
* @typedef {Object} Options
* @property {number} [fps] has to be number => 0 (including Infinity)
* @property {number} [intermediateDraws]
* @property {boolean} [showCode]
* @property {Credits} [credits]
*/
/**
* plop a dweet into a div
* @param {string} id the id of the div for the canvas
* @param {string} code the dweet code that defines the draw cycle
* @param {Options} options configuration
*/
function addDweet(id, code, options) {
/** cap for how many updates to do in one animation frame */
const fps = options?.fps === undefined ? 60 : options.fps;
// if fps is 120, you probably want 2 intermediate draws by default; if 60 you
// probably just want 1
const maxSteps =
options?.intermediateDraws === undefined
? Math.max(1, Math.ceil(fps / 60))
: options?.intermediateDraws;
// error checking for type and range errors
const numErr = (str, num) => {
if (typeof fps !== "number" || isNaN(fps)) {
throw new TypeError(str + "has to be a number that is also not NaN");
}
};
numErr("fps", fps);
numErr("intermediateDraws", maxSteps);
if (fps < 0) {
throw new RangeError("fps has to be greater or equal to 0");
}
if (maxSteps <= 0 || maxSteps !== Math.floor(maxSteps)) {
throw new RangeError(
"drawIntermediate has to be integer greater than 0 or Infinity"
);
}
if (typeof code !== "string") {
throw new TypeError("type of code has to be a string");
}
// will also be false with unlocked framerate because `maxSteps` will be
// Infinite due to the FPS being infinite
const drawIntermediate = maxSteps !== Infinity;
let totalSteps = 0;
const unlock = fps === Infinity;
// try to get the div for adding the display canvas
const targetDiv = document.getElementById(id);
if (targetDiv === null) {
throw new Error(
"couldn't find div to put the canvas (did you get the id wrong?)"
);
}
let momentaryPause = false;
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") momentaryPause = true;
});
const styleDiv = () => {
const styledDiv = document.createElement("div");
styledDiv.style.width = "100%";
styledDiv.style.backgroundColor = "black";
styledDiv.style.color = "white";
styledDiv.style.padding = "4px";
styledDiv.style.boxSizing = "border-box";
styledDiv.style.fontFamily = "monospace";
styledDiv.style.wordBreak = "break-all";
return styledDiv;
};
// add the credits div to the document
if (options?.credits) {
const creditsDiv = styleDiv();
const title = options.credits?.title;
const author = options.credits?.author;
const link = options.credits?.link;
// creating elements instead of directly creating a string of html to avoid
// breaking the html with input that contains markup, even though it's
// verbose
const creditsSpan = document.createElement("span");
creditsSpan.innerText =
"/* " + (title ? " " + title : "") + (author ? " by " + author : "");
creditsDiv.appendChild(creditsSpan);
if (link) {
creditsSpan.innerText += " ";
const creditsLink = document.createElement("a");
creditsLink.innerText = link;
creditsLink.href = link;
creditsLink.style.color = "#aaaaaa";
creditsDiv.appendChild(creditsLink);
}
const closingComment = document.createElement("span");
closingComment.innerText = " */";
creditsDiv.appendChild(closingComment);
targetDiv.appendChild(creditsDiv);
}
// dwitter shorthand
const C = Math.cos;
const S = Math.sin;
const T = Math.tan;
/**
* takes in values of any type and makes an rbga string, mimicking the
* behavior of dwitter
* @param {*} r red
* @param {*} g green
* @param {*} b blue
* @param {*} a alpha
*/
const R = (r, g, b, a = 1) => `rgba(${r | 0},${g | 0},${b | 0},${a})`;
const c = document.createElement("canvas");
c.width = 1920;
c.height = 1080;
c.style.width = "100%";
/** the div to hide extra length when canvas has a changed width */
const containingDiv = document.createElement("div");
// for constant aspect ratio
containingDiv.style.width = "100%";
containingDiv.style.height = "0";
containingDiv.style.paddingBottom = "56.25%"; // for 16:9 aspect ratio
containingDiv.style.overflow = "hidden";
containingDiv.appendChild(c);
targetDiv.appendChild(containingDiv);
const x = c.getContext("2d");
let u = new Function("t", "c", "x", "R", "C", "S", "T", code);
// create and add the code div
if (options?.showCode) {
const codeDiv = styleDiv();
codeDiv.innerText = code;
targetDiv.appendChild(codeDiv);
}
/** returns whether the canvas is visible */
const visible = () => {
const bounding = c.getBoundingClientRect();
return (
bounding.left < window.innerWidth &&
bounding.right > 0 &&
bounding.top < window.innerHeight &&
bounding.bottom > 0
);
};
/**
* how much the dweet's time should differ from the time returned by
* requestAnimationFrame
*/
let wastedTime = 0;
/** previous time returned by requestAnimationFrame */
let prevTime = 0;
/** how many times to run the dwitter draw function */
let currSteps = 1; // won't change from 1 if framerate is unlocked
/**
* kick off the animation requests
* @param {number} currTime
*/
const update = (currTime) => {
if (momentaryPause) {
momentaryPause = false;
wastedTime += currTime - prevTime;
// request a new animation frame without doing anything
prevTime = currTime;
requestAnimationFrame(update);
}
let finalTime = (currTime - wastedTime) / 1000;
if (visible()) {
// step animation correctly for locked framerate
if (!unlock) {
// has to be handled separately to avoid division by zero
if (fps === 0) currSteps = 1;
else if (finalTime >= totalSteps / fps) {
const trueSteps = Math.floor(finalTime * fps - totalSteps);
currSteps = Math.min(trueSteps, maxSteps);
// back time up if we are not doing all of the steps
wastedTime += ((trueSteps - currSteps) / fps) * 1000;
totalSteps += currSteps;
}
}
// run the dwitter drawing function, potentially stepping > 1 times if fps
// is higher than your refresh rate, or not at all if fps is lower than
// your refresh rate
if (drawIntermediate) {
for (let i = 0; i < currSteps; i++) {
// draw intermediate steps to canvas even though they aren't drawn to
// the screen
u((totalSteps + (i - currSteps)) / fps, c, x, R, C, S, T);
}
} else if (currSteps) {
u(finalTime, c, x, R, C, S, T);
}
if (fps === 0) return;
} else {
// keep track of time not on screen
wastedTime += currTime - prevTime;
}
prevTime = currTime;
requestAnimationFrame(update);
};
update(0);
}