-
Notifications
You must be signed in to change notification settings - Fork 9
/
node-example.mjs
87 lines (67 loc) · 2.48 KB
/
node-example.mjs
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
import createDotLottiePlayerModule from "./release/wasm/DotLottiePlayer.mjs";
import fs from "fs";
import path from "path";
const wasmBinary = fs.readFileSync(
path.join("./release/wasm/DotLottiePlayer.wasm")
);
const Module = await createDotLottiePlayerModule({
wasmBinary,
});
const WIDTH = 200;
const HEIGHT = 200;
function createSegments(startFrame, endFrame) {
const vector = new Module.VectorFloat();
if (startFrame && endFrame) {
vector.push_back(startFrame);
vector.push_back(endFrame);
}
return vector;
}
const dotLottiePlayer = new Module.DotLottiePlayer({
...Module.createDefaultConfig(),
backgroundColor: 0xff009aff,
});
const data = await fetch(
"https://lottie.host/647eb023-6040-4b60-a275-e2546994dd7f/zDCfp5lhLe.json"
).then((res) => res.text());
const loaded = dotLottiePlayer.loadAnimationData(data, WIDTH, HEIGHT);
if (!loaded) {
console.log("failed to load animation data");
}
dotLottiePlayer.setFrame(10.0);
const rendered = dotLottiePlayer.render();
if (!rendered) {
console.log("failed to render");
}
const frameBuffer = dotLottiePlayer.buffer();
const bmpBuffer = createBMP(WIDTH, WIDTH, frameBuffer);
fs.writeFileSync("./output.bmp", bmpBuffer);
// This is for demonstration purposes only. to avoid adding a dependency
function createBMP(width, height, frameBuffer) {
// Each pixel in BMP is 4 bytes (BGRA)
const bmpDataSize = width * height * 4;
const headerSize = 54;
const fileSize = bmpDataSize + headerSize;
const bmpBuffer = Buffer.alloc(fileSize);
// Bitmap file header
bmpBuffer.write("BM", 0); // Signature
bmpBuffer.writeInt32LE(fileSize, 2); // File size
bmpBuffer.writeInt32LE(headerSize, 10); // Pixel data offset
// DIB header
bmpBuffer.writeInt32LE(40, 14); // DIB header size
bmpBuffer.writeInt32LE(width, 18); // Width
bmpBuffer.writeInt32LE(-height, 22); // Height (negative for top-down bitmap)
bmpBuffer.writeInt16LE(1, 26); // Color planes
bmpBuffer.writeInt16LE(32, 28); // Bits per pixel
bmpBuffer.writeInt32LE(0, 30); // Compression (0 for none)
// Convert RGBA to BGRA and write pixel data
for (let i = 0; i < width * height; i++) {
const rgbaIndex = i * 4;
const bgraIndex = headerSize + i * 4;
bmpBuffer[bgraIndex + 0] = frameBuffer[rgbaIndex + 2]; // Blue
bmpBuffer[bgraIndex + 1] = frameBuffer[rgbaIndex + 1]; // Green
bmpBuffer[bgraIndex + 2] = frameBuffer[rgbaIndex + 0]; // Red
bmpBuffer[bgraIndex + 3] = frameBuffer[rgbaIndex + 3]; // Alpha
}
return bmpBuffer;
}