canvas-editor-engine 1.1.9 → 1.1.11

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.
@@ -25,3 +25,9 @@ export type TFilterMethod = 'pixel';
25
25
  export interface IFilterOptions {
26
26
  quality: number;
27
27
  }
28
+ export declare const enum E_RGBA {
29
+ r = 0,
30
+ g = 1,
31
+ b = 2,
32
+ a = 3
33
+ }
@@ -6,3 +6,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  ;
7
7
  ;
8
8
  ;
9
+ ;
@@ -1,4 +1,5 @@
1
- import type { IImageOptions, IImageSize, TBuff, TRGBABuff } from "../types/image";
1
+ import { type IImageOptions, type IImageSize, type TBuff, type TRGBABuff } from "../types/image";
2
+ import type { ISize } from '../types/general';
2
3
  export declare class Filter {
3
4
  ctx: CanvasRenderingContext2D;
4
5
  imageSize: IImageSize;
@@ -6,7 +7,8 @@ export declare class Filter {
6
7
  setImageSize(size: IImageSize): void;
7
8
  copy(options: IImageOptions): ImageData;
8
9
  update(imgData: ImageData, options: IImageOptions): void;
9
- clearEmptyPixels(imageData: ImageData): void;
10
+ clearEmptyPixels(imageData: ImageData): ImageData;
11
+ getSizeOfSparseMatrix(RGBAMatrix: TBuff<number[]>, tempSize: ISize): ISize;
10
12
  getBuffCollection(imageData: ImageData): {
11
13
  rowRGBABuff: TRGBABuff;
12
14
  hexBuff: `#${string}`[];
@@ -18,7 +18,7 @@ class Filter {
18
18
  const height = (options?.height) ? options.height : config_1.default.CANVAS_SIZE.height;
19
19
  const imgData = this.ctx.getImageData(options.x, options.y, width, height);
20
20
  const clearedImageData = this.clearEmptyPixels(imgData);
21
- return imgData;
21
+ return clearedImageData;
22
22
  }
23
23
  update(imgData, options) {
24
24
  this.ctx.clearRect(0, 0, config_1.default.CANVAS_SIZE.width, config_1.default.CANVAS_SIZE.height);
@@ -27,11 +27,24 @@ class Filter {
27
27
  clearEmptyPixels(imageData) {
28
28
  const rowRGBABuff = this.getRowRGBABuff(imageData);
29
29
  const RGBAMatrix = this.getRGBAMatrix(rowRGBABuff, { width: imageData.width, height: imageData.height });
30
- const tempSize = {
30
+ const beforeSize = {
31
31
  width: imageData.width,
32
32
  height: imageData.height,
33
33
  };
34
- ;
34
+ const tempSize = this.getSizeOfSparseMatrix(RGBAMatrix, beforeSize);
35
+ const cleared = rowRGBABuff.filter((byteArray) => {
36
+ const alpha = byteArray[3 /* E_RGBA.a */];
37
+ return !!alpha;
38
+ });
39
+ const clearedBuff = cleared.flat();
40
+ const tempCanvas = document.createElement('canvas');
41
+ const tempCtx = tempCanvas.getContext('2d');
42
+ const tempImageData = tempCtx.createImageData(tempSize.width, tempSize.height);
43
+ clearedBuff.forEach((_, i) => tempImageData.data[i] = clearedBuff[i]);
44
+ console.log('cleared', cleared);
45
+ return tempImageData;
46
+ }
47
+ getSizeOfSparseMatrix(RGBAMatrix, tempSize) {
35
48
  // case 1: Xcanvas -> Ximage && case 3: Ycanvas -> Yimage
36
49
  let leftIndex;
37
50
  let upIndex;
@@ -68,17 +81,13 @@ class Filter {
68
81
  }
69
82
  console.log('case 2: Ximage <- Xcanvas; rightIndex:', rightIndex);
70
83
  console.log('case 4: Yimage <- Ycanvas; downIndex:', downIndex);
71
- // const cleared = rowRGBABuff.filter((byteArray) => {
72
- // const alpha = byteArray[E_RGBA.a];
73
- // return !!alpha;
74
- // });
75
- // const clearedBuff = cleared.flat();
76
- // const tempCanvas = document.createElement('canvas');
77
- // const tempCtx = tempCanvas.getContext('2d');
78
- // const tempImageData = tempCtx.createImageData(tempSize.width, tempSize.height);
79
- // clearedBuff.forEach((_, i) => tempImageData.data[i] = clearedBuff[i]);
80
- // console.log('cleared', cleared);
81
- // return tempImageData;
84
+ const reduceWidth = (tempWidth) => tempWidth - (leftIndex + rightIndex);
85
+ const reduceHeight = (tempHeight) => tempHeight - (upIndex + downIndex);
86
+ const result = {
87
+ width: reduceWidth(tempSize.width),
88
+ height: reduceHeight(tempSize.height),
89
+ };
90
+ return result;
82
91
  }
83
92
  getBuffCollection(imageData) {
84
93
  const rowRGBABuff = this.getRowRGBABuff(imageData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-editor-engine",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "CanvasEditorEngine library, use: [typescript] [canvas]",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",