feat: add styled QR code generator with logo and edge customization
Generates QR codes from a URL with center logo upload, adjustable logo padding and size, dot/corner-square/corner-dot style pickers, color controls, and PNG/SVG export. Uses error correction level H so the logo can occlude up to ~30% of modules without breaking scannability.
This commit is contained in:
154
src/App.vue
Normal file
154
src/App.vue
Normal file
@@ -0,0 +1,154 @@
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref } from 'vue'
|
||||
import { useQrCode, type QrOptions } from './composables/useQrCode'
|
||||
|
||||
const preview = ref<HTMLDivElement | null>(null)
|
||||
|
||||
const options = reactive<QrOptions>({
|
||||
data: 'https://example.com',
|
||||
size: 320,
|
||||
dotsType: 'rounded',
|
||||
dotsColor: '#1f2937',
|
||||
cornersSquareType: 'extra-rounded',
|
||||
cornersSquareColor: '#1f2937',
|
||||
cornersDotType: 'dot',
|
||||
cornersDotColor: '#1f2937',
|
||||
backgroundColor: '#ffffff',
|
||||
image: null,
|
||||
imageMargin: 8,
|
||||
imageSize: 0.4,
|
||||
hideBackgroundDots: true,
|
||||
})
|
||||
|
||||
const { download } = useQrCode(preview, options)
|
||||
|
||||
const dotTypes = ['square', 'dots', 'rounded', 'classy', 'classy-rounded', 'extra-rounded'] as const
|
||||
const cornerSquareTypes = ['square', 'dot', 'extra-rounded'] as const
|
||||
const cornerDotTypes = ['square', 'dot'] as const
|
||||
|
||||
function onLogoChange(e: Event) {
|
||||
const file = (e.target as HTMLInputElement).files?.[0]
|
||||
if (!file) return
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => {
|
||||
options.image = reader.result as string
|
||||
}
|
||||
reader.readAsDataURL(file)
|
||||
}
|
||||
|
||||
function clearLogo() {
|
||||
options.image = null
|
||||
const input = document.getElementById('logo-input') as HTMLInputElement | null
|
||||
if (input) input.value = ''
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="app">
|
||||
<header>
|
||||
<h1>QR Code Studio</h1>
|
||||
<p class="subtitle">Generate styled QR codes with logo, dots, and corner customization</p>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<section class="controls">
|
||||
<div class="field">
|
||||
<label for="url">URL or text</label>
|
||||
<input id="url" v-model="options.data" type="text" placeholder="https://example.com" />
|
||||
</div>
|
||||
|
||||
<fieldset>
|
||||
<legend>Logo</legend>
|
||||
<div class="field">
|
||||
<input id="logo-input" type="file" accept="image/*" @change="onLogoChange" />
|
||||
</div>
|
||||
<div v-if="options.image" class="logo-actions">
|
||||
<img :src="options.image" alt="logo preview" class="logo-thumb" />
|
||||
<button type="button" class="ghost" @click="clearLogo">Remove logo</button>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>
|
||||
Logo padding
|
||||
<span class="value">{{ options.imageMargin }} px</span>
|
||||
</label>
|
||||
<input v-model.number="options.imageMargin" type="range" min="0" max="40" step="1" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>
|
||||
Logo size
|
||||
<span class="value">{{ Math.round(options.imageSize * 100) }} %</span>
|
||||
</label>
|
||||
<input v-model.number="options.imageSize" type="range" min="0.1" max="0.6" step="0.01" />
|
||||
</div>
|
||||
<label class="checkbox">
|
||||
<input v-model="options.hideBackgroundDots" type="checkbox" />
|
||||
Hide dots behind logo
|
||||
</label>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Body</legend>
|
||||
<div class="field">
|
||||
<label for="dotsType">Dot style</label>
|
||||
<select id="dotsType" v-model="options.dotsType">
|
||||
<option v-for="t in dotTypes" :key="t" :value="t">{{ t }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="dotsColor">Dot color</label>
|
||||
<input id="dotsColor" v-model="options.dotsColor" type="color" />
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Corners</legend>
|
||||
<div class="field">
|
||||
<label for="csType">Outer square style</label>
|
||||
<select id="csType" v-model="options.cornersSquareType">
|
||||
<option v-for="t in cornerSquareTypes" :key="t" :value="t">{{ t }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="csColor">Outer square color</label>
|
||||
<input id="csColor" v-model="options.cornersSquareColor" type="color" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="cdType">Inner dot style</label>
|
||||
<select id="cdType" v-model="options.cornersDotType">
|
||||
<option v-for="t in cornerDotTypes" :key="t" :value="t">{{ t }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="cdColor">Inner dot color</label>
|
||||
<input id="cdColor" v-model="options.cornersDotColor" type="color" />
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Background</legend>
|
||||
<div class="field">
|
||||
<label for="bg">Background color</label>
|
||||
<input id="bg" v-model="options.backgroundColor" type="color" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>
|
||||
Size
|
||||
<span class="value">{{ options.size }} px</span>
|
||||
</label>
|
||||
<input v-model.number="options.size" type="range" min="160" max="640" step="20" />
|
||||
</div>
|
||||
</fieldset>
|
||||
</section>
|
||||
|
||||
<section class="preview-pane">
|
||||
<div class="preview-card">
|
||||
<div ref="preview" class="preview"></div>
|
||||
</div>
|
||||
<div class="download">
|
||||
<button type="button" @click="download('png')">Download PNG</button>
|
||||
<button type="button" @click="download('svg')">Download SVG</button>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
66
src/composables/useQrCode.ts
Normal file
66
src/composables/useQrCode.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { onMounted, shallowRef, watch, type Ref } from 'vue'
|
||||
import QRCodeStyling, {
|
||||
type DotType,
|
||||
type CornerSquareType,
|
||||
type CornerDotType,
|
||||
type FileExtension,
|
||||
} from 'qr-code-styling'
|
||||
|
||||
export type QrOptions = {
|
||||
data: string
|
||||
size: number
|
||||
dotsType: DotType
|
||||
dotsColor: string
|
||||
cornersSquareType: CornerSquareType
|
||||
cornersSquareColor: string
|
||||
cornersDotType: CornerDotType
|
||||
cornersDotColor: string
|
||||
backgroundColor: string
|
||||
image: string | null
|
||||
imageMargin: number
|
||||
imageSize: number
|
||||
hideBackgroundDots: boolean
|
||||
}
|
||||
|
||||
export function useQrCode(container: Ref<HTMLElement | null>, options: QrOptions) {
|
||||
const qr = shallowRef<QRCodeStyling | null>(null)
|
||||
|
||||
onMounted(() => {
|
||||
qr.value = new QRCodeStyling(toLibOptions(options))
|
||||
if (container.value) qr.value.append(container.value)
|
||||
})
|
||||
|
||||
watch(
|
||||
() => ({ ...options }),
|
||||
() => qr.value?.update(toLibOptions(options)),
|
||||
{ deep: true },
|
||||
)
|
||||
|
||||
const download = (extension: FileExtension) => {
|
||||
qr.value?.download({ name: 'qrcode', extension })
|
||||
}
|
||||
|
||||
return { download }
|
||||
}
|
||||
|
||||
function toLibOptions(o: QrOptions) {
|
||||
return {
|
||||
width: o.size,
|
||||
height: o.size,
|
||||
type: 'svg' as const,
|
||||
data: o.data || ' ',
|
||||
image: o.image ?? undefined,
|
||||
margin: 0,
|
||||
qrOptions: { errorCorrectionLevel: 'H' as const },
|
||||
dotsOptions: { color: o.dotsColor, type: o.dotsType },
|
||||
backgroundOptions: { color: o.backgroundColor },
|
||||
cornersSquareOptions: { color: o.cornersSquareColor, type: o.cornersSquareType },
|
||||
cornersDotOptions: { color: o.cornersDotColor, type: o.cornersDotType },
|
||||
imageOptions: {
|
||||
crossOrigin: 'anonymous',
|
||||
margin: o.imageMargin,
|
||||
imageSize: o.imageSize,
|
||||
hideBackgroundDots: o.hideBackgroundDots,
|
||||
},
|
||||
}
|
||||
}
|
||||
242
src/style.css
Normal file
242
src/style.css
Normal file
@@ -0,0 +1,242 @@
|
||||
:root {
|
||||
font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||||
color-scheme: light;
|
||||
color: #0f172a;
|
||||
background: #f1f5f9;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
text-rendering: optimizeLegibility;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
#app {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.app {
|
||||
max-width: 1100px;
|
||||
margin: 0 auto;
|
||||
padding: 32px 24px 64px;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 28px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0 0 6px;
|
||||
font-size: 28px;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
margin: 0;
|
||||
color: #64748b;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
main {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) minmax(0, 380px);
|
||||
gap: 28px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
@media (max-width: 880px) {
|
||||
main {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.controls,
|
||||
.preview-card {
|
||||
background: #fff;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 14px;
|
||||
padding: 20px;
|
||||
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
|
||||
}
|
||||
|
||||
.controls {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
fieldset {
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 10px;
|
||||
padding: 14px 16px 16px;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
legend {
|
||||
padding: 0 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: #475569;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.field label {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: #334155;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.value {
|
||||
color: #64748b;
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
input[type="text"],
|
||||
select {
|
||||
width: 100%;
|
||||
padding: 9px 11px;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 8px;
|
||||
font: inherit;
|
||||
background: #fff;
|
||||
transition: border-color 0.12s, box-shadow 0.12s;
|
||||
}
|
||||
|
||||
input[type="text"]:focus,
|
||||
select:focus {
|
||||
outline: none;
|
||||
border-color: #6366f1;
|
||||
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.18);
|
||||
}
|
||||
|
||||
input[type="color"] {
|
||||
width: 100%;
|
||||
height: 38px;
|
||||
padding: 2px;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 8px;
|
||||
background: #fff;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
input[type="range"] {
|
||||
width: 100%;
|
||||
accent-color: #6366f1;
|
||||
}
|
||||
|
||||
input[type="file"] {
|
||||
font: inherit;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.checkbox {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 13px;
|
||||
color: #334155;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.logo-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.logo-thumb {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
object-fit: contain;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 8px;
|
||||
background: #f8fafc;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.preview-pane {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
position: sticky;
|
||||
top: 24px;
|
||||
}
|
||||
|
||||
.preview-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
min-height: 360px;
|
||||
}
|
||||
|
||||
.preview {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.preview svg,
|
||||
.preview canvas {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.download {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
button {
|
||||
font: inherit;
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
padding: 10px 14px;
|
||||
border-radius: 10px;
|
||||
border: 1px solid transparent;
|
||||
background: #4f46e5;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
transition: background 0.12s, transform 0.05s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #4338ca;
|
||||
}
|
||||
|
||||
button:active {
|
||||
transform: translateY(1px);
|
||||
}
|
||||
|
||||
button.ghost {
|
||||
background: transparent;
|
||||
color: #475569;
|
||||
border-color: #cbd5e1;
|
||||
padding: 6px 10px;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
button.ghost:hover {
|
||||
background: #f1f5f9;
|
||||
color: #1e293b;
|
||||
}
|
||||
Reference in New Issue
Block a user