Skip to content

Commit f7c27bc

Browse files
committed
Progress on export layer performance.
1 parent 866af6b commit f7c27bc

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/pg/inputPixelEditor/__examples__/basic/basic.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ export default class XPgInputPixelEditorBasic extends HTMLElement {
271271
}
272272

273273
handleChange(e: CustomEvent) {
274-
const { value } = e.detail;
275-
this.$value1.textContent = value.join('--');
274+
this.$value1.textContent = this.$input.getLayerPaths().join('--');
276275
}
277276

278277
handleInput(e: CustomEvent) {

src/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export default class PgInputPixelEditor extends HTMLElement {
112112
#isShift: boolean = false;
113113
#isAlt: boolean = false;
114114
#data: number[][][] = [];
115+
#export: number[][] = [];
115116
#undoHistory: History[] = [];
116117
#redoHistory: History[] = [];
117118
#context: CanvasRenderingContext2D;
@@ -205,6 +206,7 @@ export default class PgInputPixelEditor extends HTMLElement {
205206
opacity: 1
206207
}];
207208
this.#data = [fillGrid(this.width, this.height)];
209+
this.#export = fillGrid(this.width, this.height);
208210
this.#reset = false;
209211
this.#undoHistory = [];
210212
this.#redoHistory = [];
@@ -241,14 +243,11 @@ export default class PgInputPixelEditor extends HTMLElement {
241243
}
242244

243245
#handleChange() {
244-
const paths = this.#data.map(layer => bitmaskToPath(layer, { scale: 1 }));
245-
console.log('change:', paths);
246+
// Due to perf, don't compute anything here
247+
// use this.$input.get... methods
246248
this.dispatchEvent(new CustomEvent('change', {
247-
detail: { value: paths }
249+
detail: { export: this.#export }
248250
}));
249-
/*this.dispatchEvent(new CustomEvent('change', {
250-
detail: data
251-
}));*/
252251
};
253252

254253
#delayTimerId: number = 0;
@@ -302,6 +301,7 @@ export default class PgInputPixelEditor extends HTMLElement {
302301
);
303302
// Verify this is the only place setting pixel data!
304303
this.#data[this.#layer][y][x] = color;
304+
this.#updateExport(x, y);
305305
this.#delayedChange();
306306
}
307307

@@ -313,6 +313,28 @@ export default class PgInputPixelEditor extends HTMLElement {
313313
}
314314
}
315315

316+
/**
317+
* Update cached export grid for performance.
318+
* @param x X
319+
* @param y Y
320+
*/
321+
#updateExport(x: number, y: number) {
322+
let color = 0;
323+
let layers = this.#layers.reduce((acc: any, layer: any, index: number) => {
324+
if (layer.export) {
325+
acc.push(index);
326+
}
327+
}, []);
328+
for (let i = 0; i < layers.length; i++) {
329+
const layer = layers[i];
330+
if (this.#data[layer][y][x] !== 0) {
331+
color = this.#data[layer][y][x];
332+
break;
333+
}
334+
}
335+
this.#export[y][x] = color;
336+
}
337+
316338
#setPreview(pixels: Pixel[], previousX: number, previousY: number) {
317339
const totalSize = this.size + this.gridSize;
318340
const actualWidth = this.width * totalSize - this.gridSize;
@@ -762,6 +784,7 @@ export default class PgInputPixelEditor extends HTMLElement {
762784
}
763785
});
764786
this.#data = [fillGrid(this.width, this.height)];
787+
this.#export = fillGrid(this.width, this.height);
765788
this.#updateGrid();
766789
}
767790

@@ -1021,6 +1044,13 @@ export default class PgInputPixelEditor extends HTMLElement {
10211044

10221045
addLayer() {
10231046
this.#data.push(fillGrid(this.width, this.height));
1047+
this.#layers.push({
1048+
name: 'Layer 1',
1049+
export: true,
1050+
locked: false,
1051+
visible: true,
1052+
opacity: 1
1053+
});
10241054
}
10251055

10261056
/**
@@ -1038,4 +1068,12 @@ export default class PgInputPixelEditor extends HTMLElement {
10381068

10391069
}
10401070

1071+
getLayerPaths() {
1072+
return this.#data.map(layer => bitmaskToPath(layer, { scale: 1 }));
1073+
}
1074+
1075+
getExportPath() {
1076+
return bitmaskToPath(this.#export, { scale: 1 });
1077+
}
1078+
10411079
}

0 commit comments

Comments
 (0)