@tioniq/sceniq 0.0.0-stage → 0.1.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.
@@ -0,0 +1,88 @@
1
+ "use client";
2
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
3
+
4
+ var _chunkKRCM4WYZcjs = require('../chunk-KRCM4WYZ.cjs');
5
+ require('../chunk-OCHCBJJE.cjs');
6
+
7
+ // src/react/index.ts
8
+
9
+
10
+
11
+
12
+
13
+ var _react = require('react');
14
+ function useScene(ref, setup, deps = [], options) {
15
+ const sceneRef = _react.useRef.call(void 0, null);
16
+ const setupRef = _react.useRef.call(void 0, setup);
17
+ setupRef.current = setup;
18
+ _react.useEffect.call(void 0, () => {
19
+ const canvas = ref.current;
20
+ const context = _optionalChain([canvas, 'optionalAccess', _ => _.getContext, 'call', _2 => _2("2d")]);
21
+ if (!context) {
22
+ return;
23
+ }
24
+ const scene = new (0, _chunkKRCM4WYZcjs.Scene)(context, options);
25
+ let result;
26
+ try {
27
+ result = setupRef.current(scene);
28
+ } catch (error) {
29
+ scene.dispose();
30
+ throw error;
31
+ }
32
+ sceneRef.current = scene;
33
+ return () => {
34
+ if (typeof result === "function") {
35
+ result();
36
+ } else if (result) {
37
+ result.dispose();
38
+ }
39
+ scene.dispose();
40
+ sceneRef.current = null;
41
+ };
42
+ }, [ref, ...deps]);
43
+ return sceneRef;
44
+ }
45
+ var defaultStyle = { display: "block", width: "100%", height: "100%" };
46
+ function SceneCanvas(props) {
47
+ const { setup, deps, options, sceneRef, canvasRef, style, tabIndex, ...rest } = props;
48
+ const ref = _react.useRef.call(void 0, null);
49
+ useScene(
50
+ ref,
51
+ (s) => {
52
+ const result = setup(s);
53
+ setRef(sceneRef, s);
54
+ return () => {
55
+ setRef(sceneRef, null);
56
+ if (typeof result === "function") {
57
+ result();
58
+ } else if (result) {
59
+ result.dispose();
60
+ }
61
+ };
62
+ },
63
+ _nullishCoalesce(deps, () => ( [])),
64
+ options
65
+ );
66
+ _react.useImperativeHandle.call(void 0, canvasRef, () => ref.current, []);
67
+ return _react.createElement.call(void 0, "canvas", {
68
+ ...rest,
69
+ ref,
70
+ tabIndex: _nullishCoalesce(tabIndex, () => ( 0)),
71
+ style: style ? { ...defaultStyle, ...style } : defaultStyle
72
+ });
73
+ }
74
+ function setRef(ref, value) {
75
+ if (!ref) {
76
+ return;
77
+ }
78
+ if (typeof ref === "function") {
79
+ ref(value);
80
+ return;
81
+ }
82
+ ;
83
+ ref.current = value;
84
+ }
85
+
86
+
87
+
88
+ exports.SceneCanvas = SceneCanvas; exports.useScene = useScene;
@@ -0,0 +1,64 @@
1
+ import { CanvasHTMLAttributes, Ref, ReactElement, RefObject } from 'react';
2
+ import { S as Scene, r as SceneOptions } from '../scene-Dd7ExNOl.cjs';
3
+ import '@tioniq/disposiq';
4
+ import '@tioniq/eventiq';
5
+ import '../rect-bounds-Bd7-7ERA.cjs';
6
+
7
+ /** What a setup callback may return: a cleanup function, a disposable, or nothing. */
8
+ type SceneSetupResult = void | (() => void) | {
9
+ dispose(): void;
10
+ };
11
+ /**
12
+ * Creates a {@link Scene} on the canvas `ref` points at once it is mounted, calls `setup` with it,
13
+ * and disposes both on unmount. `setup` runs again only when `deps` change.
14
+ *
15
+ * ```tsx
16
+ * function Chart() {
17
+ * const ref = useRef<HTMLCanvasElement>(null)
18
+ * useScene(ref, scene => {
19
+ * const layer = scene.createSurface().createLayer()
20
+ * layer.add(new TextView({ text: "Hello" }))
21
+ * })
22
+ * return <canvas ref={ref} style={{ width: "100%", height: 300 }} />
23
+ * }
24
+ * ```
25
+ */
26
+ declare function useScene(ref: RefObject<HTMLCanvasElement | null>, setup: (scene: Scene) => SceneSetupResult, deps?: ReadonlyArray<unknown>, options?: SceneOptions): RefObject<Scene | null>;
27
+ interface SceneCanvasProps extends Omit<CanvasHTMLAttributes<HTMLCanvasElement>, "children" | "ref"> {
28
+ /**
29
+ * Builds the drawing: called with a new scene once the canvas is mounted, and again (on a new scene)
30
+ * whenever `deps` change. What it returns is disposed with the scene.
31
+ */
32
+ readonly setup: (scene: Scene) => SceneSetupResult;
33
+ /** When to tear the scene down and set it up again. Default: never, for the life of the component. */
34
+ readonly deps?: ReadonlyArray<unknown>;
35
+ /** Options for the scene, read when it is created. */
36
+ readonly options?: SceneOptions;
37
+ /** Receives the scene while it exists (null before mount and after unmount). */
38
+ readonly sceneRef?: Ref<Scene | null>;
39
+ /** Receives the canvas element. */
40
+ readonly canvasRef?: Ref<HTMLCanvasElement | null>;
41
+ }
42
+ /**
43
+ * A `<canvas>` with a {@link Scene} on it, for React. The canvas fills its parent by default (give
44
+ * the parent a size, or pass `style`); the scene keeps its bitmap matched to the canvas's CSS size
45
+ * and the device pixel ratio by itself. The canvas is focusable (`tabIndex` 0), so keyboard controls
46
+ * such as `+`/`-` zoom work once it is clicked.
47
+ *
48
+ * ```tsx
49
+ * <SceneCanvas
50
+ * style={{ height: 400 }}
51
+ * setup={scene => {
52
+ * const surface = scene.createSurface()
53
+ * surface.createLayer().add(new TextView({ text: "Hello" }))
54
+ * return attachZoomPan(scene, surface)
55
+ * }}
56
+ * />
57
+ * ```
58
+ *
59
+ * Changing data does not need `deps`: bind views to eventiq variables and set the variables from
60
+ * React (for example in an effect), and only the layers that show them redraw.
61
+ */
62
+ declare function SceneCanvas(props: SceneCanvasProps): ReactElement;
63
+
64
+ export { SceneCanvas, type SceneCanvasProps, type SceneSetupResult, useScene };
@@ -0,0 +1,64 @@
1
+ import { CanvasHTMLAttributes, Ref, ReactElement, RefObject } from 'react';
2
+ import { S as Scene, r as SceneOptions } from '../scene-77s46CRy.js';
3
+ import '@tioniq/disposiq';
4
+ import '@tioniq/eventiq';
5
+ import '../rect-bounds-Bd7-7ERA.js';
6
+
7
+ /** What a setup callback may return: a cleanup function, a disposable, or nothing. */
8
+ type SceneSetupResult = void | (() => void) | {
9
+ dispose(): void;
10
+ };
11
+ /**
12
+ * Creates a {@link Scene} on the canvas `ref` points at once it is mounted, calls `setup` with it,
13
+ * and disposes both on unmount. `setup` runs again only when `deps` change.
14
+ *
15
+ * ```tsx
16
+ * function Chart() {
17
+ * const ref = useRef<HTMLCanvasElement>(null)
18
+ * useScene(ref, scene => {
19
+ * const layer = scene.createSurface().createLayer()
20
+ * layer.add(new TextView({ text: "Hello" }))
21
+ * })
22
+ * return <canvas ref={ref} style={{ width: "100%", height: 300 }} />
23
+ * }
24
+ * ```
25
+ */
26
+ declare function useScene(ref: RefObject<HTMLCanvasElement | null>, setup: (scene: Scene) => SceneSetupResult, deps?: ReadonlyArray<unknown>, options?: SceneOptions): RefObject<Scene | null>;
27
+ interface SceneCanvasProps extends Omit<CanvasHTMLAttributes<HTMLCanvasElement>, "children" | "ref"> {
28
+ /**
29
+ * Builds the drawing: called with a new scene once the canvas is mounted, and again (on a new scene)
30
+ * whenever `deps` change. What it returns is disposed with the scene.
31
+ */
32
+ readonly setup: (scene: Scene) => SceneSetupResult;
33
+ /** When to tear the scene down and set it up again. Default: never, for the life of the component. */
34
+ readonly deps?: ReadonlyArray<unknown>;
35
+ /** Options for the scene, read when it is created. */
36
+ readonly options?: SceneOptions;
37
+ /** Receives the scene while it exists (null before mount and after unmount). */
38
+ readonly sceneRef?: Ref<Scene | null>;
39
+ /** Receives the canvas element. */
40
+ readonly canvasRef?: Ref<HTMLCanvasElement | null>;
41
+ }
42
+ /**
43
+ * A `<canvas>` with a {@link Scene} on it, for React. The canvas fills its parent by default (give
44
+ * the parent a size, or pass `style`); the scene keeps its bitmap matched to the canvas's CSS size
45
+ * and the device pixel ratio by itself. The canvas is focusable (`tabIndex` 0), so keyboard controls
46
+ * such as `+`/`-` zoom work once it is clicked.
47
+ *
48
+ * ```tsx
49
+ * <SceneCanvas
50
+ * style={{ height: 400 }}
51
+ * setup={scene => {
52
+ * const surface = scene.createSurface()
53
+ * surface.createLayer().add(new TextView({ text: "Hello" }))
54
+ * return attachZoomPan(scene, surface)
55
+ * }}
56
+ * />
57
+ * ```
58
+ *
59
+ * Changing data does not need `deps`: bind views to eventiq variables and set the variables from
60
+ * React (for example in an effect), and only the layers that show them redraw.
61
+ */
62
+ declare function SceneCanvas(props: SceneCanvasProps): ReactElement;
63
+
64
+ export { SceneCanvas, type SceneCanvasProps, type SceneSetupResult, useScene };
@@ -0,0 +1,88 @@
1
+ "use client";
2
+ import {
3
+ Scene
4
+ } from "../chunk-BX7YQ44O.js";
5
+ import "../chunk-CVQBUUHT.js";
6
+
7
+ // src/react/index.ts
8
+ import {
9
+ createElement,
10
+ useEffect,
11
+ useImperativeHandle,
12
+ useRef
13
+ } from "react";
14
+ function useScene(ref, setup, deps = [], options) {
15
+ const sceneRef = useRef(null);
16
+ const setupRef = useRef(setup);
17
+ setupRef.current = setup;
18
+ useEffect(() => {
19
+ const canvas = ref.current;
20
+ const context = canvas?.getContext("2d");
21
+ if (!context) {
22
+ return;
23
+ }
24
+ const scene = new Scene(context, options);
25
+ let result;
26
+ try {
27
+ result = setupRef.current(scene);
28
+ } catch (error) {
29
+ scene.dispose();
30
+ throw error;
31
+ }
32
+ sceneRef.current = scene;
33
+ return () => {
34
+ if (typeof result === "function") {
35
+ result();
36
+ } else if (result) {
37
+ result.dispose();
38
+ }
39
+ scene.dispose();
40
+ sceneRef.current = null;
41
+ };
42
+ }, [ref, ...deps]);
43
+ return sceneRef;
44
+ }
45
+ var defaultStyle = { display: "block", width: "100%", height: "100%" };
46
+ function SceneCanvas(props) {
47
+ const { setup, deps, options, sceneRef, canvasRef, style, tabIndex, ...rest } = props;
48
+ const ref = useRef(null);
49
+ useScene(
50
+ ref,
51
+ (s) => {
52
+ const result = setup(s);
53
+ setRef(sceneRef, s);
54
+ return () => {
55
+ setRef(sceneRef, null);
56
+ if (typeof result === "function") {
57
+ result();
58
+ } else if (result) {
59
+ result.dispose();
60
+ }
61
+ };
62
+ },
63
+ deps ?? [],
64
+ options
65
+ );
66
+ useImperativeHandle(canvasRef, () => ref.current, []);
67
+ return createElement("canvas", {
68
+ ...rest,
69
+ ref,
70
+ tabIndex: tabIndex ?? 0,
71
+ style: style ? { ...defaultStyle, ...style } : defaultStyle
72
+ });
73
+ }
74
+ function setRef(ref, value) {
75
+ if (!ref) {
76
+ return;
77
+ }
78
+ if (typeof ref === "function") {
79
+ ref(value);
80
+ return;
81
+ }
82
+ ;
83
+ ref.current = value;
84
+ }
85
+ export {
86
+ SceneCanvas,
87
+ useScene
88
+ };