Skip to content

Commit 9a835b8

Browse files
committed
Bug fixes.
1 parent 4134cb1 commit 9a835b8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { getGuides } from './utils/getGuides';
1818

1919
type Pixel = { x: number, y: number };
2020

21+
type Color = [number, number, number, number];
22+
2123
enum HistoryType {
2224
Group,
2325
Pixel,
@@ -44,7 +46,7 @@ type HistoryPixelType = {
4446
}
4547

4648
type HistoryColorUpdateType = {
47-
color: [number, number, number, number],
49+
color: Color,
4850
index: number
4951
}
5052

@@ -61,6 +63,10 @@ type Layer = {
6163
export: boolean
6264
}
6365

66+
function toColor([r, g, b, a]: Color) {
67+
return `rgba(${r}, ${g}, ${b}, ${a})`;
68+
}
69+
6470
@Component({
6571
selector: 'pg-input-pixel-editor',
6672
style,
@@ -94,7 +100,10 @@ export default class PgInputPixelEditor extends HTMLElement {
94100
#undoHistory: History[] = [];
95101
#redoHistory: History[] = [];
96102
#context: CanvasRenderingContext2D;
97-
#colors: string[] = ['transparent', '#000'];
103+
#colors: Color[] = [
104+
[0, 0, 0, 0],
105+
[0, 0, 0, 1]
106+
];
98107
#baseLayer: HTMLCanvasElement;
99108
#baseLayerContext: CanvasRenderingContext2D;
100109
#editLayer: HTMLCanvasElement;
@@ -261,11 +270,19 @@ export default class PgInputPixelEditor extends HTMLElement {
261270
this.size + (this.gridSize * 2) - 2,
262271
this.size + (this.gridSize * 2) - 2
263272
);
264-
this.#editLayerContext.fillStyle = this.#colors[color] === 'transparent' ? WHITE : this.#colors[color];
265-
this.#editLayerContext.fillRect(x * totalSize + 1, y * totalSize + 1, this.size - 2, this.size - 2);
266-
// No Edit Layer
267-
this.#noEditLayerContext.fillStyle = this.#colors[color] === 'transparent' ? WHITE : this.#colors[color];
268-
this.#noEditLayerContext.fillRect(x * totalSize, y * totalSize, this.size, this.size);
273+
this.#context.clearRect(x * totalSize, y * totalSize, this.size, this.size);
274+
this.#editLayerContext.clearRect(x * totalSize, y * totalSize, this.size, this.size);
275+
this.#noEditLayerContext.clearRect(x * totalSize, y * totalSize, this.size, this.size);
276+
// Edit layer
277+
if (this.#colors[color][3] !== 0) {
278+
this.#editLayerContext.fillStyle = toColor(this.#colors[color]);
279+
this.#editLayerContext.fillRect(x * totalSize + 1, y * totalSize + 1, this.size - 2, this.size - 2);
280+
}
281+
// No Edit layer
282+
if (this.#colors[color][3] !== 0) {
283+
this.#noEditLayerContext.fillStyle = toColor(this.#colors[color]);
284+
this.#noEditLayerContext.fillRect(x * totalSize, y * totalSize, this.size, this.size);
285+
}
269286
// base layer to main canvas
270287
this.#context.drawImage(
271288
this.#baseLayer,

0 commit comments

Comments
 (0)