Skip to content

Commit 4dc5b31

Browse files
add
1 parent ea02050 commit 4dc5b31

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { cn } from '$lib/utils';
3-
import { renderSVG } from 'uqr';
3+
import QRCode from 'qrcode';
44
55
interface Props {
66
value: string;
@@ -18,41 +18,36 @@
1818
class: klass
1919
}: Props = $props();
2020
21-
let svgMarkup = $state('');
21+
let canvas = $state<null | HTMLCanvasElement>(null);
2222
2323
$effect(() => {
24-
const svg = renderSVG(value, {
25-
border: 1,
26-
pixelSize: 1,
27-
blackColor: color,
28-
whiteColor: backgroundColor
29-
});
30-
31-
if (klass) {
32-
const safeClass = klass.replace(/"/g, '&quot;');
33-
svgMarkup = svg.replace('<svg', `<svg class="${safeClass}"`);
34-
return;
24+
if (canvas) {
25+
QRCode.toCanvas(
26+
canvas,
27+
value,
28+
{
29+
width: size,
30+
margin: 1,
31+
color: {
32+
dark: color,
33+
light: backgroundColor
34+
}
35+
},
36+
(error) => {
37+
if (error) console.error(error);
38+
}
39+
);
3540
}
36-
37-
svgMarkup = svg;
3841
});
3942
</script>
4043

4144
<div
4245
class={cn(
43-
`qr-code grid h-fit w-fit`,
44-
// Prevent right-click save on the rendered SVG by overlaying a transparent layer.
46+
`grid h-fit w-fit`,
47+
// I dont like the fact that canvas is a image and people can right click canvas to get the image
48+
// So i am making the canvas unclickable using the same technique facebook/instagram uses
4549
`after:col-start-1 after:row-start-1 after:h-full after:w-full after:content-['']`
4650
)}
47-
style={`width: ${size}px; height: ${size}px;`}
4851
>
49-
{@html svgMarkup}
52+
<canvas bind:this={canvas} class={cn(klass, 'col-start-1 row-start-1')}></canvas>
5053
</div>
51-
52-
<style>
53-
.qr-code :global(svg) {
54-
width: 100%;
55-
height: 100%;
56-
display: block;
57-
}
58-
</style>

0 commit comments

Comments
 (0)