-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (67 loc) · 2.15 KB
/
index.js
File metadata and controls
77 lines (67 loc) · 2.15 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
export class Context2D {
constructor(options = {}) {
this.rootElement = options.rootElement instanceof HTMLElement ? options.rootElement : document.getElementById(options.rootElement) || document.body;
this.canvas = document.createElement('canvas');
this.rootElement.appendChild(this.canvas)
this.width = options.width || this.rootElement.offsetWidth;
this.height = options.height || this.rootElement.offsetHeight;
this.canvas.width = this.width;
this.canvas.height = this.height;
this.ctx = this.canvas.getContext('2d');
this.draw = this.draw.bind(this);
this.cache = new Map();
this.resizeThrottle = options.resizeThrottle === 0 ? 0 : options.resizeThrottle || 250;
this.resizeOnWindowResize = options.resizeOnWindowResize === false ? false : true;
if (this.resizeOnWindowResize) {
window.addEventListener('resize', () => {
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(() => {
this.#resizeCanvas();
}, this.resizeThrottle);
});
}
}
#resizeCanvas() {
this.width = this.rootElement.offsetWidth;
this.height = this.rootElement.offsetHeight;
this.canvas.width = this.width;
this.canvas.height = this.height;
this.cache.clear();
}
#clearCanvas() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
#memoize = (fn, config = {}) => (...args) => {
const key = JSON.stringify(...args);
if (this.cache.has(key)) {
return this.cache.get(key);
}
const result = fn(...args);
this.cache.set(key, result);
return result;
}
#oscillate({
// Speed is in Hz, so 1 is one cycle per second
speed = 1,
from = -1,
to = 1,
offset = 0
}) {
const now = Date.now();
// Convert speed to radians
const _speed = speed * 2 * Math.PI;
return Math.sin(_speed * now / 1000 + offset) * (to - from) / 2 + (to + from) / 2;
}
draw(cb) {
this.#clearCanvas();
cb({
ctx: this.ctx,
w: this.width,
h: this.height,
memoize: this.#memoize,
oscillate: this.#oscillate
})
requestAnimationFrame(() => this.draw(cb));
}
}
export default Context2D;