mason-sprite 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.
- package/LICENSE +21 -0
- package/README.md +134 -0
- package/dist/index.cjs +273 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +242 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +353 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +61 -0
- package/dist/react/index.d.ts +61 -0
- package/dist/react/index.js +325 -0
- package/dist/react/index.js.map +1 -0
- package/dist/svelte/Sprite.svelte +88 -0
- package/dist/svelte/Sprite.svelte.d.ts +14 -0
- package/dist/svelte/Sprite.svelte.d.ts.map +1 -0
- package/dist/svelte/index.d.ts +2 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +1 -0
- package/dist/vue/Sprite.vue.d.ts +28 -0
- package/dist/vue/Sprite.vue.d.ts.map +1 -0
- package/dist/vue/index.cjs +1 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/index.js +205 -0
- package/package.json +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mason
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# mason-sprite
|
|
2
|
+
|
|
3
|
+
Lightweight sprite sheet animation for **React**, **Vue**, and **Svelte** — one package, subpath imports.
|
|
4
|
+
|
|
5
|
+
Drop in a PNG or WebP sprite sheet, set `rows`, `cols`, and `fps` — and you're done. No Lottie, no timeline editor. Just a simple **CSS** or **Canvas** sprite player.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install mason-sprite
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Peer dependencies (install only what you use):
|
|
14
|
+
|
|
15
|
+
| Framework | Peers |
|
|
16
|
+
|-----------|-------|
|
|
17
|
+
| React | `react`, `react-dom` |
|
|
18
|
+
| Vue 3 | `vue` |
|
|
19
|
+
| Svelte | `svelte` |
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Core engine (vanilla JS)
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { SpriteAnimator } from 'mason-sprite';
|
|
27
|
+
|
|
28
|
+
const animator = new SpriteAnimator({
|
|
29
|
+
src: '/sprites/hero.png',
|
|
30
|
+
rows: 2,
|
|
31
|
+
cols: 5,
|
|
32
|
+
fps: 10,
|
|
33
|
+
loop: true,
|
|
34
|
+
width: 140,
|
|
35
|
+
height: 140,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
animator.attach(document.getElementById('sprite')!);
|
|
39
|
+
animator.play();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### React
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { Sprite } from 'mason-sprite/react';
|
|
46
|
+
|
|
47
|
+
<Sprite
|
|
48
|
+
src="/sprites/hero.png"
|
|
49
|
+
rows={2}
|
|
50
|
+
cols={5}
|
|
51
|
+
fps={10}
|
|
52
|
+
loop
|
|
53
|
+
width={140}
|
|
54
|
+
height={140}
|
|
55
|
+
/>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Vue 3
|
|
59
|
+
|
|
60
|
+
```vue
|
|
61
|
+
<script setup>
|
|
62
|
+
import { Sprite } from 'mason-sprite/vue';
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<template>
|
|
66
|
+
<Sprite
|
|
67
|
+
src="/sprites/hero.png"
|
|
68
|
+
:rows="2"
|
|
69
|
+
:cols="5"
|
|
70
|
+
:fps="10"
|
|
71
|
+
:loop="true"
|
|
72
|
+
:width="140"
|
|
73
|
+
:height="140"
|
|
74
|
+
/>
|
|
75
|
+
</template>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Svelte
|
|
79
|
+
|
|
80
|
+
```svelte
|
|
81
|
+
<script>
|
|
82
|
+
import { Sprite } from 'mason-sprite/svelte';
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<Sprite
|
|
86
|
+
src="/sprites/hero.png"
|
|
87
|
+
rows={2}
|
|
88
|
+
cols={5}
|
|
89
|
+
fps={10}
|
|
90
|
+
loop
|
|
91
|
+
width={140}
|
|
92
|
+
height={140}
|
|
93
|
+
/>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Exports
|
|
97
|
+
|
|
98
|
+
| Import path | Contents |
|
|
99
|
+
|-------------|----------|
|
|
100
|
+
| `mason-sprite` | `SpriteAnimator`, types, utilities |
|
|
101
|
+
| `mason-sprite/react` | `Sprite`, `useSprite` |
|
|
102
|
+
| `mason-sprite/vue` | `Sprite` component |
|
|
103
|
+
| `mason-sprite/svelte` | `Sprite` component |
|
|
104
|
+
|
|
105
|
+
## Features
|
|
106
|
+
|
|
107
|
+
- PNG / WebP sprite sheet support
|
|
108
|
+
- CSS or Canvas rendering
|
|
109
|
+
- `play`, `pause`, `stop`, `goToFrame` controls
|
|
110
|
+
- Works with any uniform grid sprite sheet (`rows × cols`)
|
|
111
|
+
|
|
112
|
+
## Sprite Sheet Requirements
|
|
113
|
+
|
|
114
|
+
- Uniform grid — every frame is the same size
|
|
115
|
+
- PNG or WebP format
|
|
116
|
+
- `rows × cols` = total frame count
|
|
117
|
+
|
|
118
|
+
## Development
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pnpm install
|
|
122
|
+
pnpm build
|
|
123
|
+
pnpm typecheck
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Publish
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm publish
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
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/core/index.ts
|
|
21
|
+
var core_exports = {};
|
|
22
|
+
__export(core_exports, {
|
|
23
|
+
SPRITE_ANIMATION_DEFAULTS: () => SPRITE_ANIMATION_DEFAULTS,
|
|
24
|
+
SpriteAnimator: () => SpriteAnimator,
|
|
25
|
+
getBackgroundPositionPercent: () => getBackgroundPositionPercent,
|
|
26
|
+
getFramePosition: () => getFramePosition,
|
|
27
|
+
getTotalFrames: () => getTotalFrames
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(core_exports);
|
|
30
|
+
|
|
31
|
+
// src/core/constants.ts
|
|
32
|
+
var SPRITE_ANIMATION_DEFAULTS = {
|
|
33
|
+
fps: 12,
|
|
34
|
+
loop: true,
|
|
35
|
+
width: 128,
|
|
36
|
+
height: 128,
|
|
37
|
+
autoPlay: true,
|
|
38
|
+
renderer: "css"
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/core/utils.ts
|
|
42
|
+
function getTotalFrames(rows, cols) {
|
|
43
|
+
return rows * cols;
|
|
44
|
+
}
|
|
45
|
+
function getFramePosition(frameIndex, cols) {
|
|
46
|
+
return {
|
|
47
|
+
row: Math.floor(frameIndex / cols),
|
|
48
|
+
col: frameIndex % cols
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function getBackgroundPositionPercent(frameIndex, rows, cols) {
|
|
52
|
+
const { row, col } = getFramePosition(frameIndex, cols);
|
|
53
|
+
const x = cols <= 1 ? 0 : col / (cols - 1) * 100;
|
|
54
|
+
const y = rows <= 1 ? 0 : row / (rows - 1) * 100;
|
|
55
|
+
return { x, y };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/core/canvas-renderer.ts
|
|
59
|
+
function drawCanvasFrame(canvas, image, frameIndex, rows, cols, width, height) {
|
|
60
|
+
const ctx = canvas.getContext("2d");
|
|
61
|
+
if (!ctx) return;
|
|
62
|
+
const frameWidth = image.naturalWidth / cols;
|
|
63
|
+
const frameHeight = image.naturalHeight / rows;
|
|
64
|
+
const { row, col } = getFramePosition(frameIndex, cols);
|
|
65
|
+
canvas.width = width;
|
|
66
|
+
canvas.height = height;
|
|
67
|
+
ctx.clearRect(0, 0, width, height);
|
|
68
|
+
ctx.drawImage(
|
|
69
|
+
image,
|
|
70
|
+
col * frameWidth,
|
|
71
|
+
row * frameHeight,
|
|
72
|
+
frameWidth,
|
|
73
|
+
frameHeight,
|
|
74
|
+
0,
|
|
75
|
+
0,
|
|
76
|
+
width,
|
|
77
|
+
height
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/core/css-renderer.ts
|
|
82
|
+
function applyCssFrame(target, src, frameIndex, rows, cols, width, height) {
|
|
83
|
+
const { x, y } = getBackgroundPositionPercent(frameIndex, rows, cols);
|
|
84
|
+
target.style.backgroundImage = `url("${src}")`;
|
|
85
|
+
target.style.backgroundRepeat = "no-repeat";
|
|
86
|
+
target.style.backgroundSize = `${cols * 100}% ${rows * 100}%`;
|
|
87
|
+
target.style.backgroundPosition = `${x}% ${y}%`;
|
|
88
|
+
target.style.width = `${width}px`;
|
|
89
|
+
target.style.height = `${height}px`;
|
|
90
|
+
target.style.display = "inline-block";
|
|
91
|
+
}
|
|
92
|
+
function resetCssRenderer(target) {
|
|
93
|
+
target.style.backgroundImage = "";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/core/sprite-animator.ts
|
|
97
|
+
var SpriteAnimator = class {
|
|
98
|
+
constructor(options) {
|
|
99
|
+
this.currentFrame = 0;
|
|
100
|
+
this.isPlaying = false;
|
|
101
|
+
this.isLoaded = false;
|
|
102
|
+
this.rafId = null;
|
|
103
|
+
this.lastTimestamp = 0;
|
|
104
|
+
this.accumulatedTime = 0;
|
|
105
|
+
this.image = null;
|
|
106
|
+
this.target = null;
|
|
107
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
108
|
+
this.destroyed = false;
|
|
109
|
+
this.tick = (timestamp) => {
|
|
110
|
+
if (!this.isPlaying || this.destroyed) return;
|
|
111
|
+
if (this.lastTimestamp === 0) {
|
|
112
|
+
this.lastTimestamp = timestamp;
|
|
113
|
+
}
|
|
114
|
+
const delta = timestamp - this.lastTimestamp;
|
|
115
|
+
this.lastTimestamp = timestamp;
|
|
116
|
+
this.accumulatedTime += delta;
|
|
117
|
+
const frameDuration = 1e3 / this.options.fps;
|
|
118
|
+
while (this.accumulatedTime >= frameDuration) {
|
|
119
|
+
this.accumulatedTime -= frameDuration;
|
|
120
|
+
this.advanceFrame();
|
|
121
|
+
}
|
|
122
|
+
this.rafId = requestAnimationFrame(this.tick);
|
|
123
|
+
};
|
|
124
|
+
this.options = {
|
|
125
|
+
...SPRITE_ANIMATION_DEFAULTS,
|
|
126
|
+
...options
|
|
127
|
+
};
|
|
128
|
+
this.loadImage();
|
|
129
|
+
}
|
|
130
|
+
attach(target) {
|
|
131
|
+
this.target = target;
|
|
132
|
+
if (this.isLoaded) {
|
|
133
|
+
this.render();
|
|
134
|
+
}
|
|
135
|
+
if (this.options.autoPlay) {
|
|
136
|
+
this.play();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
play() {
|
|
140
|
+
if (this.destroyed || this.isPlaying) return;
|
|
141
|
+
this.isPlaying = true;
|
|
142
|
+
this.lastTimestamp = 0;
|
|
143
|
+
this.accumulatedTime = 0;
|
|
144
|
+
this.rafId = requestAnimationFrame(this.tick);
|
|
145
|
+
this.notify();
|
|
146
|
+
}
|
|
147
|
+
pause() {
|
|
148
|
+
if (!this.isPlaying) return;
|
|
149
|
+
this.isPlaying = false;
|
|
150
|
+
if (this.rafId !== null) {
|
|
151
|
+
cancelAnimationFrame(this.rafId);
|
|
152
|
+
this.rafId = null;
|
|
153
|
+
}
|
|
154
|
+
this.notify();
|
|
155
|
+
}
|
|
156
|
+
stop() {
|
|
157
|
+
this.pause();
|
|
158
|
+
this.currentFrame = 0;
|
|
159
|
+
this.render();
|
|
160
|
+
this.notify();
|
|
161
|
+
}
|
|
162
|
+
goToFrame(frame) {
|
|
163
|
+
const total = this.getTotalFrames();
|
|
164
|
+
this.currentFrame = Math.max(0, Math.min(frame, total - 1));
|
|
165
|
+
this.render();
|
|
166
|
+
this.options.onFrameChange?.(this.currentFrame);
|
|
167
|
+
this.notify();
|
|
168
|
+
}
|
|
169
|
+
getState() {
|
|
170
|
+
return {
|
|
171
|
+
currentFrame: this.currentFrame,
|
|
172
|
+
totalFrames: this.getTotalFrames(),
|
|
173
|
+
isPlaying: this.isPlaying,
|
|
174
|
+
isLoaded: this.isLoaded
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
subscribe(listener) {
|
|
178
|
+
this.listeners.add(listener);
|
|
179
|
+
listener(this.getState());
|
|
180
|
+
return () => this.listeners.delete(listener);
|
|
181
|
+
}
|
|
182
|
+
updateOptions(partial) {
|
|
183
|
+
const prevSrc = this.options.src;
|
|
184
|
+
const prevFps = this.options.fps;
|
|
185
|
+
this.options = { ...this.options, ...partial };
|
|
186
|
+
if (partial.src !== void 0 && partial.src !== prevSrc) {
|
|
187
|
+
this.loadImage();
|
|
188
|
+
} else if (this.isLoaded) {
|
|
189
|
+
this.render();
|
|
190
|
+
}
|
|
191
|
+
if (partial.fps !== void 0 && partial.fps !== prevFps) {
|
|
192
|
+
this.accumulatedTime = 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
destroy() {
|
|
196
|
+
this.destroyed = true;
|
|
197
|
+
this.pause();
|
|
198
|
+
this.listeners.clear();
|
|
199
|
+
if (this.target && this.options.renderer === "css") {
|
|
200
|
+
resetCssRenderer(this.target);
|
|
201
|
+
}
|
|
202
|
+
this.target = null;
|
|
203
|
+
this.image = null;
|
|
204
|
+
}
|
|
205
|
+
getTotalFrames() {
|
|
206
|
+
return getTotalFrames(this.options.rows, this.options.cols);
|
|
207
|
+
}
|
|
208
|
+
loadImage() {
|
|
209
|
+
this.isLoaded = false;
|
|
210
|
+
const img = new Image();
|
|
211
|
+
img.crossOrigin = "anonymous";
|
|
212
|
+
img.onload = () => {
|
|
213
|
+
if (this.destroyed) return;
|
|
214
|
+
this.image = img;
|
|
215
|
+
this.isLoaded = true;
|
|
216
|
+
if (!this.options.width || !this.options.height) {
|
|
217
|
+
const frameWidth = img.naturalWidth / this.options.cols;
|
|
218
|
+
const frameHeight = img.naturalHeight / this.options.rows;
|
|
219
|
+
this.options.width = frameWidth;
|
|
220
|
+
this.options.height = frameHeight;
|
|
221
|
+
}
|
|
222
|
+
this.render();
|
|
223
|
+
this.notify();
|
|
224
|
+
if (this.options.autoPlay && this.target) {
|
|
225
|
+
this.play();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
img.onerror = () => {
|
|
229
|
+
console.error(`[SpriteAnimator] Failed to load image: ${this.options.src}`);
|
|
230
|
+
};
|
|
231
|
+
img.src = this.options.src;
|
|
232
|
+
}
|
|
233
|
+
advanceFrame() {
|
|
234
|
+
const total = this.getTotalFrames();
|
|
235
|
+
const next = this.currentFrame + 1;
|
|
236
|
+
if (next >= total) {
|
|
237
|
+
if (this.options.loop) {
|
|
238
|
+
this.currentFrame = 0;
|
|
239
|
+
} else {
|
|
240
|
+
this.currentFrame = total - 1;
|
|
241
|
+
this.pause();
|
|
242
|
+
this.options.onComplete?.();
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
this.currentFrame = next;
|
|
246
|
+
}
|
|
247
|
+
this.render();
|
|
248
|
+
this.options.onFrameChange?.(this.currentFrame);
|
|
249
|
+
this.notify();
|
|
250
|
+
}
|
|
251
|
+
render() {
|
|
252
|
+
if (!this.target || !this.isLoaded) return;
|
|
253
|
+
const { src, rows, cols, width, height, renderer } = this.options;
|
|
254
|
+
if (renderer === "canvas" && this.target instanceof HTMLCanvasElement && this.image) {
|
|
255
|
+
drawCanvasFrame(this.target, this.image, this.currentFrame, rows, cols, width, height);
|
|
256
|
+
} else if (renderer === "css" && this.target instanceof HTMLElement) {
|
|
257
|
+
applyCssFrame(this.target, src, this.currentFrame, rows, cols, width, height);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
notify() {
|
|
261
|
+
const state = this.getState();
|
|
262
|
+
this.listeners.forEach((listener) => listener(state));
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
266
|
+
0 && (module.exports = {
|
|
267
|
+
SPRITE_ANIMATION_DEFAULTS,
|
|
268
|
+
SpriteAnimator,
|
|
269
|
+
getBackgroundPositionPercent,
|
|
270
|
+
getFramePosition,
|
|
271
|
+
getTotalFrames
|
|
272
|
+
});
|
|
273
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/index.ts","../src/core/constants.ts","../src/core/utils.ts","../src/core/canvas-renderer.ts","../src/core/css-renderer.ts","../src/core/sprite-animator.ts"],"sourcesContent":["export { SPRITE_ANIMATION_DEFAULTS } from './constants.js';\nexport { SpriteAnimator } from './sprite-animator.js';\nexport type {\n FramePosition,\n RendererMode,\n SpriteAnimationOptions,\n SpriteAnimationState,\n} from './types.js';\nexport {\n getBackgroundPositionPercent,\n getFramePosition,\n getTotalFrames,\n} from './utils.js';\n","import type { RendererMode } from './types.js';\n\nexport const SPRITE_ANIMATION_DEFAULTS = {\n fps: 12,\n loop: true,\n width: 128,\n height: 128,\n autoPlay: true,\n renderer: 'css' as RendererMode,\n} as const;\n","import type { FramePosition } from './types.js';\n\nexport function getTotalFrames(rows: number, cols: number): number {\n return rows * cols;\n}\n\nexport function getFramePosition(frameIndex: number, cols: number): FramePosition {\n return {\n row: Math.floor(frameIndex / cols),\n col: frameIndex % cols,\n };\n}\n\nexport function getBackgroundPositionPercent(\n frameIndex: number,\n rows: number,\n cols: number,\n): { x: number; y: number } {\n const { row, col } = getFramePosition(frameIndex, cols);\n const x = cols <= 1 ? 0 : (col / (cols - 1)) * 100;\n const y = rows <= 1 ? 0 : (row / (rows - 1)) * 100;\n return { x, y };\n}\n","import { getFramePosition } from './utils.js';\n\nexport function drawCanvasFrame(\n canvas: HTMLCanvasElement,\n image: HTMLImageElement,\n frameIndex: number,\n rows: number,\n cols: number,\n width: number,\n height: number,\n): void {\n const ctx = canvas.getContext('2d');\n if (!ctx) return;\n\n const frameWidth = image.naturalWidth / cols;\n const frameHeight = image.naturalHeight / rows;\n const { row, col } = getFramePosition(frameIndex, cols);\n\n canvas.width = width;\n canvas.height = height;\n\n ctx.clearRect(0, 0, width, height);\n ctx.drawImage(\n image,\n col * frameWidth,\n row * frameHeight,\n frameWidth,\n frameHeight,\n 0,\n 0,\n width,\n height,\n );\n}\n","import { getBackgroundPositionPercent } from './utils.js';\n\nexport interface CssRendererTarget {\n style: CSSStyleDeclaration;\n}\n\nexport function applyCssFrame(\n target: CssRendererTarget,\n src: string,\n frameIndex: number,\n rows: number,\n cols: number,\n width: number,\n height: number,\n): void {\n const { x, y } = getBackgroundPositionPercent(frameIndex, rows, cols);\n\n target.style.backgroundImage = `url(\"${src}\")`;\n target.style.backgroundRepeat = 'no-repeat';\n target.style.backgroundSize = `${cols * 100}% ${rows * 100}%`;\n target.style.backgroundPosition = `${x}% ${y}%`;\n target.style.width = `${width}px`;\n target.style.height = `${height}px`;\n target.style.display = 'inline-block';\n}\n\nexport function resetCssRenderer(target: CssRendererTarget): void {\n target.style.backgroundImage = '';\n}\n","import { SPRITE_ANIMATION_DEFAULTS } from './constants.js';\nimport { drawCanvasFrame } from './canvas-renderer.js';\nimport { applyCssFrame, resetCssRenderer } from './css-renderer.js';\nimport type { SpriteAnimationOptions, SpriteAnimationState } from './types.js';\nimport { getTotalFrames } from './utils.js';\n\ntype StateListener = (state: SpriteAnimationState) => void;\n\ntype ResolvedSpriteAnimationOptions = Required<\n Pick<SpriteAnimationOptions, 'src' | 'rows' | 'cols'>\n> &\n Required<Pick<SpriteAnimationOptions, 'fps' | 'loop' | 'width' | 'height' | 'autoPlay' | 'renderer'>> &\n Pick<SpriteAnimationOptions, 'onComplete' | 'onFrameChange'>;\n\nexport class SpriteAnimator {\n private options: ResolvedSpriteAnimationOptions;\n private currentFrame = 0;\n private isPlaying = false;\n private isLoaded = false;\n private rafId: number | null = null;\n private lastTimestamp = 0;\n private accumulatedTime = 0;\n private image: HTMLImageElement | null = null;\n private target: HTMLElement | HTMLCanvasElement | null = null;\n private listeners = new Set<StateListener>();\n private destroyed = false;\n\n constructor(options: SpriteAnimationOptions) {\n this.options = {\n ...SPRITE_ANIMATION_DEFAULTS,\n ...options,\n };\n this.loadImage();\n }\n\n attach(target: HTMLElement | HTMLCanvasElement): void {\n this.target = target;\n if (this.isLoaded) {\n this.render();\n }\n if (this.options.autoPlay) {\n this.play();\n }\n }\n\n play(): void {\n if (this.destroyed || this.isPlaying) return;\n this.isPlaying = true;\n this.lastTimestamp = 0;\n this.accumulatedTime = 0;\n this.rafId = requestAnimationFrame(this.tick);\n this.notify();\n }\n\n pause(): void {\n if (!this.isPlaying) return;\n this.isPlaying = false;\n if (this.rafId !== null) {\n cancelAnimationFrame(this.rafId);\n this.rafId = null;\n }\n this.notify();\n }\n\n stop(): void {\n this.pause();\n this.currentFrame = 0;\n this.render();\n this.notify();\n }\n\n goToFrame(frame: number): void {\n const total = this.getTotalFrames();\n this.currentFrame = Math.max(0, Math.min(frame, total - 1));\n this.render();\n this.options.onFrameChange?.(this.currentFrame);\n this.notify();\n }\n\n getState(): SpriteAnimationState {\n return {\n currentFrame: this.currentFrame,\n totalFrames: this.getTotalFrames(),\n isPlaying: this.isPlaying,\n isLoaded: this.isLoaded,\n };\n }\n\n subscribe(listener: StateListener): () => void {\n this.listeners.add(listener);\n listener(this.getState());\n return () => this.listeners.delete(listener);\n }\n\n updateOptions(partial: Partial<SpriteAnimationOptions>): void {\n const prevSrc = this.options.src;\n const prevFps = this.options.fps;\n this.options = { ...this.options, ...partial };\n\n if (partial.src !== undefined && partial.src !== prevSrc) {\n this.loadImage();\n } else if (this.isLoaded) {\n this.render();\n }\n\n if (partial.fps !== undefined && partial.fps !== prevFps) {\n this.accumulatedTime = 0;\n }\n }\n\n destroy(): void {\n this.destroyed = true;\n this.pause();\n this.listeners.clear();\n if (this.target && this.options.renderer === 'css') {\n resetCssRenderer(this.target);\n }\n this.target = null;\n this.image = null;\n }\n\n private getTotalFrames(): number {\n return getTotalFrames(this.options.rows, this.options.cols);\n }\n\n private loadImage(): void {\n this.isLoaded = false;\n const img = new Image();\n img.crossOrigin = 'anonymous';\n img.onload = () => {\n if (this.destroyed) return;\n this.image = img;\n this.isLoaded = true;\n\n if (!this.options.width || !this.options.height) {\n const frameWidth = img.naturalWidth / this.options.cols;\n const frameHeight = img.naturalHeight / this.options.rows;\n this.options.width = frameWidth;\n this.options.height = frameHeight;\n }\n\n this.render();\n this.notify();\n\n if (this.options.autoPlay && this.target) {\n this.play();\n }\n };\n img.onerror = () => {\n console.error(`[SpriteAnimator] Failed to load image: ${this.options.src}`);\n };\n img.src = this.options.src;\n }\n\n private tick = (timestamp: number): void => {\n if (!this.isPlaying || this.destroyed) return;\n\n if (this.lastTimestamp === 0) {\n this.lastTimestamp = timestamp;\n }\n\n const delta = timestamp - this.lastTimestamp;\n this.lastTimestamp = timestamp;\n this.accumulatedTime += delta;\n\n const frameDuration = 1000 / this.options.fps;\n while (this.accumulatedTime >= frameDuration) {\n this.accumulatedTime -= frameDuration;\n this.advanceFrame();\n }\n\n this.rafId = requestAnimationFrame(this.tick);\n };\n\n private advanceFrame(): void {\n const total = this.getTotalFrames();\n const next = this.currentFrame + 1;\n\n if (next >= total) {\n if (this.options.loop) {\n this.currentFrame = 0;\n } else {\n this.currentFrame = total - 1;\n this.pause();\n this.options.onComplete?.();\n }\n } else {\n this.currentFrame = next;\n }\n\n this.render();\n this.options.onFrameChange?.(this.currentFrame);\n this.notify();\n }\n\n private render(): void {\n if (!this.target || !this.isLoaded) return;\n\n const { src, rows, cols, width, height, renderer } = this.options;\n\n if (renderer === 'canvas' && this.target instanceof HTMLCanvasElement && this.image) {\n drawCanvasFrame(this.target, this.image, this.currentFrame, rows, cols, width, height);\n } else if (renderer === 'css' && this.target instanceof HTMLElement) {\n applyCssFrame(this.target, src, this.currentFrame, rows, cols, width, height);\n }\n }\n\n private notify(): void {\n const state = this.getState();\n this.listeners.forEach((listener) => listener(state));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,4BAA4B;AAAA,EACvC,KAAK;AAAA,EACL,MAAM;AAAA,EACN,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,UAAU;AACZ;;;ACPO,SAAS,eAAe,MAAc,MAAsB;AACjE,SAAO,OAAO;AAChB;AAEO,SAAS,iBAAiB,YAAoB,MAA6B;AAChF,SAAO;AAAA,IACL,KAAK,KAAK,MAAM,aAAa,IAAI;AAAA,IACjC,KAAK,aAAa;AAAA,EACpB;AACF;AAEO,SAAS,6BACd,YACA,MACA,MAC0B;AAC1B,QAAM,EAAE,KAAK,IAAI,IAAI,iBAAiB,YAAY,IAAI;AACtD,QAAM,IAAI,QAAQ,IAAI,IAAK,OAAO,OAAO,KAAM;AAC/C,QAAM,IAAI,QAAQ,IAAI,IAAK,OAAO,OAAO,KAAM;AAC/C,SAAO,EAAE,GAAG,EAAE;AAChB;;;ACpBO,SAAS,gBACd,QACA,OACA,YACA,MACA,MACA,OACA,QACM;AACN,QAAM,MAAM,OAAO,WAAW,IAAI;AAClC,MAAI,CAAC,IAAK;AAEV,QAAM,aAAa,MAAM,eAAe;AACxC,QAAM,cAAc,MAAM,gBAAgB;AAC1C,QAAM,EAAE,KAAK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 +1,84 @@
|
|
|
1
|
+
type RendererMode = 'css' | 'canvas';
|
|
2
|
+
interface SpriteAnimationOptions {
|
|
3
|
+
/** Sprite sheet image URL (PNG, WebP, etc.) */
|
|
4
|
+
src: string;
|
|
5
|
+
/** Number of rows in the sprite sheet */
|
|
6
|
+
rows: number;
|
|
7
|
+
/** Number of columns in the sprite sheet */
|
|
8
|
+
cols: number;
|
|
9
|
+
/** Frames per second (default: 12) */
|
|
10
|
+
fps?: number;
|
|
11
|
+
/** Whether to loop the animation (default: true) */
|
|
12
|
+
loop?: boolean;
|
|
13
|
+
/** Display width in pixels */
|
|
14
|
+
width?: number;
|
|
15
|
+
/** Display height in pixels */
|
|
16
|
+
height?: number;
|
|
17
|
+
/** Start playing automatically (default: true) */
|
|
18
|
+
autoPlay?: boolean;
|
|
19
|
+
/** Rendering mode: CSS background-position or Canvas (default: 'css') */
|
|
20
|
+
renderer?: RendererMode;
|
|
21
|
+
/** Called when a non-looping animation completes */
|
|
22
|
+
onComplete?: () => void;
|
|
23
|
+
/** Called on each frame change */
|
|
24
|
+
onFrameChange?: (frame: number) => void;
|
|
25
|
+
}
|
|
26
|
+
interface SpriteAnimationState {
|
|
27
|
+
currentFrame: number;
|
|
28
|
+
totalFrames: number;
|
|
29
|
+
isPlaying: boolean;
|
|
30
|
+
isLoaded: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface FramePosition {
|
|
33
|
+
row: number;
|
|
34
|
+
col: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const SPRITE_ANIMATION_DEFAULTS: {
|
|
38
|
+
readonly fps: 12;
|
|
39
|
+
readonly loop: true;
|
|
40
|
+
readonly width: 128;
|
|
41
|
+
readonly height: 128;
|
|
42
|
+
readonly autoPlay: true;
|
|
43
|
+
readonly renderer: RendererMode;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type StateListener = (state: SpriteAnimationState) => void;
|
|
47
|
+
declare class SpriteAnimator {
|
|
48
|
+
private options;
|
|
49
|
+
private currentFrame;
|
|
50
|
+
private isPlaying;
|
|
51
|
+
private isLoaded;
|
|
52
|
+
private rafId;
|
|
53
|
+
private lastTimestamp;
|
|
54
|
+
private accumulatedTime;
|
|
55
|
+
private image;
|
|
56
|
+
private target;
|
|
57
|
+
private listeners;
|
|
58
|
+
private destroyed;
|
|
59
|
+
constructor(options: SpriteAnimationOptions);
|
|
60
|
+
attach(target: HTMLElement | HTMLCanvasElement): void;
|
|
61
|
+
play(): void;
|
|
62
|
+
pause(): void;
|
|
63
|
+
stop(): void;
|
|
64
|
+
goToFrame(frame: number): void;
|
|
65
|
+
getState(): SpriteAnimationState;
|
|
66
|
+
subscribe(listener: StateListener): () => void;
|
|
67
|
+
updateOptions(partial: Partial<SpriteAnimationOptions>): void;
|
|
68
|
+
destroy(): void;
|
|
69
|
+
private getTotalFrames;
|
|
70
|
+
private loadImage;
|
|
71
|
+
private tick;
|
|
72
|
+
private advanceFrame;
|
|
73
|
+
private render;
|
|
74
|
+
private notify;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
declare function getTotalFrames(rows: number, cols: number): number;
|
|
78
|
+
declare function getFramePosition(frameIndex: number, cols: number): FramePosition;
|
|
79
|
+
declare function getBackgroundPositionPercent(frameIndex: number, rows: number, cols: number): {
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { type FramePosition, type RendererMode, SPRITE_ANIMATION_DEFAULTS, type SpriteAnimationOptions, type SpriteAnimationState, SpriteAnimator, getBackgroundPositionPercent, getFramePosition, getTotalFrames };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
type RendererMode = 'css' | 'canvas';
|
|
2
|
+
interface SpriteAnimationOptions {
|
|
3
|
+
/** Sprite sheet image URL (PNG, WebP, etc.) */
|
|
4
|
+
src: string;
|
|
5
|
+
/** Number of rows in the sprite sheet */
|
|
6
|
+
rows: number;
|
|
7
|
+
/** Number of columns in the sprite sheet */
|
|
8
|
+
cols: number;
|
|
9
|
+
/** Frames per second (default: 12) */
|
|
10
|
+
fps?: number;
|
|
11
|
+
/** Whether to loop the animation (default: true) */
|
|
12
|
+
loop?: boolean;
|
|
13
|
+
/** Display width in pixels */
|
|
14
|
+
width?: number;
|
|
15
|
+
/** Display height in pixels */
|
|
16
|
+
height?: number;
|
|
17
|
+
/** Start playing automatically (default: true) */
|
|
18
|
+
autoPlay?: boolean;
|
|
19
|
+
/** Rendering mode: CSS background-position or Canvas (default: 'css') */
|
|
20
|
+
renderer?: RendererMode;
|
|
21
|
+
/** Called when a non-looping animation completes */
|
|
22
|
+
onComplete?: () => void;
|
|
23
|
+
/** Called on each frame change */
|
|
24
|
+
onFrameChange?: (frame: number) => void;
|
|
25
|
+
}
|
|
26
|
+
interface SpriteAnimationState {
|
|
27
|
+
currentFrame: number;
|
|
28
|
+
totalFrames: number;
|
|
29
|
+
isPlaying: boolean;
|
|
30
|
+
isLoaded: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface FramePosition {
|
|
33
|
+
row: number;
|
|
34
|
+
col: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const SPRITE_ANIMATION_DEFAULTS: {
|
|
38
|
+
readonly fps: 12;
|
|
39
|
+
readonly loop: true;
|
|
40
|
+
readonly width: 128;
|
|
41
|
+
readonly height: 128;
|
|
42
|
+
readonly autoPlay: true;
|
|
43
|
+
readonly renderer: RendererMode;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type StateListener = (state: SpriteAnimationState) => void;
|
|
47
|
+
declare class SpriteAnimator {
|
|
48
|
+
private options;
|
|
49
|
+
private currentFrame;
|
|
50
|
+
private isPlaying;
|
|
51
|
+
private isLoaded;
|
|
52
|
+
private rafId;
|
|
53
|
+
private lastTimestamp;
|
|
54
|
+
private accumulatedTime;
|
|
55
|
+
private image;
|
|
56
|
+
private target;
|
|
57
|
+
private listeners;
|
|
58
|
+
private destroyed;
|
|
59
|
+
constructor(options: SpriteAnimationOptions);
|
|
60
|
+
attach(target: HTMLElement | HTMLCanvasElement): void;
|
|
61
|
+
play(): void;
|
|
62
|
+
pause(): void;
|
|
63
|
+
stop(): void;
|
|
64
|
+
goToFrame(frame: number): void;
|
|
65
|
+
getState(): SpriteAnimationState;
|
|
66
|
+
subscribe(listener: StateListener): () => void;
|
|
67
|
+
updateOptions(partial: Partial<SpriteAnimationOptions>): void;
|
|
68
|
+
destroy(): void;
|
|
69
|
+
private getTotalFrames;
|
|
70
|
+
private loadImage;
|
|
71
|
+
private tick;
|
|
72
|
+
private advanceFrame;
|
|
73
|
+
private render;
|
|
74
|
+
private notify;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
declare function getTotalFrames(rows: number, cols: number): number;
|
|
78
|
+
declare function getFramePosition(frameIndex: number, cols: number): FramePosition;
|
|
79
|
+
declare function getBackgroundPositionPercent(frameIndex: number, rows: number, cols: number): {
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { type FramePosition, type RendererMode, SPRITE_ANIMATION_DEFAULTS, type SpriteAnimationOptions, type SpriteAnimationState, SpriteAnimator, getBackgroundPositionPercent, getFramePosition, getTotalFrames };
|