• Stars
    star
    2,451
  • Rank 18,749 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Renders HTML into the browser's canvas

rasterizeHTML.js

NPM version

Renders HTML into the browser's canvas.

See the API.

Install

$ npm install rasterizehtml

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

Example

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

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 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().

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).

Limitations

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.

TypeScript

Import type definitions as follows:

import * as rasterizeHTML from 'rasterizehtml';

Development

Run npm test.

For tests against individual browsers run python3 -m http.server and open http://localhost:8000/test/SpecRunner.html.

Where is it used?

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

More Repositories

1

csscritic

Lightweight CSS regression testing
JavaScript
481
star
2

cjklib

Han character library for CJKV languages
Python
147
star
3

buildviz

Transparency for your build pipeline's results and runtime
Clojure
108
star
4

json-path-comparison

Comparison of the different implementations of JSONPath and language agnostic test suite.
Shell
84
star
5

xmlserializer

xmlserializer serializes a DOM subtree or DOM document into XML/XHTML
JavaScript
36
star
6

upsidedown

Simple Python module that "flips" latin characters in a string to create an "upside-down" impression
Python
27
star
7

ayepromise

A teeny-tiny promise library
JavaScript
27
star
8

pdfserver

Pdfserver is a webservice that offers common PDF operations like joining documents, selecting pages or "n pages on one".
Python
17
star
9

jquery-shiftenter

Submit your textarea through a simple press on 'Enter' with jQuery
JavaScript
14
star
10

inlineresources

Inlines style sheets, images, fonts and scripts in HTML documents.
JavaScript
13
star
11

build-facts

Dump your build pipeline's data for inspection
Clojure
13
star
12

jp

A simpler jq, and with JSONPath
Shell
11
star
13

surf

Old Git fork of the SuRF Object RDF Mapper
Python
10
star
14

greenyet

Microservices status dashboard
Clojure
10
star
15

csscritic-examples

Helping you set up CSS Critic with your project
JavaScript
9
star
16

css-font-face-src

A CSS @font-face src property value parser
TypeScript
8
star
17

eclectus

Han character dictionary
Python
7
star
18

hwr

Clone of Tegaki project
Python
6
star
19

tegaki

Chinese and Japanese Handwriting Recognition
Python
6
star
20

deniz

A simple web app for browsing RDF data
JavaScript
6
star
21

sparqlprotocolproxy

Small SPARQL protocol proxy server
Python
4
star
22

tinycors

A dead simple CORS proxy
JavaScript
2
star
23

django-wikify

django-wikify is a lightweight module to turn your static Django model views into full wiki pages.
Python
2
star
24

slow-promise

Slow down your promises
JavaScript
2
star
25

basic-auth-proxy

A tiny proxy forcing basic-auth
JavaScript
1
star
26

html2xhtml.js

HTML to XHTML converter
JavaScript
1
star