qrcode.vue

repository·main·Indexed 21 days ago

https://github.com/scopewu/qrcode.vue

A Vue.js component library for generating QR codes, supporting both Vue 2 and Vue 3. It provides Canvas and SVG rendering modes via the QrcodeVue, QrcodeCanvas, and QrcodeSvg components. Key features include logo embedding via image-settings, color gradients, rounded modules, and methods to download or convert QR codes to Data URLs.

Tokens
2.9K
Snippets
10
Records
13
Agent score
24%

What's inside qrcode.vue

  1. Customize module corner radius

    main

    Use the radius prop to round the corners of the QR code modules. The value is a ratio of the module width, ranging from 0 to 0.5.

    • 0 (default): Square modules with sharp corners.
    • 0.5: Maximum rounding (modules become circles).

    Rounding is context-aware: inner corners between adjacent dark modules remain sharp, while outer corners are rounded.

    <qrcode-vue value="test" :radius="0.35" />
  2. SSR Safety with the `id` prop

    main

    When using render-as="svg" in a Server-Side Rendering (SSR) environment, you must provide a unique id prop to ensure hydration consistency between the server and the client. If omitted, the component uses a module-level counter which is not SSR-safe.

    In Vue 3.5+, use the useId() hook to generate a stable ID.

    <script setup>
      import { useId } from 'vue'
      const uid = useId()
    </script>
    
    <qrcode-svg value="test" :id="uid" render-as="svg" />
  3. Basic Usage of qrcode.vue

    main

    You can use the QrcodeVue component in a standard Vue application by importing it and registering it in your components. For a simple implementation, pass the value prop to define the QR code content.

    import { createApp } from 'vue'
    import QrcodeVue from 'qrcode.vue'
    
    createApp({
      data: {
        value: 'https://example.com',
      },
      template: '<qrcode-vue :value="value"></qrcode-vue>',
      components: {
        QrcodeVue,
      },
    }).mount('#root')
  4. Use QrcodeCanvas and QrcodeSvg components

    main

    In addition to the main QrcodeVue component, you can import specialized components for specific rendering modes: QrcodeCanvas for canvas rendering and QrcodeSvg for SVG rendering.

    <template>
      <qrcode-vue :value="value" :size="size" level="H" render-as="svg" />
      <qrcode-canvas :value="QRCODE.VUE 😄" :size="size" level="H" />
      <qrcode-svg value="QRCODE.VUE 😄" level="H" />
    </template>
    <script>
      import QrcodeVue, { QrcodeCanvas, QrcodeSvg } from 'qrcode.vue'
    
      export default {
        data() {
          return {
            value: 'https://example.com',
            size: 300,
          }
        },
        components: {
          QrcodeVue,
          QrcodeCanvas,
          QrcodeSvg,
        },
      }
    </script>
  5. Download or convert QR code to Data URL

    main

    Both QrcodeCanvas and QrcodeSvg expose methods via template refs to download the QR code or convert it to a data URL.

    QrcodeCanvas Methods

    MethodSignatureDescription
    toDataURL(type?: string, quality?: number) => string | undefinedConvert the canvas to a data URL.
    download(filename?: string) => voidTrigger a download of the QR code as a PNG image.

    QrcodeSvg Methods

    MethodSignatureDescription
    toDataURL() => string | undefinedConvert the SVG element to a data URL.
    download(filename?: string) => voidTrigger a download of the QR code as an SVG image.
    <script setup>
    import { ref } from 'vue'
    import { QrcodeCanvas } from 'qrcode.vue'
    
    const qrRef = ref()
    const handleDownload = () => {
      qrRef.value?.download('my-qrcode.png')
    }
    </script>
    
    <qrcode-canvas ref="qrRef" value="https://example.com" />
  6. Configure image-settings for QR code logos

    main

    To embed a logo in your QR code, use the image-settings prop.

    Type Definition:

    export type ImageSettings = {
      src: string, // The URL of image.
      x?: number,  // The horizontal offset. Defaults to center.
      y?: number,  // The vertical offset. Defaults to center.
      height: number, // The height of image
      width: number, // The width of image
      excavate?: boolean, // Whether or not to "excavate" the modules around the image.
      borderRadius?: number, // The border radius of image.
      crossOrigin?: 'anonymous' | 'use-credentials' | '', // The CORS attribute for the image.
    }

    CORS Note: When using a cross-origin logo, set crossOrigin: 'anonymous' and ensure the image server responds with Access-Control-Allow-Origin to avoid a SecurityError when using toDataURL or download on a canvas.

    const imageSettings = ref<ImageSettings>({
      src: 'https://github.com/scopewu.png',
      width: 30,
      height: 30,
      excavate: true,
      crossOrigin: 'anonymous',
    })
  7. Configure QrcodeVue component props

    main

    The QrcodeVue component accepts several props to customize the appearance and behavior of the QR code:

    PropTypeDefaultDescription
    valuestring''The content of the QR code.
    sizenumber100The size of the QR code element.
    render-asRenderAs('canvas' | 'svg')canvasRendering mode. svg is suitable for SSR.
    marginnumber0Width of the quiet zone.
    levelLevel('L' | 'M' | 'Q' | 'H')LError correction level.
    backgroundstring#ffffffBackground color.
    foregroundstring#000000Foreground color.
    image-settingsImageSettings{}Settings for embedding a logo image.
    radiusnumber0Corner radius of modules (0 to 0.5).
    idstringundefinedCustom ID for SVG elements. Use useId() in Vue 3.5+ for SSR safety.
    gradientbooleanfalseEnable gradient fill.
    gradient-typeGradientType('linear' | 'radial')linearType of gradient.
    gradient-start-colorstring#000000Start color of gradient.
    gradient-end-colorstring#ffffffEnd color of gradient.
    classstring''CSS class name for the element.
  8. Export QR code as Data URL or Download

    main

    Both QrcodeSvg and QrcodeCanvas (and by extension QrcodeVue) expose methods via component refs to export the generated QR code.

    • toDataURL(type?: string, quality?: number): Returns the QR code as a base64 Data URL. For canvas, type can be 'image/png'. For SVG, it returns an encoded SVG string.
    • download(filename?: string): Triggers a browser download of the QR code. The default filename is qrcode.png for canvas and qrcode.svg for SVG.
    <script setup>
    import { ref } from 'vue'
    import QrcodeVue from 'qrcode.vue'
    
    const qrRef = ref()
    
    const handleDownload = () => {
      qrRef.value.download('my-qr-code.png')
    }
    
    const handleDataURL = () => {
      const url = qrRef.value.toDataURL()
      console.log(url)
    }
    </script>
    
    <template>
      <QrcodeVue ref="qrRef" value="https://example.com" />
      <button @click="handleDownload">Download</button>
    </template>
  9. Use the QrcodeVue component

    main

    The QrcodeVue component is the primary entry point for generating QR codes. It acts as a wrapper that renders either a <canvas> or an <svg> based on the render-as prop.

    Both rendering modes support common features like error correction levels, custom colors, gradients, and embedded images.

    <script setup>
    import QrcodeVue from 'qrcode.vue'
    </script>
    
    <template>
      <QrcodeVue 
        value="https://example.com" 
        :size="200" 
        render-as="canvas" 
      />
    </template>
  10. Configure QrcodeVue props

    main

    The following props are available for QrcodeVue, QrcodeCanvas, and QrcodeSvg:

    PropTypeDefaultDescription
    valuestring''Required. The text or URL to encode.
    sizenumber100The width and height of the QR code in pixels.
    levelLevel'L'Error correction level: 'L', 'M', 'Q', or 'H'.
    backgroundstring'#fff'Background color (CSS color string).
    foregroundstring'#000'QR code module color (CSS color string).
    marginnumber0White space around the QR code.
    radiusnumber0Corner radius for modules (0 to 0.5).
    gradientbooleanfalseWhether to use a color gradient.
    gradientTypeGradientType'linear'Type of gradient: 'linear' or 'radial'.
    gradientStartColorstring'#000'Starting color of the gradient.
    gradientEndColorstring'#fff'Ending color of the gradient.
    image-settingsImageSettings{}Configuration for an embedded logo image.
    render-asRenderAs'canvas'The rendering engine: 'canvas' or 'svg'.
    idstringundefinedOptional unique ID for the component.
  11. Configure image-settings for embedded logos

    main

    To embed an image (like a logo) inside the QR code, provide an object to the image-settings prop.

    ImageSettings Object Keys:

    • src (string): Required. The URL or path to the image.
    • x (number, optional): X position relative to the QR code.
    • y (number, optional): Y position relative to the QR code.
    • height (number, optional): Height of the image.
    • width (number, optional): Width of the image.
    • excavate (boolean, optional): If true, the QR modules behind the image are removed to create a clear space.
    • borderRadius (number, optional): Corner radius for the image.
    • crossOrigin ('anonymous' | 'use-credentials' | ''): CORS setting for the image.
    <QrcodeVue
      value="https://example.com"
      :image-settings='{
        src: "/logo.png",
        width: 40,
        height: 40,
        excavate: true,
        borderRadius: 8
      }'
    />