fix: async draw

This commit is contained in:
2025-12-20 21:03:27 +01:00
parent d02f66b4bd
commit 8b6d4dbf90
2 changed files with 30 additions and 14 deletions
+1
View File
@@ -26,6 +26,7 @@ dev: node_modules ## run dev version of static site
lint: node_modules ## lint code lint: node_modules ## lint code
@$(BUN) run lint @$(BUN) run lint
@$(BUN) run type-check
fix: node_modules ## fix and reformat code fix: node_modules ## fix and reformat code
@$(BUN) run format @$(BUN) run format
+29 -14
View File
@@ -75,12 +75,12 @@ function randomElement<T>(items: T[]): T {
function randomize() { function randomize() {
centerX.value = Math.random(); centerX.value = Math.random();
centerY.value = Math.random(); centerY.value = Math.random();
zoom.value = 1 + Math.random() * 3; zoom.value = 1 + Math.random() * 2;
filter.value = randomElement(Object.values(Filter)); filter.value = randomElement(Object.values(Filter));
paPos.value = randomElement(Object.values(PAPosition)); paPos.value = randomElement(Object.values(PAPosition));
paScale.value = 0.1 + Math.random() * 0.2; paScale.value = 0.1 + Math.random() * 0.2;
paMargin.value = Math.random() * 0.1; paMargin.value = Math.random() * 0.1;
draw(); asyncDraw();
} }
function reset() { function reset() {
@@ -91,7 +91,7 @@ function reset() {
paPos.value = PAPosition.BR; paPos.value = PAPosition.BR;
paScale.value = 0.2; paScale.value = 0.2;
paMargin.value = 0.05; paMargin.value = 0.05;
draw(); asyncDraw();
} }
function newImage() { function newImage() {
@@ -128,6 +128,13 @@ function applyFilter(
return [r, g, b]; return [r, g, b];
} }
const drawTimeout = ref<number | undefined>(undefined);
function asyncDraw() {
clearTimeout(drawTimeout.value);
drawTimeout.value = setTimeout(draw);
}
function draw() { function draw() {
if (!canvas.value) { if (!canvas.value) {
return; return;
@@ -155,9 +162,9 @@ function draw() {
// @ts-expect-error: safer to cast // @ts-expect-error: safer to cast
const pixelData = imageData.data as number[]; const pixelData = imageData.data as number[];
for (let i = 0; i < pixelData.length; i += 4) { for (let i = 0; i < pixelData.length; i += 4) {
const r1 = pixelData[i]; const r1 = pixelData[i]!;
const g1 = pixelData[i + 1]; const g1 = pixelData[i + 1]!;
const b1 = pixelData[i + 2]; const b1 = pixelData[i + 2]!;
const [r2, g2, b2] = applyFilter(filter.value, r1, g1, b1); const [r2, g2, b2] = applyFilter(filter.value, r1, g1, b1);
@@ -265,7 +272,7 @@ onMounted(() => {
min="0" min="0"
max="1" max="1"
step="0.01" step="0.01"
@input="draw" @input="asyncDraw"
/> />
</td> </td>
<td>{{ (centerX * 100).toFixed(0) }}%</td> <td>{{ (centerX * 100).toFixed(0) }}%</td>
@@ -280,7 +287,7 @@ onMounted(() => {
min="0" min="0"
max="1" max="1"
step="0.01" step="0.01"
@input="draw" @input="asyncDraw"
/> />
</td> </td>
<td>{{ (centerY * 100).toFixed(0) }}%</td> <td>{{ (centerY * 100).toFixed(0) }}%</td>
@@ -293,9 +300,9 @@ onMounted(() => {
v-model="zoom" v-model="zoom"
type="range" type="range"
min="1" min="1"
max="4" max="3"
step="0.01" step="0.01"
@input="draw" @input="asyncDraw"
/> />
</td> </td>
<td>{{ (zoom * 100).toFixed(0) }}%</td> <td>{{ (zoom * 100).toFixed(0) }}%</td>
@@ -303,7 +310,11 @@ onMounted(() => {
<tr> <tr>
<td><label for="filter">Filter:</label></td> <td><label for="filter">Filter:</label></td>
<td> <td>
<select id="filter" v-model="filter" @change="draw"> <select
id="filter"
v-model="filter"
@change="asyncDraw"
>
<option <option
v-for="value in Object.values(Filter)" v-for="value in Object.values(Filter)"
:key="`filter-${value}`" :key="`filter-${value}`"
@@ -317,7 +328,11 @@ onMounted(() => {
<tr> <tr>
<td><label for="pa-pos">Sticker Position:</label></td> <td><label for="pa-pos">Sticker Position:</label></td>
<td> <td>
<select id="pa-pos" v-model="paPos" @change="draw"> <select
id="pa-pos"
v-model="paPos"
@change="asyncDraw"
>
<option <option
v-for="value in Object.values(PAPosition)" v-for="value in Object.values(PAPosition)"
:key="`pa-pos-${value}`" :key="`pa-pos-${value}`"
@@ -338,7 +353,7 @@ onMounted(() => {
min="0.10" min="0.10"
max="0.30" max="0.30"
step="0.01" step="0.01"
@input="draw" @input="asyncDraw"
/> />
</td> </td>
<td>{{ (paScale * 100).toFixed(0) }}%</td> <td>{{ (paScale * 100).toFixed(0) }}%</td>
@@ -353,7 +368,7 @@ onMounted(() => {
min="0" min="0"
max="0.1" max="0.1"
step="0.01" step="0.01"
@input="draw" @input="asyncDraw"
/> />
</td> </td>
<td>{{ (paMargin * 100).toFixed(0) }}%</td> <td>{{ (paMargin * 100).toFixed(0) }}%</td>