@vim3d/react 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -1
- package/esm/index.js +1 -0
- package/esm/viewer.js +169 -0
- package/index.d.ts +2 -0
- package/index.js +3 -1
- package/package.json +23 -8
- package/viewer.d.ts +41 -0
- package/viewer.js +172 -0
- package/esm/index.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
1
|
# @vim3d/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Render a Vim3D scene in React. The viewer compiles with `@vim3d/core` and
|
|
4
|
+
uses `@vim3d/threejs` for drawing and playback.
|
|
5
|
+
|
|
6
|
+
```tsx
|
|
7
|
+
import { useRef } from 'react';
|
|
8
|
+
import { Vim3DViewer, type Vim3DViewerHandle } from '@vim3d/react';
|
|
9
|
+
|
|
10
|
+
function ScenePreview({ source }: { source: string }) {
|
|
11
|
+
const viewer = useRef<Vim3DViewerHandle>(null);
|
|
12
|
+
return (
|
|
13
|
+
<>
|
|
14
|
+
<Vim3DViewer ref={viewer} source={source} width="100%" height={360}
|
|
15
|
+
onDiagnostics={console.error} onError={console.error} />
|
|
16
|
+
<button onClick={() => viewer.current?.pause()}>Pause</button>
|
|
17
|
+
<button onClick={() => viewer.current?.play()}>Play</button>
|
|
18
|
+
</>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `source` accepts scene text or a `SceneSource` with a path. Pass
|
|
24
|
+
`compileOptions` for scene FPS, frame count, or asset loading. Keep
|
|
25
|
+
object-valued inputs and `createRenderer` stable across unrelated renders
|
|
26
|
+
to avoid creating a new player.
|
|
27
|
+
- Playback starts automatically unless `autoPlay={false}`. `playing`
|
|
28
|
+
overrides automatic playback; changes to it play or pause the viewer.
|
|
29
|
+
Changing `frame` seeks to that frame, and does not disable playback.
|
|
30
|
+
`fps` sets playback speed, and `loop` wraps a scene with a frame count.
|
|
31
|
+
The ref also exposes `play`, `pause`, `stop`, `seek`, `step`,
|
|
32
|
+
`getFrame`, `getPlayer`, and `getScene`.
|
|
33
|
+
- Set `width`, `height`, `pixelRatio`, and `backgroundColor` to control
|
|
34
|
+
the canvas. `className` and `style` apply to its container. The viewer
|
|
35
|
+
creates a WebGL renderer by default, or uses `createRenderer(canvas)`
|
|
36
|
+
and disposes the returned renderer when the player is replaced or unmounted.
|
|
37
|
+
- `onCompile` receives core's compile result. `onDiagnostics` receives
|
|
38
|
+
compile and frame diagnostics; `onError` receives exceptions during
|
|
39
|
+
setup, prop updates, or imperative viewer actions. `onFrame` receives
|
|
40
|
+
the immutable snapshot after each successful render, and `onReady`
|
|
41
|
+
receives the player after initialization.
|
package/esm/index.js
CHANGED
package/esm/viewer.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { createElement, forwardRef, useEffect, useImperativeHandle, useRef, } from 'react';
|
|
2
|
+
import { compileScene, } from '@vim3d/core';
|
|
3
|
+
import { Vim3DPlayer } from '@vim3d/threejs';
|
|
4
|
+
import { Color, WebGLRenderer } from 'three';
|
|
5
|
+
function playback(props, scene) {
|
|
6
|
+
return {
|
|
7
|
+
fps: props.fps ?? scene.fps,
|
|
8
|
+
loop: props.loop ?? false,
|
|
9
|
+
playing: props.playing ?? props.autoPlay ?? true,
|
|
10
|
+
frame: props.frame,
|
|
11
|
+
background: props.backgroundColor,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function attempt(run, props) {
|
|
15
|
+
try {
|
|
16
|
+
return run();
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (!props.onError)
|
|
20
|
+
throw error;
|
|
21
|
+
props.onError(error instanceof Error ? error : new Error(String(error)));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export const Vim3DViewer = forwardRef(function Vim3DViewer(props, ref) {
|
|
25
|
+
const container = useRef(null);
|
|
26
|
+
const canvas = useRef(null);
|
|
27
|
+
const current = useRef(props);
|
|
28
|
+
const controller = useRef(null);
|
|
29
|
+
const { source, compileOptions, createRenderer, width = 640, height = 360, className, style } = props;
|
|
30
|
+
useImperativeHandle(ref, () => {
|
|
31
|
+
const call = (run) => {
|
|
32
|
+
const player = controller.current?.player;
|
|
33
|
+
return player && attempt(() => run(player), current.current);
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
play: () => call(player => player.play()),
|
|
37
|
+
pause: () => call(player => player.pause()),
|
|
38
|
+
stop: () => call(player => player.stop()),
|
|
39
|
+
seek: frame => call(player => player.seek(frame)),
|
|
40
|
+
step: delta => call(player => player.step(delta)),
|
|
41
|
+
getFrame: () => controller.current?.player.getFrame(),
|
|
42
|
+
getPlayer: () => controller.current?.player,
|
|
43
|
+
getScene: () => controller.current?.player.adapter.scene,
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
useEffect(() => { current.current = props; });
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const output = canvas.current;
|
|
49
|
+
output.style.visibility = 'hidden';
|
|
50
|
+
const result = attempt(() => compileScene(typeof source === 'string' ? { path: 'scene.vim3d', text: source } : source, compileOptions), current.current);
|
|
51
|
+
if (!result)
|
|
52
|
+
return;
|
|
53
|
+
current.current.onCompile?.(result);
|
|
54
|
+
if ('diagnostics' in result) {
|
|
55
|
+
current.current.onDiagnostics?.(result.diagnostics);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const host = container.current;
|
|
59
|
+
const scene = result.value;
|
|
60
|
+
let renderer;
|
|
61
|
+
let player;
|
|
62
|
+
let resizeObserver;
|
|
63
|
+
let listenToWindow = false;
|
|
64
|
+
let rendered = false;
|
|
65
|
+
let lastWidth = -1, lastHeight = -1, lastRatio = -1;
|
|
66
|
+
let settings = playback(current.current, scene);
|
|
67
|
+
function resize() {
|
|
68
|
+
const w = host.clientWidth, h = host.clientHeight;
|
|
69
|
+
if (!w || !h)
|
|
70
|
+
return false;
|
|
71
|
+
const ratio = current.current.pixelRatio ?? (window.devicePixelRatio || 1);
|
|
72
|
+
if (w === lastWidth && h === lastHeight && ratio === lastRatio)
|
|
73
|
+
return false;
|
|
74
|
+
if (ratio !== lastRatio)
|
|
75
|
+
renderer.setPixelRatio?.(ratio);
|
|
76
|
+
renderer.setSize(w, h, false);
|
|
77
|
+
lastWidth = w;
|
|
78
|
+
lastHeight = h;
|
|
79
|
+
lastRatio = ratio;
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
function background(color) {
|
|
83
|
+
player.adapter.scene.background = color === undefined ? null : new Color(color);
|
|
84
|
+
}
|
|
85
|
+
function onResize() {
|
|
86
|
+
if (controller.current?.player !== player)
|
|
87
|
+
return;
|
|
88
|
+
attempt(() => {
|
|
89
|
+
if (resize() && rendered)
|
|
90
|
+
player.renderFrame(player.getFrame());
|
|
91
|
+
}, current.current);
|
|
92
|
+
}
|
|
93
|
+
function dispose() {
|
|
94
|
+
if (controller.current?.player === player)
|
|
95
|
+
controller.current = null;
|
|
96
|
+
resizeObserver?.disconnect();
|
|
97
|
+
if (listenToWindow)
|
|
98
|
+
window.removeEventListener('resize', onResize);
|
|
99
|
+
player?.dispose();
|
|
100
|
+
renderer?.dispose();
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
renderer = createRenderer ? createRenderer(output) : new WebGLRenderer({ canvas: output, antialias: true });
|
|
104
|
+
player = new Vim3DPlayer(scene, {
|
|
105
|
+
renderer: renderer.getSize ? renderer : {
|
|
106
|
+
render: renderer.render.bind(renderer),
|
|
107
|
+
getSize: size => size.set(lastWidth, lastHeight),
|
|
108
|
+
},
|
|
109
|
+
fps: settings.fps, loop: settings.loop,
|
|
110
|
+
onFrame: snapshot => {
|
|
111
|
+
if (!rendered)
|
|
112
|
+
output.style.visibility = '';
|
|
113
|
+
rendered = true;
|
|
114
|
+
current.current.onFrame?.(snapshot);
|
|
115
|
+
},
|
|
116
|
+
onDiagnostics: diagnostics => current.current.onDiagnostics?.(diagnostics),
|
|
117
|
+
});
|
|
118
|
+
controller.current = {
|
|
119
|
+
player,
|
|
120
|
+
sync() {
|
|
121
|
+
const next = playback(current.current, scene);
|
|
122
|
+
if (next.fps !== settings.fps)
|
|
123
|
+
player.setFps(next.fps);
|
|
124
|
+
if (next.loop !== settings.loop)
|
|
125
|
+
player.setLoop(next.loop);
|
|
126
|
+
const resized = resize();
|
|
127
|
+
const recolored = next.background !== settings.background;
|
|
128
|
+
if (recolored)
|
|
129
|
+
background(next.background);
|
|
130
|
+
let seekSucceeded = false;
|
|
131
|
+
if (next.frame !== settings.frame && next.frame !== undefined) {
|
|
132
|
+
seekSucceeded = 'value' in player.seek(next.frame);
|
|
133
|
+
}
|
|
134
|
+
else if ((resized || recolored) && rendered)
|
|
135
|
+
player.renderFrame(player.getFrame());
|
|
136
|
+
if (next.playing && (seekSucceeded || !settings.playing))
|
|
137
|
+
player.play();
|
|
138
|
+
else if (!next.playing && settings.playing)
|
|
139
|
+
player.pause();
|
|
140
|
+
settings = next;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
resize();
|
|
144
|
+
background(settings.background);
|
|
145
|
+
player.renderFrame(settings.frame ?? 0);
|
|
146
|
+
if (settings.playing && rendered)
|
|
147
|
+
player.play();
|
|
148
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
149
|
+
resizeObserver = new ResizeObserver(onResize);
|
|
150
|
+
resizeObserver.observe(host);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
window.addEventListener('resize', onResize);
|
|
154
|
+
listenToWindow = true;
|
|
155
|
+
}
|
|
156
|
+
current.current.onReady?.(player);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
dispose();
|
|
160
|
+
attempt(() => { throw error; }, current.current);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
return dispose;
|
|
164
|
+
}, [source, compileOptions, createRenderer]);
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
attempt(() => controller.current?.sync(), current.current);
|
|
167
|
+
});
|
|
168
|
+
return createElement('div', { ref: container, className, style: { width, height, ...style } }, createElement('canvas', { ref: canvas, style: { display: 'block', width: '100%', height: '100%' } }));
|
|
169
|
+
});
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.name = void 0;
|
|
3
|
+
exports.Vim3DViewer = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/react';
|
|
5
|
+
var viewer_js_1 = require("./viewer.js");
|
|
6
|
+
Object.defineProperty(exports, "Vim3DViewer", { enumerable: true, get: function () { return viewer_js_1.Vim3DViewer; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/react",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React components for Vim3D scenes.",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/react",
|
|
@@ -20,14 +20,29 @@
|
|
|
20
20
|
"bugs": {
|
|
21
21
|
"url": "https://github.com/pyramation/vim3d/issues"
|
|
22
22
|
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/three": "^0.185.4",
|
|
25
|
+
"three": "^0.185.1",
|
|
26
|
+
"@vim3d/core": "^0.2.0",
|
|
27
|
+
"@vim3d/threejs": "^0.2.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
27
31
|
},
|
|
28
32
|
"devDependencies": {
|
|
29
|
-
"@types/
|
|
33
|
+
"@types/react": "^19.0.0",
|
|
34
|
+
"@types/react-dom": "^19.0.0",
|
|
35
|
+
"jest-environment-jsdom": "^30.4.1",
|
|
30
36
|
"makage": "^0.8.0",
|
|
31
|
-
"
|
|
37
|
+
"react": "^19.0.0",
|
|
38
|
+
"react-dom": "^19.0.0"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"clean": "makage clean",
|
|
42
|
+
"build": "makage build && cp scripts/esm-package.json dist/esm/package.json",
|
|
43
|
+
"build:dev": "makage build --dev && cp scripts/esm-package.json dist/esm/package.json",
|
|
44
|
+
"lint": "eslint . --fix",
|
|
45
|
+
"test": "jest",
|
|
46
|
+
"test:watch": "jest --watch"
|
|
32
47
|
}
|
|
33
|
-
}
|
|
48
|
+
}
|
package/viewer.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
import { type CompiledScene, type CompileOptions, type Diagnostic, type FrameSnapshot, type Result, type SceneSource } from '@vim3d/core';
|
|
3
|
+
import { Vim3DPlayer, type FrameRenderer } from '@vim3d/threejs';
|
|
4
|
+
import { type ColorRepresentation, type Scene } from 'three';
|
|
5
|
+
export interface Vim3DRenderTarget extends FrameRenderer {
|
|
6
|
+
setSize(width: number, height: number, updateStyle?: boolean): void;
|
|
7
|
+
setPixelRatio?(ratio: number): void;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
}
|
|
10
|
+
export interface Vim3DViewerProps {
|
|
11
|
+
source: string | SceneSource;
|
|
12
|
+
compileOptions?: CompileOptions;
|
|
13
|
+
autoPlay?: boolean;
|
|
14
|
+
playing?: boolean;
|
|
15
|
+
frame?: number;
|
|
16
|
+
fps?: number;
|
|
17
|
+
loop?: boolean;
|
|
18
|
+
width?: number | string;
|
|
19
|
+
height?: number | string;
|
|
20
|
+
pixelRatio?: number;
|
|
21
|
+
backgroundColor?: ColorRepresentation;
|
|
22
|
+
className?: string;
|
|
23
|
+
style?: CSSProperties;
|
|
24
|
+
createRenderer?: (canvas: HTMLCanvasElement) => Vim3DRenderTarget;
|
|
25
|
+
onCompile?: (result: Result<CompiledScene>) => void;
|
|
26
|
+
onDiagnostics?: (diagnostics: readonly Diagnostic[]) => void;
|
|
27
|
+
onError?: (error: Error) => void;
|
|
28
|
+
onFrame?: (snapshot: FrameSnapshot) => void;
|
|
29
|
+
onReady?: (player: Vim3DPlayer) => void;
|
|
30
|
+
}
|
|
31
|
+
export interface Vim3DViewerHandle {
|
|
32
|
+
play(): void;
|
|
33
|
+
pause(): void;
|
|
34
|
+
stop(): Result<FrameSnapshot> | undefined;
|
|
35
|
+
seek(frame: number): Result<FrameSnapshot> | undefined;
|
|
36
|
+
step(delta?: number): Result<FrameSnapshot> | undefined;
|
|
37
|
+
getFrame(): number | undefined;
|
|
38
|
+
getPlayer(): Vim3DPlayer | undefined;
|
|
39
|
+
getScene(): Scene | undefined;
|
|
40
|
+
}
|
|
41
|
+
export declare const Vim3DViewer: import("react").ForwardRefExoticComponent<Vim3DViewerProps & import("react").RefAttributes<Vim3DViewerHandle>>;
|
package/viewer.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Vim3DViewer = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const core_1 = require("@vim3d/core");
|
|
6
|
+
const threejs_1 = require("@vim3d/threejs");
|
|
7
|
+
const three_1 = require("three");
|
|
8
|
+
function playback(props, scene) {
|
|
9
|
+
return {
|
|
10
|
+
fps: props.fps ?? scene.fps,
|
|
11
|
+
loop: props.loop ?? false,
|
|
12
|
+
playing: props.playing ?? props.autoPlay ?? true,
|
|
13
|
+
frame: props.frame,
|
|
14
|
+
background: props.backgroundColor,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function attempt(run, props) {
|
|
18
|
+
try {
|
|
19
|
+
return run();
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (!props.onError)
|
|
23
|
+
throw error;
|
|
24
|
+
props.onError(error instanceof Error ? error : new Error(String(error)));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Vim3DViewer = (0, react_1.forwardRef)(function Vim3DViewer(props, ref) {
|
|
28
|
+
const container = (0, react_1.useRef)(null);
|
|
29
|
+
const canvas = (0, react_1.useRef)(null);
|
|
30
|
+
const current = (0, react_1.useRef)(props);
|
|
31
|
+
const controller = (0, react_1.useRef)(null);
|
|
32
|
+
const { source, compileOptions, createRenderer, width = 640, height = 360, className, style } = props;
|
|
33
|
+
(0, react_1.useImperativeHandle)(ref, () => {
|
|
34
|
+
const call = (run) => {
|
|
35
|
+
const player = controller.current?.player;
|
|
36
|
+
return player && attempt(() => run(player), current.current);
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
play: () => call(player => player.play()),
|
|
40
|
+
pause: () => call(player => player.pause()),
|
|
41
|
+
stop: () => call(player => player.stop()),
|
|
42
|
+
seek: frame => call(player => player.seek(frame)),
|
|
43
|
+
step: delta => call(player => player.step(delta)),
|
|
44
|
+
getFrame: () => controller.current?.player.getFrame(),
|
|
45
|
+
getPlayer: () => controller.current?.player,
|
|
46
|
+
getScene: () => controller.current?.player.adapter.scene,
|
|
47
|
+
};
|
|
48
|
+
}, []);
|
|
49
|
+
(0, react_1.useEffect)(() => { current.current = props; });
|
|
50
|
+
(0, react_1.useEffect)(() => {
|
|
51
|
+
const output = canvas.current;
|
|
52
|
+
output.style.visibility = 'hidden';
|
|
53
|
+
const result = attempt(() => (0, core_1.compileScene)(typeof source === 'string' ? { path: 'scene.vim3d', text: source } : source, compileOptions), current.current);
|
|
54
|
+
if (!result)
|
|
55
|
+
return;
|
|
56
|
+
current.current.onCompile?.(result);
|
|
57
|
+
if ('diagnostics' in result) {
|
|
58
|
+
current.current.onDiagnostics?.(result.diagnostics);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const host = container.current;
|
|
62
|
+
const scene = result.value;
|
|
63
|
+
let renderer;
|
|
64
|
+
let player;
|
|
65
|
+
let resizeObserver;
|
|
66
|
+
let listenToWindow = false;
|
|
67
|
+
let rendered = false;
|
|
68
|
+
let lastWidth = -1, lastHeight = -1, lastRatio = -1;
|
|
69
|
+
let settings = playback(current.current, scene);
|
|
70
|
+
function resize() {
|
|
71
|
+
const w = host.clientWidth, h = host.clientHeight;
|
|
72
|
+
if (!w || !h)
|
|
73
|
+
return false;
|
|
74
|
+
const ratio = current.current.pixelRatio ?? (window.devicePixelRatio || 1);
|
|
75
|
+
if (w === lastWidth && h === lastHeight && ratio === lastRatio)
|
|
76
|
+
return false;
|
|
77
|
+
if (ratio !== lastRatio)
|
|
78
|
+
renderer.setPixelRatio?.(ratio);
|
|
79
|
+
renderer.setSize(w, h, false);
|
|
80
|
+
lastWidth = w;
|
|
81
|
+
lastHeight = h;
|
|
82
|
+
lastRatio = ratio;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
function background(color) {
|
|
86
|
+
player.adapter.scene.background = color === undefined ? null : new three_1.Color(color);
|
|
87
|
+
}
|
|
88
|
+
function onResize() {
|
|
89
|
+
if (controller.current?.player !== player)
|
|
90
|
+
return;
|
|
91
|
+
attempt(() => {
|
|
92
|
+
if (resize() && rendered)
|
|
93
|
+
player.renderFrame(player.getFrame());
|
|
94
|
+
}, current.current);
|
|
95
|
+
}
|
|
96
|
+
function dispose() {
|
|
97
|
+
if (controller.current?.player === player)
|
|
98
|
+
controller.current = null;
|
|
99
|
+
resizeObserver?.disconnect();
|
|
100
|
+
if (listenToWindow)
|
|
101
|
+
window.removeEventListener('resize', onResize);
|
|
102
|
+
player?.dispose();
|
|
103
|
+
renderer?.dispose();
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
renderer = createRenderer ? createRenderer(output) : new three_1.WebGLRenderer({ canvas: output, antialias: true });
|
|
107
|
+
player = new threejs_1.Vim3DPlayer(scene, {
|
|
108
|
+
renderer: renderer.getSize ? renderer : {
|
|
109
|
+
render: renderer.render.bind(renderer),
|
|
110
|
+
getSize: size => size.set(lastWidth, lastHeight),
|
|
111
|
+
},
|
|
112
|
+
fps: settings.fps, loop: settings.loop,
|
|
113
|
+
onFrame: snapshot => {
|
|
114
|
+
if (!rendered)
|
|
115
|
+
output.style.visibility = '';
|
|
116
|
+
rendered = true;
|
|
117
|
+
current.current.onFrame?.(snapshot);
|
|
118
|
+
},
|
|
119
|
+
onDiagnostics: diagnostics => current.current.onDiagnostics?.(diagnostics),
|
|
120
|
+
});
|
|
121
|
+
controller.current = {
|
|
122
|
+
player,
|
|
123
|
+
sync() {
|
|
124
|
+
const next = playback(current.current, scene);
|
|
125
|
+
if (next.fps !== settings.fps)
|
|
126
|
+
player.setFps(next.fps);
|
|
127
|
+
if (next.loop !== settings.loop)
|
|
128
|
+
player.setLoop(next.loop);
|
|
129
|
+
const resized = resize();
|
|
130
|
+
const recolored = next.background !== settings.background;
|
|
131
|
+
if (recolored)
|
|
132
|
+
background(next.background);
|
|
133
|
+
let seekSucceeded = false;
|
|
134
|
+
if (next.frame !== settings.frame && next.frame !== undefined) {
|
|
135
|
+
seekSucceeded = 'value' in player.seek(next.frame);
|
|
136
|
+
}
|
|
137
|
+
else if ((resized || recolored) && rendered)
|
|
138
|
+
player.renderFrame(player.getFrame());
|
|
139
|
+
if (next.playing && (seekSucceeded || !settings.playing))
|
|
140
|
+
player.play();
|
|
141
|
+
else if (!next.playing && settings.playing)
|
|
142
|
+
player.pause();
|
|
143
|
+
settings = next;
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
resize();
|
|
147
|
+
background(settings.background);
|
|
148
|
+
player.renderFrame(settings.frame ?? 0);
|
|
149
|
+
if (settings.playing && rendered)
|
|
150
|
+
player.play();
|
|
151
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
152
|
+
resizeObserver = new ResizeObserver(onResize);
|
|
153
|
+
resizeObserver.observe(host);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
window.addEventListener('resize', onResize);
|
|
157
|
+
listenToWindow = true;
|
|
158
|
+
}
|
|
159
|
+
current.current.onReady?.(player);
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
dispose();
|
|
163
|
+
attempt(() => { throw error; }, current.current);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
return dispose;
|
|
167
|
+
}, [source, compileOptions, createRenderer]);
|
|
168
|
+
(0, react_1.useEffect)(() => {
|
|
169
|
+
attempt(() => controller.current?.sync(), current.current);
|
|
170
|
+
});
|
|
171
|
+
return (0, react_1.createElement)('div', { ref: container, className, style: { width, height, ...style } }, (0, react_1.createElement)('canvas', { ref: canvas, style: { display: 'block', width: '100%', height: '100%' } }));
|
|
172
|
+
});
|
package/esm/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const name = "@vim3d/react";
|