Skip to content

Commit 492efd4

Browse files
committed
Add resize handles.
1 parent 79066b3 commit 492efd4

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/pg/inputPixelEditor/__examples__/basic/basic.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<div class="example">
2+
<div>
3+
<input part="width" type="range" min="10" max="400">
4+
<input part="height" type="range" min="10" max="240">
5+
<input part="size" type="range" min="4" max="16">
6+
</div>
27
<pg-input-pixel-editor part="input" width="10" height="10" size="10" style="box-shadow:0 0 0.5rem rgba(0, 0, 0, 0.2)"></pg-input-pixel-editor>
38
<div>
49
<code>onchange: <span part="value1"></span></code>
510
</div>
611
<div>
712
<code>oninput: <span part="value2"></span></code>
813
</div>
14+
915
<div>
1016
<button part="clear">clear</button>
1117
<button part="modePixel">Pixel</button>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export default class XPgInputPixelEditorBasic extends HTMLElement {
1515
@Part() $value1: HTMLSpanElement;
1616
@Part() $value2: HTMLSpanElement;
1717
@Part() $debug: HTMLDivElement;
18+
@Part() $width: HTMLInputElement;
19+
@Part() $height: HTMLInputElement;
20+
@Part() $size: HTMLInputElement;
1821

1922
@Part() $clear: HTMLButtonElement;
2023
@Part() $modePixel: HTMLButtonElement;
@@ -25,7 +28,13 @@ export default class XPgInputPixelEditorBasic extends HTMLElement {
2528
@Part() $modeEllipseOutline: HTMLButtonElement;
2629

2730
connectedCallback() {
31+
this.$width.value = '10';
32+
this.$height.value = '10';
33+
this.$size.value = '10';
2834
this.$input.addEventListener('change', this.handleChange.bind(this));
35+
this.$width.addEventListener('change', this.handleWidthChange.bind(this));
36+
this.$height.addEventListener('change', this.handleHeightChange.bind(this));
37+
this.$size.addEventListener('change', this.handleSizeChange.bind(this));
2938
this.$input.addEventListener('input', this.handleInput.bind(this));
3039
this.$input.addEventListener('debug', this.handleDebug.bind(this));
3140
this.$modePixel.addEventListener('click', () => {
@@ -71,4 +80,19 @@ export default class XPgInputPixelEditorBasic extends HTMLElement {
7180
//context.lineWidth = 1;
7281
//context.strokeRect(x, y, width, height);
7382
}
83+
84+
handleWidthChange(e) {
85+
this.$input.width = e.target.value;
86+
this.$debug.innerHTML = '';
87+
}
88+
89+
handleHeightChange(e) {
90+
this.$input.height = e.target.value;
91+
this.$debug.innerHTML = '';
92+
}
93+
94+
handleSizeChange(e) {
95+
this.$input.size = e.target.value;
96+
this.$debug.innerHTML = '';
97+
}
7498
}

src/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import iterateGrid from './utils/interateGrid';
1414
import bitmaskToPath from './utils/bitmapToMask';
1515
import createLayer from './utils/createLayer';
1616
import diffGrid from './utils/diffGrid';
17+
import { getGuides } from './utils/getGuides';
1718

1819
type Pixel = { x: number, y: number };
1920

@@ -152,25 +153,39 @@ export default class PgInputPixelEditor extends HTMLElement {
152153
}
153154
}
154155

156+
#reset = true;
155157
#init() {
156158
const totalSize = this.size + this.gridSize;
157159
const actualWidth = this.width * totalSize - this.gridSize;
158160
const actualHeight = this.height * totalSize - this.gridSize;
159161
this.$canvas.width = actualWidth;
160162
this.$canvas.height = actualHeight;
161-
this.#layer = 0;
162-
this.#layers = [{
163-
name: 'Layer 1',
164-
export: true,
165-
locked: false,
166-
visible: true,
167-
opacity: 1
168-
}];
169-
this.#data = [fillGrid(this.width, this.height)];
170163
[this.#baseLayer, this.#baseLayerContext] = createLayer(actualWidth, actualHeight);
171164
[this.#editLayer, this.#editLayerContext] = createLayer(actualWidth, actualHeight);
172165
[this.#noEditLayer, this.#noEditLayerContext] = createLayer(actualWidth, actualHeight);
173166
[this.#previewLayer, this.#previewLayerContext] = createLayer(actualWidth, actualHeight);
167+
if (this.#reset) {
168+
this.#layer = 0;
169+
this.#layers = [{
170+
name: 'Layer 1',
171+
export: true,
172+
locked: false,
173+
visible: true,
174+
opacity: 1
175+
}];
176+
this.#data = [fillGrid(this.width, this.height)];
177+
this.#reset = false;
178+
}
179+
this.#redraw();
180+
}
181+
182+
#redraw() {
183+
// Render individual pixels
184+
this.#data[this.#layer].forEach((row, y) => {
185+
row.forEach((cell, x) => {
186+
this.#setPixel(x, y, cell);
187+
});
188+
});
174189
}
175190

176191
#handleChange() {
@@ -579,6 +594,13 @@ export default class PgInputPixelEditor extends HTMLElement {
579594
cloned[y][x] = cloned[y][x] === 0 ? 1 : 0;
580595
});
581596
this.#data[this.#layer] = cloned;
597+
}
598+
applyGuides() {
599+
const guides = getGuides(this.width, this.height, this.size, this.gridSize);
600+
this.#baseLayerContext.drawImage(guides, 0, 0);
601+
}
602+
clearGuides() {
603+
582604
}
583605
undo() {
584606
// ToDo: Rewrite to use new history api

0 commit comments

Comments
 (0)