Merge pull request #25 from leomurca/feature/download_image

Add download button for each design
This commit is contained in:
Leonardo Murça 2023-01-29 21:25:06 -03:00 committed by GitHub
commit 067f2fa122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "embroidery-viewer", "name": "embroidery-viewer",
"private": true, "private": true,
"version": "1.2.0", "version": "1.2.1",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View file

@ -7,6 +7,23 @@
let stitchesRefs = []; let stitchesRefs = [];
let sizeRefs = []; let sizeRefs = [];
let errorMessageRef; let errorMessageRef;
const downloadCanvasAsImage = (canvas, filename) => {
const image = canvas
.toDataURL("image/png")
.replace("image/png", "image/octet-stream");
const link = document.createElement("a");
link.download = `${filename.split(".").slice(0, -1).join(".")}.png`;
link.href = image;
link.click();
};
const onKeydown = (evt) => {
if (evt.key === "Enter") {
document.getElementById("download-button").click();
}
};
</script> </script>
{#if files.length !== 0} {#if files.length !== 0}
@ -18,6 +35,14 @@
<div class="stitches-container" bind:this={stitchesRefs[i]} /> <div class="stitches-container" bind:this={stitchesRefs[i]} />
<div class="size-container" bind:this={sizeRefs[i]} /> <div class="size-container" bind:this={sizeRefs[i]} />
<div class="colors-container" bind:this={colorRefs[i]} /> <div class="colors-container" bind:this={colorRefs[i]} />
<div
id="download-button"
role="button"
on:keydown={onKeydown}
on:click={() => downloadCanvasAsImage(canvasRefs[i], file.name)}
>
Download
</div>
</div> </div>
{canvasRefs[i] && {canvasRefs[i] &&
renderFileToCanvas( renderFileToCanvas(
@ -56,7 +81,7 @@
} }
.canvas { .canvas {
height: 80%; height: 70%;
width: 100%; width: 100%;
object-fit: contain; object-fit: contain;
} }
@ -67,12 +92,27 @@
flex-wrap: wrap; flex-wrap: wrap;
gap: 5px; gap: 5px;
row-gap: 5px; row-gap: 5px;
padding-bottom: 15px;
} }
.stitches-container { .stitches-container {
padding: 10px 0; padding: 10px 0;
} }
div[role="button"] {
background-color: #05345f;
font-weight: 500;
color: white;
padding: 10px;
border-radius: 0;
}
div[role="button"]:hover {
cursor: pointer;
background-color: black;
color: white;
}
@media only screen and (max-device-width: 812px) { @media only screen and (max-device-width: 812px) {
.canvas-container { .canvas-container {
width: 100%; width: 100%;