Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="divDrop" style="margin: 10px; padding: 10px; background-color: lightgray; border: 4px dashed gray; border-radius: 12px;">
    <div>Drag and drop an image file here</div>
    <div>or <input id="iptFile" type="file" accept="image/*"></div>
    <div id="divPreviewContainer"></div>
</div>
<div style="margin: 10px; padding: 10px;">
    <input type="text" id="iptResult" style="width: 600px">
    <div id="divErrorOut" style="display: none; margin: 4px; padding: 4px; background-color: pink; border: 1px solid red; border-radius: 4px;"></div>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                (() => {
    "use strict";
    const divDrop = /** @type {HTMLDivElement} */(document.getElementById("divDrop"));
    const divPreviewContainer = /** @type {HTMLDivElement} */(document.getElementById("divPreviewContainer"));
    const divErrorOut = /** @type {HTMLDivElement} */(document.getElementById("divErrorOut"));
    const iptFile = /** @type {HTMLInputElement} */(document.getElementById("iptFile"));
    const iptResult = /** @type {HTMLInputElement} */(document.getElementById("iptResult"));

    /** @type { (file: File) => Promise<any> } */
    async function decodeQrCode(file) {
        divErrorOut.style.display = "none";
        try {
            // read ile
            const fileReader = new FileReader();
            const fileReadAsync = new Promise((resolve, reject) => {
                fileReader.onload = ev => resolve(ev.target?.result);
                fileReader.onerror = ev => reject(ev);
            });
            fileReader.readAsDataURL(file);
            /** @type {string} */
            const dataUrl = await fileReadAsync;

            // load as image
            divPreviewContainer.innerHTML = "";
            const imgPreview = document.createElement("img");
            const imgLoadAsync = new Promise(resolve => imgPreview.onload = ev => resolve(ev))
            imgPreview.setAttribute("src", dataUrl);
            divPreviewContainer.append(imgPreview);
            await imgLoadAsync;
            const { naturalWidth, naturalHeight } = imgPreview;

            // convert image to raw binary
            var canvas = new OffscreenCanvas(naturalWidth, naturalHeight);
            var ctx = canvas.getContext("2d");
            if (!ctx) throw "failure to getContext";
            ctx.drawImage(imgPreview, 0, 0);
            const imageData = ctx.getImageData(0, 0, naturalWidth, naturalHeight);

            // decode with jsQR
            const code = jsQR(imageData.data, naturalWidth, naturalHeight);
            if (code) {
                iptResult.value = code.data;
            } else {
                iptResult.value = "";
                throw "decode QR error";
            }
        } catch (err) {
            divErrorOut.style.display = "block";
            divErrorOut.textContent = `${err}`;
        }
    }

    // Handling D&D
    divDrop.addEventListener("dragover", ev => {
        ev.preventDefault();
    });
    divDrop.addEventListener("drop", ev => {
        ev.preventDefault();
        let imageFiles;
        if (ev.dataTransfer && 0 < (imageFiles = [...ev.dataTransfer.files].filter(f => f.type.startsWith("image/"))).length) {
            const dt = new DataTransfer();
            imageFiles.forEach(f => dt.items.add(f));
            iptFile.files = dt.files;
            decodeQrCode(imageFiles[0]);
        }
    });
    iptFile.addEventListener("change", ev => iptFile.files && 0 < iptFile.files.length ? decodeQrCode(iptFile.files[0]) : undefined)
})();
              
            
!
999px

Console