This repository was archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageFrame.js
102 lines (96 loc) · 2.71 KB
/
ImageFrame.js
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
"use strict";
class ImageFrame {
constructor(rawFrameData) {
/**
* The delay of a frame
* @type {number}
*/
this.delay = rawFrameData.delay;
const { x, y, width, height } = rawFrameData.rect;
/**
* The position to animate on the X axis
* @type {number}
*/
this.x = x;
/**
* The position to animate on the Y axis
* @type {number}
*/
this.y = y;
/**
* The width of the frame
* @type {number}
*/
this.width = width;
/**
* The height of the frame
* @type {number}
*/
this.height = height;
/**
* The disposal method of the frame
* @type {number}
*/
this.dispose = this.disposeMethod = rawFrameData.dispose;
/**
* The blending method of the frame
* @type {number}
*/
this.blend = this.blendMethod = rawFrameData.blend;
/**
* The color data
* @type {Buffer}
*/
this.data = Buffer.from(rawFrameData.data.buffer);
/**
* The delay of the frame in milliseconds
* @type {number}
*/
this.delay = rawFrameData.delay;
/**
* Whatever came out of upng.js
*/
this.raw = rawFrameData;
}
/**
* Returns a JIMP compatible bitmap with the frame metadata
* @returns {import("@jimp/core").Bitmap}
*/
get bitmap() {
return {
width: this.width,
height: this.height,
data: this.data,
delay: this.delay,
dispose: this.dispose,
blend: this.blend
};
}
/**
* Create an ImageFrame from a JIMP image
* @param {import("@jimp/core").Image} img The JIMP image
* @param {object} opts The options for the frame
*/
static createFromJIMP(img, opts) {
const o = {...opts}
delete o.rect;
const opt = {
dispose: 1,
blend: 0,
rect: {
x: opts && opts.rect &&
opts.rect.x != null ? opts.rect.x : opts && (opts.x || 0),
y: opts && opts.rect &&
opts.rect.y != null ? opts.rect.y : opts && (opts.y || 0),
width: opts && opts.rect &&
opts.rect.width != null ? opts.rect.height : img.bitmap.width,
height: opts && opts.rect &&
opts.rect.height != null ? opts.rect.height : img.bitmap.height
},
data: img.bitmap.data,
...o
};
return new ImageFrame(opt);
}
}
module.exports = ImageFrame;