Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                    <h2>Compare libreoffice test images</h2>
    <p>

        <img id="imgBefore" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAA3ZAAAN2QHmodeGAAAFyUlEQVR4nO1Za2gcVRQ+szuzO9ndZtfEktY2tVorVdNKxRpQweqPmh/B1w8JhCL4R4RYiPhCfKFEfwX/2VYULJIqJFDqg4KC1EaoEoyGhFJClBgbN+a9z9ndeXnOzJ3szCbbQuY2i5ADh5udmXvP953z3cdMRNM04f9sYq0B+LVNArW2TQJXM0EQdmKTQL+Mi4V2PWJwJ4CgBUkKdImi9F48HpGikTptfj4l19fLX2cyxeeQyALPeNwJxOOx/r17djz6+afdsX233QBg5CCXWYS3es62n/js4mXkdyeSmOMVjysBURQ7djVvbRv87p2oLJUAtHmLQDSUg963W8MhMSscPzXWh48e4RaT10BkKJkXens6orKYRvBZC7zlut2+231r6MOTI4exCgmswjKPmFwJFIvFu+5pqbczr7vB22QkMwd7d8vK2Hj+AD5+gUdMrgRCUnD+yt9T8ZubGsrAHSKMTH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WsOsjSLISioCXzUZKWZcAqz9jdUfnQQDWsqHh5M7RIQX27ttM2Qi5xnmJkFguC+tkgGVyB1oM/bCntI0SueZhroysYrSUOhnwUkYayUoKElcoq9CvQeBOtFm0ObQ4W8o3IqnrnkYpMkxWOXSScMnF2ZkX6PgRLx67krBIMQJeKK2ZLG2eguvQyJ8AlGbF7UShDDEZzJ4ahPzXP4HqkJyeteaP9p+GljPnrSCYqsaWUOvzb2gqmb7Rgq74PoB+VSLRNxeDYTTnLAUU5V+jMWhDU0lmLOozOI7L5ZjLq0WKXgKJfAe2HJzl1qDNaoaHn3KN67tRIqLDZt4hYRMJVKNtE6h22yZQ7XbDE/gf25LGjrgcuP4AAAAASUVORK5CYII="  >
    </p>
    <p>
        <button id="diff" class="js-compareImages">compare images</button>
        <canvas id="cnvDiff"></canvas>
    </p>
    <p id="result">
    </p>
              
            
!

CSS

              
                
              
            
!

JS

              
                // Converts image to canvas; returns new canvas element
function convertImageToCanvas(imageID)
{
    var image = document.getElementById(imageID);
    var canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
         canvas.getContext("2d").drawImage(image, 0, 0);
    // image.style = "width: 400px";
    return canvas;
}


function writeResultToPage(imgDataOutput)
{
    console.log("writeResultToPage was called. imgDataOutput.data.length =", imgDataOutput.data.length);
    var canvas = document.createElement("canvas"); //  new HTMLCanvasElement();
    var ctx = canvas.getContext("2d");
    ctx.putImageData(imgDataOutput, 0, 0);
    var result = document.getElementById("result");
    result.appendChild(ctx.canvas);
}


function printNumberPixels(pixels)
{
    let div = document.createElement('div');
    div.className = "alert";
    div.innerHTML = "<strong>Differing pixels: </strong>" + pixels;
    document.body.append(div);
}

function compareImages()
{
    console.clear();
    var cnvBefore = convertImageToCanvas("imgBefore");
    var cnvAfter = convertImageToCanvas("imgAfter");

    var ctxBefore = cnvBefore.getContext("2d");
    var ctxAfter = cnvAfter.getContext("2d");

    let imgDataBefore = ctxBefore.getImageData(0,0,cnvBefore.width, cnvBefore.height);
    let imgDataAfter = ctxAfter.getImageData(0,0, cnvAfter.width, cnvAfter.height);

    const hght = imgDataBefore.height;
    const wdth = imgDataBefore.width;

    var imgDataOutput = new ImageData(wdth, hght);

    var numDiffPixels = pixelmatch(imgDataBefore.data, imgDataAfter.data,
				   imgDataOutput.data, wdth, hght, {threshold: 0.1});
    console.log("numDiffPixels =", numDiffPixels);
    printNumberPixels(numDiffPixels);

    // this line does not work
    writeResultToPage(imgDataOutput)

}

document.querySelector('button.js-compareImages').addEventListener('click', compareImages);

              
            
!
999px

Console