|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ImageToAsciiArt } from 'image-to-ascii-art'; |
| 3 | +import TextareaCopyable from '@/components/TextareaCopyable.vue'; |
| 4 | +import { languages, translateToLanguage } from '@/utils/ascii-lang-utils'; |
| 5 | +
|
| 6 | +const inputBase64 = ref(''); |
| 7 | +const language = useStorage('image-to-ascii-art:language', 'raw'); |
| 8 | +const scale = ref(100); |
| 9 | +const errored = ref(false); |
| 10 | +const processing = ref(false); |
| 11 | +
|
| 12 | +function toBase64(file: File) { |
| 13 | + return new Promise<string>((resolve, reject) => { |
| 14 | + const reader = new FileReader(); |
| 15 | + reader.readAsDataURL(file); |
| 16 | + reader.onload = () => resolve(reader.result?.toString() ?? ''); |
| 17 | + reader.onerror = error => reject(error); |
| 18 | + }); |
| 19 | +} |
| 20 | +
|
| 21 | +const languagesOptions = languages.map(lang => ({ value: lang.id, label: lang.name })); |
| 22 | +
|
| 23 | +const output = computedAsync(async () => { |
| 24 | + const inputBase64Value = inputBase64.value; |
| 25 | + if (!inputBase64Value) { |
| 26 | + return ''; |
| 27 | + } |
| 28 | + const scaleValue = scale.value / 100.0; |
| 29 | + const languageValue = language.value; |
| 30 | +
|
| 31 | + let outputValue = ''; |
| 32 | + processing.value = true; |
| 33 | + try { |
| 34 | + errored.value = false; |
| 35 | +
|
| 36 | + const imageToAsciiArt = new ImageToAsciiArt({ |
| 37 | + config: { |
| 38 | + drawWidth: scaleValue, |
| 39 | + drawHeight: scaleValue * 0.4, |
| 40 | + }, |
| 41 | + }); |
| 42 | + outputValue = translateToLanguage(await imageToAsciiArt.convert(inputBase64Value), languageValue); |
| 43 | + imageToAsciiArt.destroy(); |
| 44 | + } |
| 45 | + catch (e) { |
| 46 | + errored.value = true; |
| 47 | + } |
| 48 | + processing.value = false; |
| 49 | +
|
| 50 | + return outputValue; |
| 51 | +}); |
| 52 | +
|
| 53 | +async function onFileUploaded(uploadedFile: File) { |
| 54 | + inputBase64.value = await toBase64(uploadedFile); |
| 55 | +} |
| 56 | +</script> |
| 57 | + |
| 58 | +<template> |
| 59 | + <c-card style="max-width: 600px;"> |
| 60 | + <div style="flex: 0 0 100%"> |
| 61 | + <div mx-auto max-w-600px> |
| 62 | + <c-file-upload |
| 63 | + title="Drag and drop a Image file here, or click to select a file" |
| 64 | + paste-image |
| 65 | + @file-upload="onFileUploaded" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + |
| 70 | + <n-form-item label="Output scale" label-placement="left" mt-2> |
| 71 | + <n-slider v-model:value="scale" :step="1" :min="1" :max="100" mr-2 /> |
| 72 | + <n-input-number v-model:value="scale" size="small" :min="1" :max="100" /> |
| 73 | + </n-form-item> |
| 74 | + |
| 75 | + <c-select v-model:value="language" :options="languagesOptions" searchable mt-3 /> |
| 76 | + |
| 77 | + <n-divider /> |
| 78 | + |
| 79 | + <div v-if="processing" flex items-center justify-center> |
| 80 | + <n-spin size="medium" /> |
| 81 | + <span class="ml-2">Processing...</span> |
| 82 | + </div> |
| 83 | + |
| 84 | + <c-alert v-if="errored" mt-1 text-center type="error"> |
| 85 | + Current settings resulted in error. |
| 86 | + </c-alert> |
| 87 | + |
| 88 | + <n-form-item v-if="!processing && !errored" label="Ascii Art text:"> |
| 89 | + <TextareaCopyable |
| 90 | + :value="output" |
| 91 | + mb-1 mt-1 |
| 92 | + copy-placement="outside" |
| 93 | + /> |
| 94 | + </n-form-item> |
| 95 | + </c-card> |
| 96 | +</template> |
| 97 | + |
| 98 | +<style lang="less"> |
| 99 | +.n-code pre { |
| 100 | + font-size: 0.2em !important; |
| 101 | +} |
| 102 | +</style> |
0 commit comments