forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgif-animator.js
More file actions
248 lines (215 loc) · 7.25 KB
/
gif-animator.js
File metadata and controls
248 lines (215 loc) · 7.25 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2025 Music Blocks Contributors
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This file depends on SuperGif (libgif.js), which is licensed
// under the MIT License. See: https://github.com/buzzfeed/libgif-js
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/**
* GIF animation manager for Music Blocks.
*
* Handles animated GIF playback on the turtle canvas using the
* SuperGif decoder from libgif.js. This module manages frame timing,
* rendering, position updates, and cleanup of animation resources.
*/
/* exported GIFAnimator */
/**
* Manages animated GIF playback on the canvas.
*/
class GIFAnimator {
constructor() {
this.animations = new Map(); // Tracks active GIF animations by ID
this.frameRequestId = null;
this.isRunning = false;
this.gifCounter = 0; // Generates unique animation IDs
}
/**
* Determines whether a data URL represents a GIF.
*/
isAnimatedGIF(dataURL) {
return typeof dataURL === "string" && dataURL.startsWith("data:image/gif");
}
/**
* Creates a unique identifier for a GIF animation.
*/
generateGifId() {
return `gif_${Date.now()}_${this.gifCounter++}`;
}
/**
* Creates and initializes a new GIF animation instance.
*
* Loads the GIF through SuperGif, extracts frame metadata, and registers
* the animation for rendering.
*
* @returns {Promise<string|null>} A unique GIF ID or null if not animated.
*/
async createAnimation(dataURL, canvas, x, y, width, height, rotation) {
try {
if (!window.SuperGif) {
throw new Error("libgif.js (SuperGif) is not loaded");
}
// Hidden <img> element required by SuperGif
const img = document.createElement("img");
img.src = dataURL;
img.style.display = "none";
document.body.appendChild(img);
const gifPlayer = new SuperGif({ gif: img });
await new Promise(resolve => gifPlayer.load(resolve));
const totalFrames = gifPlayer.get_length();
// Ignore static (single-frame) GIFs
if (totalFrames <= 1) {
document.body.removeChild(img);
return null;
}
const internalCanvas = gifPlayer.get_canvas();
const frameCanvas = document.createElement("canvas");
frameCanvas.width = internalCanvas.width;
frameCanvas.height = internalCanvas.height;
const animation = {
gifPlayer,
frames: totalFrames,
frameCanvas,
frameCtx: frameCanvas.getContext("2d"),
currentFrame: 0,
lastFrameTime: 0,
canvas,
x,
y,
width,
height,
rotation,
disposed: false,
imgElement: img
};
const gifId = this.generateGifId();
this.animations.set(gifId, animation);
if (!this.isRunning) {
this.start();
}
return gifId;
} catch (error) {
console.error("Failed to create GIF animation:", error);
return null;
}
}
/**
* Renders a single GIF frame to the target canvas.
*/
renderFrame(animation) {
const ctx = animation.canvas.getContext("2d");
// Clear only the region occupied by the GIF
ctx.save();
ctx.translate(animation.x, animation.y);
ctx.rotate((animation.rotation * Math.PI) / 180);
ctx.clearRect(
-animation.width / 2 - 2,
-animation.height / 2 - 2,
animation.width + 4,
animation.height + 4
);
ctx.restore();
// Decode the next frame
animation.gifPlayer.move_to(animation.currentFrame);
const frameImage = animation.gifPlayer.get_canvas();
// Draw the frame
ctx.save();
ctx.translate(animation.x, animation.y);
ctx.rotate((animation.rotation * Math.PI) / 180);
ctx.drawImage(
frameImage,
-animation.width / 2,
-animation.height / 2,
animation.width,
animation.height
);
ctx.restore();
}
/**
* Main animation loop. Advances and renders frames based on time.
*/
animate(timestamp) {
if (!this.isRunning) return;
const FRAME_DELAY = 120; // ~8 FPS
this.animations.forEach((animation, gifId) => {
if (animation.disposed) {
animation.gifPlayer.pause();
document.body.removeChild(animation.imgElement);
this.animations.delete(gifId);
return;
}
// Initialize first frame timestamp
if (!animation.lastFrameTime) {
animation.lastFrameTime = timestamp;
this.renderFrame(animation);
return;
}
if (timestamp - animation.lastFrameTime >= FRAME_DELAY) {
this.renderFrame(animation);
animation.currentFrame = (animation.currentFrame + 1) % animation.frames;
animation.lastFrameTime = timestamp;
}
});
if (this.animations.size > 0) {
this.frameRequestId = requestAnimationFrame(ts => this.animate(ts));
} else {
this.isRunning = false;
}
}
/**
* Starts the animation loop.
*/
start() {
if (!this.isRunning) {
this.isRunning = true;
this.frameRequestId = requestAnimationFrame(ts => this.animate(ts));
}
}
/**
* Updates position and rotation of an active animation.
*/
updatePosition(gifId, x, y, rotation) {
const animation = this.animations.get(gifId);
if (animation && !animation.disposed) {
animation.x = x;
animation.y = y;
animation.rotation = rotation;
}
}
/**
* Stops and removes a specific GIF animation.
*/
stopAnimation(gifId) {
const animation = this.animations.get(gifId);
if (animation) {
animation.disposed = true;
this.animations.delete(gifId);
}
}
/**
* Stops all animations and resets internal state.
*/
stopAll() {
this.animations.forEach(anim => (anim.disposed = true));
this.animations.clear();
if (this.frameRequestId) {
cancelAnimationFrame(this.frameRequestId);
this.frameRequestId = null;
}
this.isRunning = false;
}
/**
* Returns how many GIF animations are currently active.
*/
getActiveCount() {
return this.animations.size;
}
}
if (typeof module !== "undefined" && module.exports) {
module.exports = GIFAnimator;
}