@next2d/display 1.14.20

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Next2D
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,11 @@
1
+ @next2d/display
2
+ =============
3
+
4
+ ## Installation
5
+
6
+ ```
7
+ npm install @next2d/display
8
+ ```
9
+
10
+ ## License
11
+ This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,142 @@
1
+ import { DisplayObjectImpl } from "@next2d/interface";
2
+ import { Matrix, ColorTransform } from "@next2d/geom";
3
+ /**
4
+ * BitmapData クラスを使用すると、Bitmap オブジェクトのデータ (ピクセル) を処理できます。
5
+ * BitmapData クラスのメソッドを使用して、任意のサイズの透明または不透明のビットマップイメージを作成し
6
+ * 実行時に様々な方法で操作できます。
7
+ *
8
+ * The BitmapData class lets you work with the data (pixels) of a Bitmap object.
9
+ * You can use the methods of the BitmapData class to create arbitrarily sized transparent or
10
+ * opaque bitmap images and manipulate them in various ways at runtime.
11
+ *
12
+ * @class
13
+ * @memberOf next2d.display
14
+ */
15
+ export declare class BitmapData {
16
+ private readonly _$instanceId;
17
+ private _$width;
18
+ private _$height;
19
+ _$buffer: Uint8Array | null;
20
+ private _$image;
21
+ private _$canvas;
22
+ private _$pixelBuffer;
23
+ /**
24
+ * @param {number} [width=0]
25
+ * @param {number} [height=0]
26
+ * @constructor
27
+ * @public
28
+ */
29
+ constructor(width?: number, height?: number);
30
+ /**
31
+ * @description 指定されたクラスのストリングを返します。
32
+ * Returns the string representation of the specified class.
33
+ *
34
+ * @return {string}
35
+ * @default [class BitmapData]
36
+ * @method
37
+ * @static
38
+ */
39
+ static toString(): string;
40
+ /**
41
+ * @description 指定されたクラスの空間名を返します。
42
+ * Returns the space name of the specified class.
43
+ *
44
+ * @return {string}
45
+ * @default next2d.display.BitmapData
46
+ * @const
47
+ * @static
48
+ */
49
+ static get namespace(): string;
50
+ /**
51
+ * @description 指定されたオブジェクトのストリングを返します。
52
+ * Returns the string representation of the specified object.
53
+ *
54
+ * @return {string}
55
+ * @default [object BitmapData]
56
+ * @method
57
+ * @public
58
+ */
59
+ toString(): string;
60
+ /**
61
+ * @description 指定されたオブジェクトの空間名を返します。
62
+ * Returns the space name of the specified object.
63
+ *
64
+ * @return {string}
65
+ * @default next2d.display.BitmapData
66
+ * @const
67
+ * @public
68
+ */
69
+ get namespace(): string;
70
+ /**
71
+ * @description ビットマップイメージの高さ(ピクセル単位)です。
72
+ * The height of the bitmap image in pixels.
73
+ *
74
+ * @return {number}
75
+ * @readonly
76
+ * @public
77
+ */
78
+ get height(): number;
79
+ /**
80
+ * @description Uint8Arrayを利用して BitmapData を生成します。
81
+ * Generates BitmapData using Uint8Array.
82
+ *
83
+ * @return {Uint8Array}
84
+ * @public
85
+ */
86
+ get buffer(): Uint8Array | null;
87
+ set buffer(buffer: Uint8Array | null);
88
+ /**
89
+ * @description Imageクラスを利用して BitmapData を生成します。
90
+ * Use the Image class to generate BitmapData.
91
+ *
92
+ * @return {HTMLImageElement}
93
+ * @public
94
+ */
95
+ get image(): HTMLImageElement | null;
96
+ set image(image: HTMLImageElement | null);
97
+ /**
98
+ * @description Canvasクラス利用して BitmapData を生成します。
99
+ * Use the Canvas class to generate BitmapData.
100
+ *
101
+ * @return {HTMLCanvasElement}
102
+ * @public
103
+ */
104
+ get canvas(): HTMLCanvasElement | null;
105
+ set canvas(canvas: HTMLCanvasElement | null);
106
+ /**
107
+ * @description ビットマップイメージの幅(ピクセル単位)です。
108
+ * The width of the bitmap image in pixels.
109
+ *
110
+ * @return {number}
111
+ * @readonly
112
+ * @public
113
+ */
114
+ get width(): number;
115
+ /**
116
+ * @description 新しいオブジェクトとして、このクラスのクローンを返します。
117
+ * 含まれるオブジェクトはまったく同じコピーになります。
118
+ * Returns a clone of this class as a new object,
119
+ * with an exact copy of the contained object.
120
+ *
121
+ * @return {BitmapData}
122
+ * @method
123
+ * @public
124
+ */
125
+ clone(): BitmapData;
126
+ /**
127
+ * @member {WebGLTexture}
128
+ * @method
129
+ * @private
130
+ */
131
+ getTexture(): WebGLTexture | null;
132
+ /**
133
+ * @param {DisplayObject} source
134
+ * @param {Matrix} [matrix=null]
135
+ * @param {ColorTransform} [color_transform=null]
136
+ * @param {HTMLCanvasElement} [canvas=null]
137
+ * @param {function} [callback=null]
138
+ * @return {void}
139
+ * @public
140
+ */
141
+ draw(source: DisplayObjectImpl<any>, matrix?: Matrix | null, color_transform?: ColorTransform | null, canvas?: HTMLCanvasElement | null, callback?: Function | null): void;
142
+ }
@@ -0,0 +1,386 @@
1
+ import { DisplayObjectContainer } from "./DisplayObjectContainer";
2
+ import { $COLOR_ARRAY_IDENTITY, $getArray, $MATRIX_ARRAY_IDENTITY, $multiplicationMatrix, $poolArray } from "@next2d/share";
3
+ import { $getInstanceId, $bitmapDrawMap, $currentPlayer, $poolColorTransform, $poolMatrix, $postContainerWorker, $rendererWorker } from "@next2d/util";
4
+ /**
5
+ * BitmapData クラスを使用すると、Bitmap オブジェクトのデータ (ピクセル) を処理できます。
6
+ * BitmapData クラスのメソッドを使用して、任意のサイズの透明または不透明のビットマップイメージを作成し
7
+ * 実行時に様々な方法で操作できます。
8
+ *
9
+ * The BitmapData class lets you work with the data (pixels) of a Bitmap object.
10
+ * You can use the methods of the BitmapData class to create arbitrarily sized transparent or
11
+ * opaque bitmap images and manipulate them in various ways at runtime.
12
+ *
13
+ * @class
14
+ * @memberOf next2d.display
15
+ */
16
+ export class BitmapData {
17
+ /**
18
+ * @param {number} [width=0]
19
+ * @param {number} [height=0]
20
+ * @constructor
21
+ * @public
22
+ */
23
+ constructor(width = 0, height = 0) {
24
+ /**
25
+ * @type {number}
26
+ * @private
27
+ */
28
+ this._$instanceId = $getInstanceId();
29
+ /**
30
+ * @type {number}
31
+ * @default 0
32
+ * @private
33
+ */
34
+ this._$width = width | 0;
35
+ /**
36
+ * @type {number}
37
+ * @default 0
38
+ * @private
39
+ */
40
+ this._$height = height | 0;
41
+ /**
42
+ * @type {Uint8Array}
43
+ * @default null
44
+ * @private
45
+ */
46
+ this._$buffer = null;
47
+ /**
48
+ * @type {HTMLImageElement}
49
+ * @default null
50
+ * @private
51
+ */
52
+ this._$image = null;
53
+ /**
54
+ * @type {HTMLCanvasElement}
55
+ * @default null
56
+ * @private
57
+ */
58
+ this._$canvas = null;
59
+ /**
60
+ * @type {WebGLBuffer}
61
+ * @type {null}
62
+ * @private
63
+ */
64
+ this._$pixelBuffer = null;
65
+ }
66
+ /**
67
+ * @description 指定されたクラスのストリングを返します。
68
+ * Returns the string representation of the specified class.
69
+ *
70
+ * @return {string}
71
+ * @default [class BitmapData]
72
+ * @method
73
+ * @static
74
+ */
75
+ static toString() {
76
+ return "[class BitmapData]";
77
+ }
78
+ /**
79
+ * @description 指定されたクラスの空間名を返します。
80
+ * Returns the space name of the specified class.
81
+ *
82
+ * @return {string}
83
+ * @default next2d.display.BitmapData
84
+ * @const
85
+ * @static
86
+ */
87
+ static get namespace() {
88
+ return "next2d.display.BitmapData";
89
+ }
90
+ /**
91
+ * @description 指定されたオブジェクトのストリングを返します。
92
+ * Returns the string representation of the specified object.
93
+ *
94
+ * @return {string}
95
+ * @default [object BitmapData]
96
+ * @method
97
+ * @public
98
+ */
99
+ toString() {
100
+ return "[object BitmapData]";
101
+ }
102
+ /**
103
+ * @description 指定されたオブジェクトの空間名を返します。
104
+ * Returns the space name of the specified object.
105
+ *
106
+ * @return {string}
107
+ * @default next2d.display.BitmapData
108
+ * @const
109
+ * @public
110
+ */
111
+ get namespace() {
112
+ return "next2d.display.BitmapData";
113
+ }
114
+ /**
115
+ * @description ビットマップイメージの高さ(ピクセル単位)です。
116
+ * The height of the bitmap image in pixels.
117
+ *
118
+ * @return {number}
119
+ * @readonly
120
+ * @public
121
+ */
122
+ get height() {
123
+ return this._$height;
124
+ }
125
+ /**
126
+ * @description Uint8Arrayを利用して BitmapData を生成します。
127
+ * Generates BitmapData using Uint8Array.
128
+ *
129
+ * @return {Uint8Array}
130
+ * @public
131
+ */
132
+ get buffer() {
133
+ return this._$buffer;
134
+ }
135
+ set buffer(buffer) {
136
+ this._$buffer = buffer;
137
+ }
138
+ /**
139
+ * @description Imageクラスを利用して BitmapData を生成します。
140
+ * Use the Image class to generate BitmapData.
141
+ *
142
+ * @return {HTMLImageElement}
143
+ * @public
144
+ */
145
+ get image() {
146
+ return this._$image;
147
+ }
148
+ set image(image) {
149
+ this._$canvas = null;
150
+ this._$image = image;
151
+ if (!image) {
152
+ return;
153
+ }
154
+ this._$width = image.width;
155
+ this._$height = image.height;
156
+ }
157
+ /**
158
+ * @description Canvasクラス利用して BitmapData を生成します。
159
+ * Use the Canvas class to generate BitmapData.
160
+ *
161
+ * @return {HTMLCanvasElement}
162
+ * @public
163
+ */
164
+ get canvas() {
165
+ return this._$canvas;
166
+ }
167
+ set canvas(canvas) {
168
+ this._$image = null;
169
+ this._$canvas = canvas;
170
+ if (!canvas) {
171
+ return;
172
+ }
173
+ this._$width = canvas.width;
174
+ this._$height = canvas.height;
175
+ }
176
+ /**
177
+ * @description ビットマップイメージの幅(ピクセル単位)です。
178
+ * The width of the bitmap image in pixels.
179
+ *
180
+ * @return {number}
181
+ * @readonly
182
+ * @public
183
+ */
184
+ get width() {
185
+ return this._$width;
186
+ }
187
+ /**
188
+ * @description 新しいオブジェクトとして、このクラスのクローンを返します。
189
+ * 含まれるオブジェクトはまったく同じコピーになります。
190
+ * Returns a clone of this class as a new object,
191
+ * with an exact copy of the contained object.
192
+ *
193
+ * @return {BitmapData}
194
+ * @method
195
+ * @public
196
+ */
197
+ clone() {
198
+ const bitmapData = new BitmapData(this.width, this.height);
199
+ if (this._$image !== null || this._$canvas !== null) {
200
+ const player = $currentPlayer();
201
+ const cacheStore = player.cacheStore;
202
+ const canvas = cacheStore.getCanvas();
203
+ canvas.width = this.width;
204
+ canvas.height = this.height;
205
+ const context = canvas.getContext("2d");
206
+ if (!context) {
207
+ throw new Error("the context is null.");
208
+ }
209
+ if (this._$image) {
210
+ context.drawImage(this._$image, 0, 0);
211
+ }
212
+ if (this._$canvas) {
213
+ context.drawImage(this._$canvas, 0, 0);
214
+ }
215
+ bitmapData.canvas = canvas;
216
+ }
217
+ else if (this._$buffer !== null) {
218
+ bitmapData._$buffer = this._$buffer.slice();
219
+ }
220
+ return bitmapData;
221
+ }
222
+ /**
223
+ * @member {WebGLTexture}
224
+ * @method
225
+ * @private
226
+ */
227
+ getTexture() {
228
+ const { width, height } = this;
229
+ if (!width || !height) {
230
+ return null;
231
+ }
232
+ const player = $currentPlayer();
233
+ const context = player.context;
234
+ if (!context) {
235
+ throw new Error("the context is null.");
236
+ }
237
+ if (this._$image !== null) {
238
+ return context
239
+ .frameBuffer
240
+ .createTextureFromImage(this._$image);
241
+ }
242
+ if (this._$canvas !== null) {
243
+ return context
244
+ .frameBuffer
245
+ .createTextureFromCanvas(this._$canvas);
246
+ }
247
+ if (this._$buffer !== null) {
248
+ return context
249
+ .frameBuffer
250
+ .createTextureFromPixels(width, height, this._$buffer, true);
251
+ }
252
+ return null;
253
+ }
254
+ /**
255
+ * @param {DisplayObject} source
256
+ * @param {Matrix} [matrix=null]
257
+ * @param {ColorTransform} [color_transform=null]
258
+ * @param {HTMLCanvasElement} [canvas=null]
259
+ * @param {function} [callback=null]
260
+ * @return {void}
261
+ * @public
262
+ */
263
+ draw(source, matrix = null, color_transform = null, canvas = null, callback = null) {
264
+ const { width, height } = this;
265
+ if (!width || !height) {
266
+ return;
267
+ }
268
+ const player = $currentPlayer();
269
+ const cacheWidth = player._$width;
270
+ const cacheHeight = player._$height;
271
+ const resize = width > cacheWidth || height > cacheHeight;
272
+ if (resize) {
273
+ player._$width = width;
274
+ player._$height = height;
275
+ player._$resizeCanvas(width, height, player.scaleX);
276
+ }
277
+ const colorTransform = color_transform
278
+ ? color_transform._$colorTransform
279
+ : $COLOR_ARRAY_IDENTITY;
280
+ let tMatrix = matrix
281
+ ? matrix._$matrix
282
+ : $MATRIX_ARRAY_IDENTITY;
283
+ if (matrix) {
284
+ const matrix = source._$transform.matrix;
285
+ matrix.invert();
286
+ tMatrix = $multiplicationMatrix(tMatrix, matrix._$matrix);
287
+ $poolMatrix(matrix);
288
+ }
289
+ if (!canvas) {
290
+ canvas = player.cacheStore.getCanvas();
291
+ }
292
+ if ($rendererWorker) {
293
+ if (!source._$stage) {
294
+ if (source instanceof DisplayObjectContainer) {
295
+ if ($postContainerWorker) {
296
+ $postContainerWorker(source);
297
+ }
298
+ }
299
+ else {
300
+ source._$createWorkerInstance();
301
+ source._$postProperty();
302
+ }
303
+ }
304
+ canvas.width = width;
305
+ canvas.height = height;
306
+ const context = canvas.getContext("2d");
307
+ if (!context) {
308
+ throw new Error("the context is null.");
309
+ }
310
+ context.setTransform(1, 0, 0, 1, 0, 0);
311
+ context.clearRect(0, 0, width, height);
312
+ const instanceId = source._$instanceId;
313
+ $bitmapDrawMap.set(instanceId, {
314
+ "source": source,
315
+ "context": context,
316
+ "callback": callback
317
+ });
318
+ const options = $getArray();
319
+ const message = {
320
+ "command": "bitmapDraw",
321
+ "sourceId": instanceId,
322
+ "width": width,
323
+ "height": height
324
+ };
325
+ if (tMatrix[0] !== 1 || tMatrix[1] !== 0
326
+ || tMatrix[2] !== 0 || tMatrix[3] !== 1
327
+ || tMatrix[4] !== 0 || tMatrix[5] !== 0) {
328
+ message.matrix = tMatrix.slice();
329
+ options.push(message.matrix.buffer);
330
+ }
331
+ if (colorTransform[0] !== 1 || colorTransform[1] !== 1
332
+ || colorTransform[2] !== 1 || colorTransform[3] !== 1
333
+ || colorTransform[4] !== 0 || colorTransform[5] !== 0
334
+ || colorTransform[6] !== 0 || colorTransform[7] !== 0) {
335
+ message.colorTransform = colorTransform.slice();
336
+ options.push(message.colorTransform.buffer);
337
+ }
338
+ $rendererWorker.postMessage(message, options);
339
+ $poolArray(options);
340
+ }
341
+ else {
342
+ const context = player.context;
343
+ if (!context) {
344
+ throw new Error("the context is null.");
345
+ }
346
+ const manager = context.frameBuffer;
347
+ const currentAttachment = manager.currentAttachment;
348
+ context._$bind(player._$attachment);
349
+ // reset
350
+ context.reset();
351
+ context.setTransform(1, 0, 0, 1, 0, 0);
352
+ context.clearRect(0, 0, player._$width, player._$height);
353
+ context.beginPath();
354
+ source._$draw(context, tMatrix, colorTransform);
355
+ const texture = manager
356
+ .getTextureFromCurrentAttachment();
357
+ // reset and draw to main canvas
358
+ manager.unbind();
359
+ context.reset();
360
+ context.setTransform(1, 0, 0, 1, 0, 0);
361
+ context.clearRect(0, 0, texture.width + 1, texture.height + 1);
362
+ context.drawImage(texture, 0, 0, texture.width, texture.height);
363
+ canvas.width = width;
364
+ canvas.height = height;
365
+ const ctx = canvas.getContext("2d");
366
+ if (!ctx) {
367
+ return;
368
+ }
369
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
370
+ ctx.clearRect(0, 0, width, height);
371
+ ctx.drawImage(player.canvas, 0, 0);
372
+ if (currentAttachment) {
373
+ context._$bind(currentAttachment);
374
+ }
375
+ if (callback) {
376
+ callback(canvas);
377
+ }
378
+ }
379
+ if (matrix) {
380
+ $poolMatrix(matrix);
381
+ }
382
+ if (color_transform) {
383
+ $poolColorTransform(color_transform);
384
+ }
385
+ }
386
+ }