Code Monkey home page Code Monkey logo

bpg-ww's Introduction

bpg-ww

(forked from @m-2k)

BPG JavaScript Decoder on WebWorkers

Allows 8 bit depth BPG images and animations

Usage

const worker = new Worker('/static/bpgdec8a-ww.min.js');

worker.onmessage = (event) => {
    switch(event.data.type) {
        case 'log': console.log(`Worker log: ${event.data.message}`); break;
        case 'debug': console.log(`Worker debug: ${event.data.data}`); break;
        case 'res':
            let image = event.data.image;
            let frames = event.data.frames;
            let loop_count = event.data.loop_count;
            let canvas = document.getElementById(event.data.meta);
            canvas.width = image.width;
            canvas.height = image.height;
            
            let context = canvas.getContext('2d');

            (function() {
                function d() {
                    var a = image.n;
                    ++a >= frames.length && (0 == loop_count || image.q < loop_count ? (a = 0, image.q++) : a =- 1);
                    0 <= a && (image.n = a, context.putImageData(frames[a].image, 0, 0), setTimeout(d, frames[a].duration))
                };
                context.putImageData(image, 0, 0);
                frames.length > 1 && (image.n = 0, image.q = 0, setTimeout(d, frames[0].duration));
            }.bind(context, image, frames, loop_count))();

            console.log('Decode done');
            break;
        default: console.log(`Unknown event: ${event.data}`); break;
    };
};

/**
 * Decodes an bpg image
 *
 * @param  [ArrayBuffer] buffer
 * @param  [String] element
 * @return [void]
 */
function decodeBpg(buffer, element) {
    worker.postMessage({
        type: 'image',
        img: buffer,
        meta: element
    });
}

More info

bpg-ww's People

Contributors

m-2k avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.