Pen Settings



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


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


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.


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.


                <div id="message"></div>
<div style="margin: 50px 50px">
    <img id="original" src="" width="500" height="375" crossorigin="anonymous">
    <canvas id="output" width="500" height="375" style="border: 1px solid;"></canvas>

<!-- script src="Filters.js"></script -->
<script type="text/javascript">
    const image = document.getElementById('original');
    image.onload = function () {
        console.log("Original Image: %s, %d x %d", image.src, image.width, image.height);

        // Extract data from the image object
        // imageDataObj: {data (Uint8ClampedArray), width, height}
        // const imageDataObj = Filters.getPixels(image);
        const imageDataObj = getImageData(image);

        console.log("Pixels: type '%s', %d bytes, %d x %d, data: %s...", typeof(,
  , imageDataObj.width, imageDataObj.height,
  , 10).toString());

        const outputCanvas = document.getElementById('output'),
            ctx = outputCanvas.getContext('2d');

        let useWorker = false;
        if ( useWorker ) {
            const worker = new Worker("threshold-worker.js");

            console.log("Posting image pixels to worker");
            // can't pass a object directly as a Transferable, get a 'DataCloneError';
            // is a Uint8ClampedArray, is a TypedArray
            worker.postMessage(imageDataObj, []);

            worker.addEventListener('message', (evt) => {
                console.log("Received data back from worker");
                const results =;
                displayFilteredImage(ctx, results.newImageObj);
                showMessage('Filtered image in ' + results.duration + ' msec');
       } else {
            // No worker: call code inline
            const results = thresholdImage(imageDataObj);
            displayFilteredImage(ctx, results.newImageData);
            const messageArea = document.getElementById('message');
            showMessage('Filtered image in ' + results.duration + ' msec');

    function getImageData(image) {
        const tempCanvas = document.createElement('canvas'),
            tempCtx = tempCanvas.getContext('2d');
        tempCanvas.width = image.width;
        tempCanvas.height = image.height;
        tempCtx.drawImage(image, 0, 0, image.width, image.height);
        const imageDataObj = tempCtx.getImageData(0, 0, image.width, image.height);
        return imageDataObj;

    function thresholdImage(pixelData) {
        const thresholdLevel = 96,    // 0-255
            tstart = new Date().getTime();

        const newImageDataObj = Filters.threshold(pixelData, thresholdLevel);
        const duration = new Date().getTime() - tstart;

        console.log("Filter image: %d msec", duration);
        return {newImageData: newImageDataObj, duration: duration};

    function displayFilteredImage(ctx, newPixelData) {
        const tstart = new Date().getTime();
        ctx.putImageData(newPixelData, 0, 0);
        console.log("Render filtered image: %d msec", (new Date().getTime() - tstart));

    function showMessage(msg) {
        const messageArea = document.getElementById('message');
        messageArea.innerText = msg;





 * Excerpt from "Making Image Filters with Canvas - HTML5 Rocks"
 * By Ilmari Heikkinen, Published May 25th, 2011
var Filters = {};
Filters.threshold = function(pixels, threshold) {
    var d =;
    for (var i=0; i<d.length; i+=4) {
        var r = d[i];
        var g = d[i+1];
        var b = d[i+2];
        var v = (0.2126*r + 0.7152*g + 0.0722*b >= threshold) ? 255 : 0;
        d[i] = d[i+1] = d[i+2] = v
    return pixels;