flotilla/src/app/components/QRCode.svelte

38 lines
1 KiB
Svelte
Raw Normal View History

<script lang="ts">
import QRCode from "qrcode"
import {onMount} from "svelte"
import Button from "@lib/components/Button.svelte"
2026-06-08 21:14:40 +00:00
import {clip} from "@app/toast"
const {code, ...props} = $props()
2025-02-04 00:37:14 +00:00
let canvas: Element | undefined = $state()
let wrapper: Element | undefined = $state()
let scale = $state(0.1)
let height = $state(0)
2025-01-31 22:14:42 +00:00
const copy = () => clip(code)
onMount(() => {
2025-02-04 00:37:14 +00:00
if (canvas && wrapper) {
QRCode.toCanvas(canvas, code)
2025-02-04 00:37:14 +00:00
const wrapperRect = wrapper.getBoundingClientRect()
const canvasRect = canvas.getBoundingClientRect()
2025-02-04 00:37:14 +00:00
scale = wrapperRect.width / (canvasRect.width * 10)
2026-01-09 19:02:32 +00:00
height = canvasRect.height * 10 * scale
2025-02-04 00:37:14 +00:00
}
})
</script>
2025-09-22 21:57:43 +00:00
<Button class="flex w-full justify-center {props.class}" onclick={copy}>
<div bind:this={wrapper} class="w-md" style={`height: ${height}px`}>
<canvas
class="rounded-box"
bind:this={canvas}
2025-02-03 23:01:42 +00:00
style={`transform-origin: top left; transform: scale(${scale}, ${scale})`}>
</canvas>
</div>
</Button>