-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.ts
More file actions
65 lines (52 loc) · 1.35 KB
/
Copy pathsimulation.ts
File metadata and controls
65 lines (52 loc) · 1.35 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
import p5 from 'p5';
import { Sketch, Theme } from '../models';
export abstract class Simulation {
protected resizer: ResizeObserver;
protected processor?: p5;
protected height: number = 0;
protected width: number = 0;
protected theme: Theme = new Theme(
this.start.bind(this)
);
protected abstract run(s: p5): void;
protected sketch(s: p5): Sketch {
return new Sketch(
s,
this.theme,
this.width,
this.height
);
}
constructor(
protected element: HTMLElement
) {
this.width = element.clientWidth;
this.height = element.clientHeight;
const self = this;
this.resizer = new ResizeObserver(this.handleResize.bind(self));
this.resizer.observe(element);
}
protected handleResize() {
this.height = this.element.clientHeight;
this.width = this.element.clientWidth;
if (this.processor)
this.start();
}
destroy() {
this.reset();
this.resizer.disconnect();
}
reset() {
if (this.processor) {
this.processor.remove();
this.processor = undefined;
}
}
start() {
this.reset();
this.processor = new p5(
this.run.bind(this),
this.element
);
}
}