Novice, Practiced, Mastered

    TypeScript icon, indicating that this package has built-in type declarations

    1.3.0 • Public • Published


    NPM version

    Renders HTML into the browser's canvas.

    See the API.


    $ npm install rasterizehtml

    Then include a script tag with node_modules/rasterizehtml/dist/rasterizeHTML.allinone.js or require through browserify.


    var canvas = document.getElementById("canvas");
    rasterizeHTML.drawHTML('Some ' +
                           '<span style="color: green; font-size: 20px;">HTML</span>' +
                           ' with an image <img src="someimg.png">',

    See the examples page. The code also ships with examples, make sure to run npm test first to compile the library.

    How does it work

    For security reasons rendering HTML into a canvas is severly limited. Firefox offers such a function via ctx.drawWindow(), but only with Chrome privileges (see

    As described in and however it is possible by embedding the HTML into an SVG image as a <foreignObject> and then drawing the resulting image via ctx.drawImage().

    In addition SVG is not allowed to link to external resources and so rasterizeHTML.js will load external images, fonts and stylesheets and store them inline via data: URIs (or inline style elements respectively).


    All resources (HTML page, CSS, images, fonts and JS) that are needed for drawing the page can only be loaded if from the same origin, unless techniques like CORS are used. I.E. drawURL() can only load pages from the same domain as the current page and all draw methods can equally only embed styling and images from that domain.

    The code is tested under Firefox, Chrome & Safari.

    There's basic support for Microsoft Edge, however it will not work under any version of Internet Explorer.

    Also the individual browsers still have some issues when rendering SVGs with embedded HTML to the canvas.

    The full list of limitations is here.


    Import type definitions as follows:

    import * as rasterizeHTML from 'rasterizehtml';


    Run npm test. There's also a vagrant image that installs all necessary build dependencies.

    For tests against individual browsers open test/SpecRunner.html, for integration tests under Safari open test/manualIntegrationTestForWebkit.html (under Chrome you will either need to start the browser passing in the option --allow-file-access-from-files or load the page through a local webserver).

    Build Status

    Where is it used?

    • CSS Critic, a lightweight tool for regression testing of Cascading Style Sheets
    • ...



    npm i rasterizehtml

    DownloadsWeekly Downloads






    Unpacked Size

    337 kB

    Total Files


    Last publish


    • cburgmer