mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-26 12:01:45 +00:00
get image placement data from a submit form
This commit is contained in:
parent
5248c09267
commit
688447e63c
@ -15,19 +15,27 @@
|
|||||||
interval: NodeJS.Timeout;
|
interval: NodeJS.Timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SubmitData {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
w: number;
|
||||||
|
h: number;
|
||||||
|
}
|
||||||
|
|
||||||
let imageURL: string | null, lastImage: File;
|
let imageURL: string | null, lastImage: File;
|
||||||
let uploadData: UploadData, matrix: Matrix;
|
let uploadData: UploadData, matrix: Matrix;
|
||||||
let uploadStarted = false;
|
let uploadStarted = false,
|
||||||
|
submitData: SubmitData;
|
||||||
|
|
||||||
async function updateMatrix() {
|
async function updateMatrix() {
|
||||||
await fetch('/api/redirect?endpoint=update');
|
await fetch('/api/redirect?endpoint=update');
|
||||||
clearInterval(uploadData.interval);
|
clearInterval(uploadData.interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function placeImage() {
|
async function placeImage(data: SubmitData) {
|
||||||
const fdata = new FormData();
|
const fdata = new FormData();
|
||||||
fdata.append('x', '0');
|
fdata.append('x', data.x.toString());
|
||||||
fdata.append('y', '0');
|
fdata.append('y', data.y.toString());
|
||||||
await post(fdata, 'image');
|
await post(fdata, 'image');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +59,7 @@
|
|||||||
if (!mdata.success) alert(`Error while processing '${endpoint}'!`);
|
if (!mdata.success) alert(`Error while processing '${endpoint}'!`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fileAsDataURL(file: File, width: number, height: number): Promise<string> {
|
async function fileAsDataURL(file: File, data: SubmitData): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
@ -59,11 +67,11 @@
|
|||||||
const context = canvas.getContext('2d');
|
const context = canvas.getContext('2d');
|
||||||
|
|
||||||
// Set the canvas dimensions
|
// Set the canvas dimensions
|
||||||
canvas.width = width;
|
canvas.width = data.w;
|
||||||
canvas.height = height;
|
canvas.height = data.h;
|
||||||
|
|
||||||
// Draw the image onto the canvas, resizing it to fit the canvas
|
// Draw the image onto the canvas, resizing it to fit the canvas
|
||||||
context?.drawImage(img, 0, 0, width, height);
|
context?.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
// Convert the canvas image to a Data URL
|
// Convert the canvas image to a Data URL
|
||||||
const dataURL = canvas.toDataURL();
|
const dataURL = canvas.toDataURL();
|
||||||
@ -77,7 +85,8 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadImage(event: Event) {
|
function getImage(event: Event) {
|
||||||
|
uploadStarted = false;
|
||||||
// Revoke previous image url, if any
|
// Revoke previous image url, if any
|
||||||
if (imageURL) {
|
if (imageURL) {
|
||||||
URL.revokeObjectURL(imageURL);
|
URL.revokeObjectURL(imageURL);
|
||||||
@ -92,6 +101,18 @@
|
|||||||
// Load image data and file
|
// Load image data and file
|
||||||
lastImage = file;
|
lastImage = file;
|
||||||
imageURL = URL.createObjectURL(file);
|
imageURL = URL.createObjectURL(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSubmit(event: SubmitEvent) {
|
||||||
|
// Get image data from submit
|
||||||
|
const form = event.target as HTMLFormElement;
|
||||||
|
const fdata = new FormData(form);
|
||||||
|
submitData = {
|
||||||
|
x: parseInt(fdata.get('x')?.toString() ?? '0'),
|
||||||
|
y: parseInt(fdata.get('y')?.toString() ?? '0'),
|
||||||
|
w: parseInt(fdata.get('w')?.toString() ?? matrix.width.toString()),
|
||||||
|
h: parseInt(fdata.get('h')?.toString() ?? matrix.height.toString())
|
||||||
|
};
|
||||||
|
|
||||||
// Time upload by saving date
|
// Time upload by saving date
|
||||||
uploadData = {
|
uploadData = {
|
||||||
@ -105,6 +126,9 @@
|
|||||||
start: performance.now(),
|
start: performance.now(),
|
||||||
elapsed: '0'
|
elapsed: '0'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Start upload process
|
||||||
|
uploadStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
@ -115,11 +139,75 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<input type="file" name="image" accept="image/*" on:change={uploadImage} />
|
<form on:submit|preventDefault={handleSubmit}>
|
||||||
{#if imageURL}
|
<label for="x">Set X coordinate:</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
name="x"
|
||||||
|
id="x"
|
||||||
|
min="0"
|
||||||
|
max={(matrix?.width ?? 192) - 1}
|
||||||
|
step="1"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label for="y">Set Y coordinate:</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
name="y"
|
||||||
|
id="y"
|
||||||
|
min="0"
|
||||||
|
max={(matrix?.height ?? 192) - 1}
|
||||||
|
step="1"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label for="w">Set image width:</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
name="w"
|
||||||
|
id="w"
|
||||||
|
min="1"
|
||||||
|
max={matrix?.width ?? 192}
|
||||||
|
step="1"
|
||||||
|
value={matrix?.width ?? 192}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label for="h">Set image height:</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
name="h"
|
||||||
|
id="h"
|
||||||
|
min="1"
|
||||||
|
max={matrix?.height ?? 192}
|
||||||
|
step="1"
|
||||||
|
value={matrix?.height ?? 192}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label for="image">Set an image:</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
name="image"
|
||||||
|
id="image"
|
||||||
|
accept="image/*"
|
||||||
|
required
|
||||||
|
on:change={getImage}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<input
|
||||||
|
type="submit"
|
||||||
|
value="Upload!"
|
||||||
|
/>
|
||||||
|
{#if imageURL}
|
||||||
<img src={imageURL} alt="User uploaded" />
|
<img src={imageURL} alt="User uploaded" />
|
||||||
{#if uploadStarted}
|
{/if}
|
||||||
{#await fileAsDataURL(lastImage, matrix.width, matrix.height)}
|
</form>
|
||||||
|
{#if uploadStarted}
|
||||||
|
{#await fileAsDataURL(lastImage, submitData)}
|
||||||
<p>Loading image data . . .</p>
|
<p>Loading image data . . .</p>
|
||||||
<p>{uploadData.elapsed} seconds elapsed.</p>
|
<p>{uploadData.elapsed} seconds elapsed.</p>
|
||||||
{:then dataUrl}
|
{:then dataUrl}
|
||||||
@ -127,7 +215,7 @@
|
|||||||
<p>Sending image . . .</p>
|
<p>Sending image . . .</p>
|
||||||
<p>{uploadData.elapsed} seconds elapsed.</p>
|
<p>{uploadData.elapsed} seconds elapsed.</p>
|
||||||
{:then}
|
{:then}
|
||||||
{#await placeImage()}
|
{#await placeImage(submitData)}
|
||||||
<p>Placing image . . .</p>
|
<p>Placing image . . .</p>
|
||||||
<p>{uploadData.elapsed} seconds elapsed.</p>
|
<p>{uploadData.elapsed} seconds elapsed.</p>
|
||||||
{:then}
|
{:then}
|
||||||
@ -141,5 +229,4 @@
|
|||||||
{/await}
|
{/await}
|
||||||
{/await}
|
{/await}
|
||||||
{/await}
|
{/await}
|
||||||
{/if}
|
|
||||||
{/if}
|
{/if}
|
||||||
|
Loading…
Reference in New Issue
Block a user