Code Monkey home page Code Monkey logo

rasterizehtml.js's Introduction

rasterizeHTML.js

Renders HTML into the browser's canvas.

See the API.

Example

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

See the examples page and the examples shipped with the code.

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 https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas).

As described in http://robert.ocallahan.org/2011/11/drawing-dom-content-to-canvas.html and https://developer.mozilla.org/en/HTML/Canvas/Drawing_DOM_objects_into_a_canvas however it is possible by embedding the HTML into an SVG image as a <foreignObject> and then drawing the resulting image via ctx.drawImage().

To cope with the existing limitations, rasterizeHTML.js will load external images, fonts and stylesheets and store them inline via data: URIs or inline style elements respectively.

Limitations

SVG is not allowed to link to external resources, as such all resources need to be embedded using data: URIs. While the solution to that is loading and inlining external resources those can only be loaded if from the same origin (unless CORS is used).

The code is tested under Firefox, Chrome & Safari. However IE is not supported so far.

At the time of writing it seems that the individual browsers still have some issues with rendering SVGs with embedded HTML to the canvas. See the wiki for a list of known issues and do add your findings there.

Development

For linting, tests and minification install Node.js, SlimerJS, Firefox and run

$ ./go

For the integration test under Chrome and 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 framework for regression testing of Cascading Style Sheets
  • ...

Author

Christoph Burgmer. Licensed under MIT. Reach out on Twitter.

rasterizehtml.js's People

Watchers

James Cloos avatar jeep 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.