-
-
Notifications
You must be signed in to change notification settings - Fork 574
/
View2D.ts
90 lines (77 loc) · 2.21 KB
/
View2D.ts
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
import {PlaybackState, SimpleSignal, lazy} from '@motion-canvas/core';
import {initial, signal} from '../decorators';
import {nodeName} from '../decorators/nodeName';
import {useScene2D} from '../scenes/useScene2D';
import type {Node} from './Node';
import {Rect, RectProps} from './Rect';
export interface View2DProps extends RectProps {
assetHash: string;
}
@nodeName('View2D')
export class View2D extends Rect {
@lazy(() => {
const frameID = 'motion-canvas-2d-frame';
let frame = document.querySelector<HTMLDivElement>(`#${frameID}`);
if (!frame) {
frame = document.createElement('div');
frame.id = frameID;
frame.style.position = 'absolute';
frame.style.pointerEvents = 'none';
frame.style.top = '0';
frame.style.left = '0';
frame.style.opacity = '0';
frame.style.overflow = 'hidden';
document.body.prepend(frame);
}
return frame.shadowRoot ?? frame.attachShadow({mode: 'open'});
})
public static shadowRoot: ShadowRoot;
@initial(PlaybackState.Paused)
@signal()
public declare readonly playbackState: SimpleSignal<PlaybackState, this>;
@initial(0)
@signal()
public declare readonly globalTime: SimpleSignal<number, this>;
@signal()
public declare readonly assetHash: SimpleSignal<string, this>;
public constructor(props: View2DProps) {
super({
composite: true,
fontFamily: 'Roboto',
fontSize: 48,
lineHeight: '120%',
textWrap: false,
fontStyle: 'normal',
...props,
});
this.view2D = this;
View2D.shadowRoot.append(this.element);
this.applyFlex();
}
public override dispose() {
this.removeChildren();
super.dispose();
}
public override render(context: CanvasRenderingContext2D) {
this.computedSize();
this.computedPosition();
super.render(context);
}
/**
* Find a node by its key.
*
* @param key - The key of the node.
*/
public findKey<T extends Node = Node>(key: string): T | null {
return (useScene2D().getNode(key) as T) ?? null;
}
protected override requestLayoutUpdate() {
this.updateLayout();
}
protected override requestFontUpdate() {
this.applyFont();
}
public override view(): View2D {
return this;
}
}