|
| 1 | +<script lang="ts"> |
| 2 | + import settings from "$lib/state/settings"; |
| 3 | +
|
| 4 | + import { onMount } from "svelte"; |
| 5 | + import { goto } from "$app/navigation"; |
| 6 | +
|
| 7 | + import { downloadFile } from "$lib/download"; |
| 8 | + import { removeImageBackground } from "$lib/workers/removebg"; |
| 9 | +
|
| 10 | + import DropReceiver from "$components/misc/DropReceiver.svelte"; |
| 11 | + import FileReceiver from "$components/misc/FileReceiver.svelte"; |
| 12 | + import Skeleton from "$components/misc/Skeleton.svelte"; |
| 13 | +
|
| 14 | + let imageContainer: HTMLElement; |
| 15 | +
|
| 16 | + let draggedOver = false; |
| 17 | + let file: File | undefined; |
| 18 | +
|
| 19 | + let thinking = false; |
| 20 | + let done = false; |
| 21 | +
|
| 22 | + let result: HTMLCanvasElement; |
| 23 | +
|
| 24 | + const processImage = async () => { |
| 25 | + if (file) { |
| 26 | + thinking = true; |
| 27 | + const removedBackground = await removeImageBackground(file); |
| 28 | + if (removedBackground) { |
| 29 | + thinking = false; |
| 30 | + done = true; |
| 31 | + result = removedBackground; |
| 32 | + imageContainer.append(removedBackground); |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | +
|
| 37 | + const exportImage = async () => { |
| 38 | + result.toBlob(async (blob) => { |
| 39 | + if (!blob || !file) return; |
| 40 | + return await downloadFile({ |
| 41 | + file: new File([blob], `${file.name} (cutout).png`, { |
| 42 | + type: "image/png", |
| 43 | + }), |
| 44 | + }); |
| 45 | + }, "image/png"); |
| 46 | + }; |
| 47 | +
|
| 48 | + onMount(() => { |
| 49 | + if (!($settings.advanced.duck && $settings.advanced.debug)) { |
| 50 | + goto("/", { replaceState: true }); |
| 51 | + } |
| 52 | + }); |
| 53 | +</script> |
| 54 | + |
| 55 | +<DropReceiver bind:file bind:draggedOver id="cutout-container"> |
| 56 | + {#if !thinking && !done} |
| 57 | + <FileReceiver |
| 58 | + bind:draggedOver |
| 59 | + bind:file |
| 60 | + acceptTypes={["image/*"]} |
| 61 | + acceptExtensions={["jpg", "png", "webp"]} |
| 62 | + /> |
| 63 | + <div class="subtext early-note"> |
| 64 | + this is a very early & basic proof-of-concept, nothing about this feature is final or complete. please don't share or talk about it. |
| 65 | + </div> |
| 66 | + {#if file} |
| 67 | + <button on:click={processImage}>process imported stuff</button> |
| 68 | + {/if} |
| 69 | + {/if} |
| 70 | + |
| 71 | + {#if thinking} |
| 72 | + <div>thinking very hard rn...</div> |
| 73 | + {/if} |
| 74 | + |
| 75 | + {#if done} |
| 76 | + <div>thought a lot, here's what i got:</div> |
| 77 | + {/if} |
| 78 | + |
| 79 | + {#if thinking || done} |
| 80 | + <div id="image-preview" bind:this={imageContainer}> |
| 81 | + {#if !done} |
| 82 | + <Skeleton width="100%" height="100%" class="big" /> |
| 83 | + {/if} |
| 84 | + </div> |
| 85 | + {/if} |
| 86 | + |
| 87 | + {#if done} |
| 88 | + <div id="finished-actions"> |
| 89 | + <button |
| 90 | + on:click={() => { |
| 91 | + done = false; |
| 92 | + file = undefined; |
| 93 | + }} |
| 94 | + > |
| 95 | + start over |
| 96 | + </button> |
| 97 | + <button on:click={exportImage}>save the result</button> |
| 98 | + </div> |
| 99 | + {/if} |
| 100 | +</DropReceiver> |
| 101 | + |
| 102 | +<style> |
| 103 | + :global(#cutout-container) { |
| 104 | + display: flex; |
| 105 | + justify-content: center; |
| 106 | + align-items: center; |
| 107 | + flex-direction: column; |
| 108 | + width: 100%; |
| 109 | + gap: 18px; |
| 110 | + } |
| 111 | +
|
| 112 | + .early-note { |
| 113 | + max-width: 400px; |
| 114 | + text-align: center; |
| 115 | + } |
| 116 | +
|
| 117 | + #image-preview { |
| 118 | + background: var(--button); |
| 119 | + display: flex; |
| 120 | + flex-direction: column; |
| 121 | + justify-content: center; |
| 122 | + align-items: center; |
| 123 | + width: 720px; |
| 124 | + height: 480px; |
| 125 | + overflow: hidden; |
| 126 | + border-radius: var(--border-radius); |
| 127 | + box-shadow: var(--button-box-shadow); |
| 128 | + } |
| 129 | +
|
| 130 | + #finished-actions { |
| 131 | + display: flex; |
| 132 | + flex-direction: row; |
| 133 | + gap: 6px; |
| 134 | + } |
| 135 | +
|
| 136 | + button { |
| 137 | + padding: 12px 24px; |
| 138 | + border-radius: 200px; |
| 139 | + } |
| 140 | +
|
| 141 | + :global(canvas) { |
| 142 | + height: 100%; |
| 143 | + } |
| 144 | +</style> |
0 commit comments