zxing-js/library

View on GitHub
docs/examples/qr-svg-writer/index.html

Summary

Maintainability
Test Coverage
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="author" content="ZXing for JS">

    <title>ZXing | Write QR Code to SVG</title>

    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.css">
    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>

<body>

    <main class="wrapper" style="padding-top:2em">

        <section class="container" id="demo-content">
            <h1 class="title">Write QR Code to SVG</h1>

            <p>
                <a class="button-small button-outline" href="../../index.html">HOME 🏡</a>
            </p>

            <p>This example shows how to write a QR code to SVG with ZXing javascript library.

                <div>
                    <label for="textInput">Content to write in QR code</label>
                </div>
                <div>
                    <textarea id="textInput" maxlength="256">QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan.</textarea>
                </div>

                <div>
                    <a class="button" id="writeButton">Write</a>
                    <a class="button" id="saveButton" disabled="disabled">Save File</a>
                </div>

                <label>Result:</label>
                <pre><code id="result"></code></pre>

                <p>See the <a href="https://github.com/zxing-js/library/tree/master/docs/examples/qr-svg-writer/">source code</a> for this example.</p>

        </section>

        <footer class="footer">
            <section class="container">
                <p>ZXing TypeScript Demo. Licensed under the <a target="_blank" href="https://github.com/zxing-js/library#license" title="MIT">MIT</a>.</p>
            </section>
        </footer>

    </main>

    <script type="text/javascript" src="https://unpkg.com/@zxing/library@latest"></script>
    <script type="text/javascript" src="https://unpkg.com/file-saver@latest"></script>
    <script type="text/javascript">
        window.addEventListener('load', () => {

            const codeWriter = new ZXing.BrowserQRCodeSvgWriter()

            console.log('ZXing code writer initialized')

            let svgElement;

            document.getElementById('writeButton').addEventListener('click', () => {
                const input = document.getElementById('textInput').value
                svgElement = codeWriter.writeToDom('#result', input, 300, 300)
                document.getElementById('saveButton').removeAttribute('disabled')
            })

            document.getElementById('saveButton').addEventListener('click', () => {
                const svgData = (new XMLSerializer()).serializeToString(document.getElementById('result'))
                const blob = new Blob([svgData])
                saveAs(blob, 'zxing-qrcode-example.svg')
            })

        })
    </script>

</body>

</html>