@sprite-motion/react 0.1.1
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/dist/index.cjs +147 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
SpriteAnimation: () => SpriteAnimation,
|
|
24
|
+
useSpriteAnimation: () => useSpriteAnimation
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/SpriteAnimation.tsx
|
|
29
|
+
var import_react2 = require("react");
|
|
30
|
+
|
|
31
|
+
// src/useSpriteAnimation.ts
|
|
32
|
+
var import_core = require("@sprite-motion/core");
|
|
33
|
+
var import_react = require("react");
|
|
34
|
+
function useSpriteAnimation(options) {
|
|
35
|
+
const ref = (0, import_react.useRef)(null);
|
|
36
|
+
const animatorRef = (0, import_react.useRef)(null);
|
|
37
|
+
const [state, setState] = (0, import_react.useState)({
|
|
38
|
+
currentFrame: 0,
|
|
39
|
+
totalFrames: options.rows * options.cols,
|
|
40
|
+
isPlaying: false,
|
|
41
|
+
isLoaded: false
|
|
42
|
+
});
|
|
43
|
+
const { enabled = true, ...animatorOptions } = options;
|
|
44
|
+
(0, import_react.useEffect)(() => {
|
|
45
|
+
if (!enabled) return;
|
|
46
|
+
const animator = new import_core.SpriteAnimator(animatorOptions);
|
|
47
|
+
animatorRef.current = animator;
|
|
48
|
+
const unsubscribe = animator.subscribe(setState);
|
|
49
|
+
queueMicrotask(() => {
|
|
50
|
+
if (ref.current) {
|
|
51
|
+
animator.attach(ref.current);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return () => {
|
|
55
|
+
unsubscribe();
|
|
56
|
+
animator.destroy();
|
|
57
|
+
animatorRef.current = null;
|
|
58
|
+
};
|
|
59
|
+
}, [enabled]);
|
|
60
|
+
(0, import_react.useEffect)(() => {
|
|
61
|
+
animatorRef.current?.updateOptions(animatorOptions);
|
|
62
|
+
}, [
|
|
63
|
+
animatorOptions.src,
|
|
64
|
+
animatorOptions.rows,
|
|
65
|
+
animatorOptions.cols,
|
|
66
|
+
animatorOptions.fps,
|
|
67
|
+
animatorOptions.loop,
|
|
68
|
+
animatorOptions.width,
|
|
69
|
+
animatorOptions.height,
|
|
70
|
+
animatorOptions.autoPlay,
|
|
71
|
+
animatorOptions.renderer
|
|
72
|
+
]);
|
|
73
|
+
return {
|
|
74
|
+
ref,
|
|
75
|
+
state,
|
|
76
|
+
play: () => animatorRef.current?.play(),
|
|
77
|
+
pause: () => animatorRef.current?.pause(),
|
|
78
|
+
stop: () => animatorRef.current?.stop(),
|
|
79
|
+
goToFrame: (frame) => animatorRef.current?.goToFrame(frame)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/SpriteAnimation.tsx
|
|
84
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
85
|
+
var SpriteAnimation = (0, import_react2.forwardRef)(
|
|
86
|
+
function SpriteAnimation2({
|
|
87
|
+
src,
|
|
88
|
+
rows,
|
|
89
|
+
cols,
|
|
90
|
+
fps = 12,
|
|
91
|
+
loop = true,
|
|
92
|
+
width = 128,
|
|
93
|
+
height = 128,
|
|
94
|
+
autoPlay = true,
|
|
95
|
+
renderer = "css",
|
|
96
|
+
onComplete,
|
|
97
|
+
onFrameChange,
|
|
98
|
+
className,
|
|
99
|
+
style
|
|
100
|
+
}, ref) {
|
|
101
|
+
const { ref: targetRef, play, pause, stop, goToFrame } = useSpriteAnimation({
|
|
102
|
+
src,
|
|
103
|
+
rows,
|
|
104
|
+
cols,
|
|
105
|
+
fps,
|
|
106
|
+
loop,
|
|
107
|
+
width,
|
|
108
|
+
height,
|
|
109
|
+
autoPlay,
|
|
110
|
+
renderer,
|
|
111
|
+
onComplete,
|
|
112
|
+
onFrameChange
|
|
113
|
+
});
|
|
114
|
+
(0, import_react2.useImperativeHandle)(ref, () => ({ play, pause, stop, goToFrame }), [
|
|
115
|
+
play,
|
|
116
|
+
pause,
|
|
117
|
+
stop,
|
|
118
|
+
goToFrame
|
|
119
|
+
]);
|
|
120
|
+
if (renderer === "canvas") {
|
|
121
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
122
|
+
"canvas",
|
|
123
|
+
{
|
|
124
|
+
ref: targetRef,
|
|
125
|
+
className,
|
|
126
|
+
style
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
131
|
+
"div",
|
|
132
|
+
{
|
|
133
|
+
ref: targetRef,
|
|
134
|
+
className,
|
|
135
|
+
style,
|
|
136
|
+
role: "img",
|
|
137
|
+
"aria-label": "Sprite animation"
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
143
|
+
0 && (module.exports = {
|
|
144
|
+
SpriteAnimation,
|
|
145
|
+
useSpriteAnimation
|
|
146
|
+
});
|
|
147
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/SpriteAnimation.tsx","../src/useSpriteAnimation.ts"],"sourcesContent":["export { SpriteAnimation } from './SpriteAnimation.js';\nexport type { SpriteAnimationHandle, SpriteAnimationProps } from './SpriteAnimation.js';\nexport { useSpriteAnimation } from './useSpriteAnimation.js';\nexport type { UseSpriteAnimationOptions, UseSpriteAnimationReturn } from './useSpriteAnimation.js';\n","import type { SpriteAnimationOptions } from '@sprite-motion/core';\nimport { forwardRef, useImperativeHandle } from 'react';\nimport { useSpriteAnimation } from './useSpriteAnimation.js';\n\nexport interface SpriteAnimationProps extends SpriteAnimationOptions {\n className?: string;\n style?: React.CSSProperties;\n}\n\nexport interface SpriteAnimationHandle {\n play: () => void;\n pause: () => void;\n stop: () => void;\n goToFrame: (frame: number) => void;\n}\n\nexport const SpriteAnimation = forwardRef<SpriteAnimationHandle, SpriteAnimationProps>(\n function SpriteAnimation(\n {\n src,\n rows,\n cols,\n fps = 12,\n loop = true,\n width = 128,\n height = 128,\n autoPlay = true,\n renderer = 'css',\n onComplete,\n onFrameChange,\n className,\n style,\n },\n ref,\n ) {\n const { ref: targetRef, play, pause, stop, goToFrame } = useSpriteAnimation({\n src,\n rows,\n cols,\n fps,\n loop,\n width,\n height,\n autoPlay,\n renderer,\n onComplete,\n onFrameChange,\n });\n\n useImperativeHandle(ref, () => ({ play, pause, stop, goToFrame }), [\n play,\n pause,\n stop,\n goToFrame,\n ]);\n\n if (renderer === 'canvas') {\n return (\n <canvas\n ref={targetRef as React.RefObject<HTMLCanvasElement>}\n className={className}\n style={style}\n />\n );\n }\n\n return (\n <div\n ref={targetRef as React.RefObject<HTMLDivElement>}\n className={className}\n style={style}\n role=\"img\"\n aria-label=\"Sprite animation\"\n />\n );\n },\n);\n","import { SpriteAnimator, type SpriteAnimationOptions, type SpriteAnimationState } from '@sprite-motion/core';\nimport { useEffect, useRef, useState } from 'react';\n\nexport interface UseSpriteAnimationOptions extends SpriteAnimationOptions {\n /** Skip auto-attach; useful when controlling the target manually */\n enabled?: boolean;\n}\n\nexport interface UseSpriteAnimationReturn {\n ref: React.RefObject<HTMLElement | HTMLCanvasElement | null>;\n state: SpriteAnimationState;\n play: () => void;\n pause: () => void;\n stop: () => void;\n goToFrame: (frame: number) => void;\n}\n\nexport function useSpriteAnimation(options: UseSpriteAnimationOptions): UseSpriteAnimationReturn {\n const ref = useRef<HTMLElement | HTMLCanvasElement | null>(null);\n const animatorRef = useRef<SpriteAnimator | null>(null);\n const [state, setState] = useState<SpriteAnimationState>({\n currentFrame: 0,\n totalFrames: options.rows * options.cols,\n isPlaying: false,\n isLoaded: false,\n });\n\n const { enabled = true, ...animatorOptions } = options;\n\n useEffect(() => {\n if (!enabled) return;\n\n const animator = new SpriteAnimator(animatorOptions);\n animatorRef.current = animator;\n\n const unsubscribe = animator.subscribe(setState);\n\n queueMicrotask(() => {\n if (ref.current) {\n animator.attach(ref.current);\n }\n });\n\n return () => {\n unsubscribe();\n animator.destroy();\n animatorRef.current = null;\n };\n }, [enabled]);\n\n useEffect(() => {\n animatorRef.current?.updateOptions(animatorOptions);\n }, [\n animatorOptions.src,\n animatorOptions.rows,\n animatorOptions.cols,\n animatorOptions.fps,\n animatorOptions.loop,\n animatorOptions.width,\n animatorOptions.height,\n animatorOptions.autoPlay,\n animatorOptions.renderer,\n ]);\n\n return {\n ref,\n state,\n play: () => animatorRef.current?.play(),\n pause: () => animatorRef.current?.pause(),\n stop: () => animatorRef.current?.stop(),\n goToFrame: (frame) => animat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S,KAAK;AAAA,IACtC,WAAW,CAAC,UAAU,YAAY,SAAS,UAAU,KAAK;AAAA,EAC5D;AACF;;;ADdQ;AA1CD,IAAM,sBAAkB;AAAA,EAC7B,SAASC,iBACP;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,WAAW;AAAA,IACX,WAAW;AAAA,IACX;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GACA,KACA;AACA,UAAM,EAAE,KAAK,WAAW,MAAM,OAAO,MAAM,UAAU,IAAI,mBAAmB;AAAA,MAC1E;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,2CAAoB,KAAK,OAAO,EAAE,MAAM,OAAO,MAAM,UAAU,IAAI;AAAA,MACjE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,QAAI,aAAa,UAAU;AACzB,aACE;AAAA,QAAC;AAAA;AAAA,UACC,KAAK;AAAA,UACL;AAAA,UACA;AAAA;AAAA,MACF;AAAA,IAEJ;AAEA,WACE;AAAA,MAAC;AAAA;AAAA,QACC,KAAK;AAAA,QACL;AAAA,QACA;AAAA,QACA,MAAK;AAAA,QACL,cAAW;AAAA;AAAA,IACb;AAAA,EAEJ;AACF;","names":["import_react","SpriteAnimation"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { SpriteAnimationOptions, SpriteAnimationState } from '@sprite-motion/core';
|
|
3
|
+
|
|
4
|
+
interface SpriteAnimationProps extends SpriteAnimationOptions {
|
|
5
|
+
className?: string;
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
}
|
|
8
|
+
interface SpriteAnimationHandle {
|
|
9
|
+
play: () => void;
|
|
10
|
+
pause: () => void;
|
|
11
|
+
stop: () => void;
|
|
12
|
+
goToFrame: (frame: number) => void;
|
|
13
|
+
}
|
|
14
|
+
declare const SpriteAnimation: react.ForwardRefExoticComponent<SpriteAnimationProps & react.RefAttributes<SpriteAnimationHandle>>;
|
|
15
|
+
|
|
16
|
+
interface UseSpriteAnimationOptions extends SpriteAnimationOptions {
|
|
17
|
+
/** Skip auto-attach; useful when controlling the target manually */
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface UseSpriteAnimationReturn {
|
|
21
|
+
ref: React.RefObject<HTMLElement | HTMLCanvasElement | null>;
|
|
22
|
+
state: SpriteAnimationState;
|
|
23
|
+
play: () => void;
|
|
24
|
+
pause: () => void;
|
|
25
|
+
stop: () => void;
|
|
26
|
+
goToFrame: (frame: number) => void;
|
|
27
|
+
}
|
|
28
|
+
declare function useSpriteAnimation(options: UseSpriteAnimationOptions): UseSpriteAnimationReturn;
|
|
29
|
+
|
|
30
|
+
export { SpriteAnimation, type SpriteAnimationHandle, type SpriteAnimationProps, type UseSpriteAnimationOptions, type UseSpriteAnimationReturn, useSpriteAnimation };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { SpriteAnimationOptions, SpriteAnimationState } from '@sprite-motion/core';
|
|
3
|
+
|
|
4
|
+
interface SpriteAnimationProps extends SpriteAnimationOptions {
|
|
5
|
+
className?: string;
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
}
|
|
8
|
+
interface SpriteAnimationHandle {
|
|
9
|
+
play: () => void;
|
|
10
|
+
pause: () => void;
|
|
11
|
+
stop: () => void;
|
|
12
|
+
goToFrame: (frame: number) => void;
|
|
13
|
+
}
|
|
14
|
+
declare const SpriteAnimation: react.ForwardRefExoticComponent<SpriteAnimationProps & react.RefAttributes<SpriteAnimationHandle>>;
|
|
15
|
+
|
|
16
|
+
interface UseSpriteAnimationOptions extends SpriteAnimationOptions {
|
|
17
|
+
/** Skip auto-attach; useful when controlling the target manually */
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface UseSpriteAnimationReturn {
|
|
21
|
+
ref: React.RefObject<HTMLElement | HTMLCanvasElement | null>;
|
|
22
|
+
state: SpriteAnimationState;
|
|
23
|
+
play: () => void;
|
|
24
|
+
pause: () => void;
|
|
25
|
+
stop: () => void;
|
|
26
|
+
goToFrame: (frame: number) => void;
|
|
27
|
+
}
|
|
28
|
+
declare function useSpriteAnimation(options: UseSpriteAnimationOptions): UseSpriteAnimationReturn;
|
|
29
|
+
|
|
30
|
+
export { SpriteAnimation, type SpriteAnimationHandle, type SpriteAnimationProps, type UseSpriteAnimationOptions, type UseSpriteAnimationReturn, useSpriteAnimation };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// src/SpriteAnimation.tsx
|
|
2
|
+
import { forwardRef, useImperativeHandle } from "react";
|
|
3
|
+
|
|
4
|
+
// src/useSpriteAnimation.ts
|
|
5
|
+
import { SpriteAnimator } from "@sprite-motion/core";
|
|
6
|
+
import { useEffect, useRef, useState } from "react";
|
|
7
|
+
function useSpriteAnimation(options) {
|
|
8
|
+
const ref = useRef(null);
|
|
9
|
+
const animatorRef = useRef(null);
|
|
10
|
+
const [state, setState] = useState({
|
|
11
|
+
currentFrame: 0,
|
|
12
|
+
totalFrames: options.rows * options.cols,
|
|
13
|
+
isPlaying: false,
|
|
14
|
+
isLoaded: false
|
|
15
|
+
});
|
|
16
|
+
const { enabled = true, ...animatorOptions } = options;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!enabled) return;
|
|
19
|
+
const animator = new SpriteAnimator(animatorOptions);
|
|
20
|
+
animatorRef.current = animator;
|
|
21
|
+
const unsubscribe = animator.subscribe(setState);
|
|
22
|
+
queueMicrotask(() => {
|
|
23
|
+
if (ref.current) {
|
|
24
|
+
animator.attach(ref.current);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return () => {
|
|
28
|
+
unsubscribe();
|
|
29
|
+
animator.destroy();
|
|
30
|
+
animatorRef.current = null;
|
|
31
|
+
};
|
|
32
|
+
}, [enabled]);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
animatorRef.current?.updateOptions(animatorOptions);
|
|
35
|
+
}, [
|
|
36
|
+
animatorOptions.src,
|
|
37
|
+
animatorOptions.rows,
|
|
38
|
+
animatorOptions.cols,
|
|
39
|
+
animatorOptions.fps,
|
|
40
|
+
animatorOptions.loop,
|
|
41
|
+
animatorOptions.width,
|
|
42
|
+
animatorOptions.height,
|
|
43
|
+
animatorOptions.autoPlay,
|
|
44
|
+
animatorOptions.renderer
|
|
45
|
+
]);
|
|
46
|
+
return {
|
|
47
|
+
ref,
|
|
48
|
+
state,
|
|
49
|
+
play: () => animatorRef.current?.play(),
|
|
50
|
+
pause: () => animatorRef.current?.pause(),
|
|
51
|
+
stop: () => animatorRef.current?.stop(),
|
|
52
|
+
goToFrame: (frame) => animatorRef.current?.goToFrame(frame)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/SpriteAnimation.tsx
|
|
57
|
+
import { jsx } from "react/jsx-runtime";
|
|
58
|
+
var SpriteAnimation = forwardRef(
|
|
59
|
+
function SpriteAnimation2({
|
|
60
|
+
src,
|
|
61
|
+
rows,
|
|
62
|
+
cols,
|
|
63
|
+
fps = 12,
|
|
64
|
+
loop = true,
|
|
65
|
+
width = 128,
|
|
66
|
+
height = 128,
|
|
67
|
+
autoPlay = true,
|
|
68
|
+
renderer = "css",
|
|
69
|
+
onComplete,
|
|
70
|
+
onFrameChange,
|
|
71
|
+
className,
|
|
72
|
+
style
|
|
73
|
+
}, ref) {
|
|
74
|
+
const { ref: targetRef, play, pause, stop, goToFrame } = useSpriteAnimation({
|
|
75
|
+
src,
|
|
76
|
+
rows,
|
|
77
|
+
cols,
|
|
78
|
+
fps,
|
|
79
|
+
loop,
|
|
80
|
+
width,
|
|
81
|
+
height,
|
|
82
|
+
autoPlay,
|
|
83
|
+
renderer,
|
|
84
|
+
onComplete,
|
|
85
|
+
onFrameChange
|
|
86
|
+
});
|
|
87
|
+
useImperativeHandle(ref, () => ({ play, pause, stop, goToFrame }), [
|
|
88
|
+
play,
|
|
89
|
+
pause,
|
|
90
|
+
stop,
|
|
91
|
+
goToFrame
|
|
92
|
+
]);
|
|
93
|
+
if (renderer === "canvas") {
|
|
94
|
+
return /* @__PURE__ */ jsx(
|
|
95
|
+
"canvas",
|
|
96
|
+
{
|
|
97
|
+
ref: targetRef,
|
|
98
|
+
className,
|
|
99
|
+
style
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return /* @__PURE__ */ jsx(
|
|
104
|
+
"div",
|
|
105
|
+
{
|
|
106
|
+
ref: targetRef,
|
|
107
|
+
className,
|
|
108
|
+
style,
|
|
109
|
+
role: "img",
|
|
110
|
+
"aria-label": "Sprite animation"
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
export {
|
|
116
|
+
SpriteAnimation,
|
|
117
|
+
useSpriteAnimation
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/SpriteAnimation.tsx","../src/useSpriteAnimation.ts"],"sourcesContent":["import type { SpriteAnimationOptions } from '@sprite-motion/core';\nimport { forwardRef, useImperativeHandle } from 'react';\nimport { useSpriteAnimation } from './useSpriteAnimation.js';\n\nexport interface SpriteAnimationProps extends SpriteAnimationOptions {\n className?: string;\n style?: React.CSSProperties;\n}\n\nexport interface SpriteAnimationHandle {\n play: () => void;\n pause: () => void;\n stop: () => void;\n goToFrame: (frame: number) => void;\n}\n\nexport const SpriteAnimation = forwardRef<SpriteAnimationHandle, SpriteAnimationProps>(\n function SpriteAnimation(\n {\n src,\n rows,\n cols,\n fps = 12,\n loop = true,\n width = 128,\n height = 128,\n autoPlay = true,\n renderer = 'css',\n onComplete,\n onFrameChange,\n className,\n style,\n },\n ref,\n ) {\n const { ref: targetRef, play, pause, stop, goToFrame } = useSpriteAnimation({\n src,\n rows,\n cols,\n fps,\n loop,\n width,\n height,\n autoPlay,\n renderer,\n onComplete,\n onFrameChange,\n });\n\n useImperativeHandle(ref, () => ({ play, pause, stop, goToFrame }), [\n play,\n pause,\n stop,\n goToFrame,\n ]);\n\n if (renderer === 'canvas') {\n return (\n <canvas\n ref={targetRef as React.RefObject<HTMLCanvasElement>}\n className={className}\n style={style}\n />\n );\n }\n\n return (\n <div\n ref={targetRef as React.RefObject<HTMLDivElement>}\n className={className}\n style={style}\n role=\"img\"\n aria-label=\"Sprite animation\"\n />\n );\n },\n);\n","import { SpriteAnimator, type SpriteAnimationOptions, type SpriteAnimationState } from '@sprite-motion/core';\nimport { useEffect, useRef, useState } from 'react';\n\nexport interface UseSpriteAnimationOptions extends SpriteAnimationOptions {\n /** Skip auto-attach; useful when controlling the target manually */\n enabled?: boolean;\n}\n\nexport interface UseSpriteAnimationReturn {\n ref: React.RefObject<HTMLElement | HTMLCanvasElement | null>;\n state: SpriteAnimationState;\n play: () => void;\n pause: () => void;\n stop: () => void;\n goToFrame: (frame: number) => void;\n}\n\nexport function useSpriteAnimation(options: UseSpriteAnimationOptions): UseSpriteAnimationReturn {\n const ref = useRef<HTMLElement | HTMLCanvasElement | null>(null);\n const animatorRef = useRef<SpriteAnimator | null>(null);\n const [state, setState] = useState<SpriteAnimationState>({\n currentFrame: 0,\n totalFrames: options.rows * options.cols,\n isPlaying: false,\n isLoaded: false,\n });\n\n const { enabled = true, ...animatorOptions } = options;\n\n useEffect(() => {\n if (!enabled) return;\n\n const animator = new SpriteAnimator(animatorOptions);\n animatorRef.current = animator;\n\n const unsubscribe = animator.subscribe(setState);\n\n queueMicrotask(() => {\n if (ref.current) {\n animator.attach(ref.current);\n }\n });\n\n return () => {\n unsubscribe();\n animator.destroy();\n animatorRef.current = null;\n };\n }, [enabled]);\n\n useEffect(() => {\n animatorRef.current?.updateOptions(animatorOptions);\n }, [\n animatorOptions.src,\n animatorOptions.rows,\n animatorOptions.cols,\n animatorOptions.fps,\n animatorOptions.loop,\n animatorOptions.width,\n animatorOptions.height,\n animatorOptions.autoPlay,\n animatorOptions.renderer,\n ]);\n\n return {\n ref,\n state,\n play: () => animatorRef.current?.play(),\n pause: () => animatorRef.current?.pause(),\n stop: () => animatorRef.current?.stop(),\n goToFrame: (frame) => animatorRef.current?.goToFrame(frame),\n };\n}\n"],"mappings":";AACA,SAAS,YAAY,2BAA2B;;;ACDhD,SAAS,sBAA8E;AACvF,SAAS,WAAW,QAAQ,gBAAgB;AAgBrC,SAAS,mBAAmB,SAA8D;AAC/F,QAAM,MAAM,OAA+C,IAAI;AAC/D,QAAM,cAAc,OAA8B,IAAI;AACtD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA+B;AAAA,IACvD,cAAc;AAAA,IACd,aAAa,QAAQ,OAAO,QAAQ;AAAA,IACpC,WAAW;AAAA,IACX,UAAU;AAAA,EACZ,CAAC;AAED,QAAM,EAAE,UAAU,MAAM,GAAG,gBAAgB,IAAI;AAE/C,YAAU,MAAM;AACd,QAAI,CAAC,QAAS;AAEd,UAAM,WAAW,IAAI,eAAe,eAAe;AACnD,gBAAY,UAAU;AAEtB,UAAM,cAAc,SAAS,UAAU,QAAQ;AAE/C,mBAAe,MAAM;AACnB,UAAI,IAAI,SAAS;AACf,iBAAS,OAAO,IAAI,OAAO;AAAA,MAC7B;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,kBAAY;AACZ,eAAS,QAAQ;AACjB,kBAAY,UAAU;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,YAAU,MAAM;AACd,gBAAY,SAAS,cAAc,eAAe;AAAA,EACpD,GAAG;AAAA,IACD,gBAAgB;AAAA,IAChB,gBAAgB;AAAA,IAChB,gBAAgB;AA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package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sprite-motion/react",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "React component for sprite sheet animation",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"dev": "tsup --watch"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"react": ">=17",
|
|
31
|
+
"react-dom": ">=17"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@sprite-motion/core": "workspace:*"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "^19.1.6",
|
|
38
|
+
"@types/react-dom": "^19.1.5",
|
|
39
|
+
"react": "^19.1.0",
|
|
40
|
+
"react-dom": "^19.1.0",
|
|
41
|
+
"tsup": "^8.5.0"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"sprite",
|
|
48
|
+
"animation",
|
|
49
|
+
"react",
|
|
50
|
+
"spritesheet"
|
|
51
|
+
],
|
|
52
|
+
"license": "MIT"
|
|
53
|
+
}
|