MCPcopy Index your code
hub / github.com/cburgmer/rasterizeHTML.js

github.com/cburgmer/rasterizeHTML.js @1.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.4.1 ↗ · + Follow
98 symbols 196 edges 27 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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="https://github.com/cburgmer/rasterizeHTML.js/raw/1.4.1/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
  • ...

Extension points exported contracts — how you extend this code

Options (Interface)
(no doc)
src/typings.d.ts
Resource (Interface)
(no doc)
src/typings.d.ts
RenderResult (Interface)
(no doc)
src/typings.d.ts
Error (Interface)
(no doc)
src/typings.d.ts

Core symbols most depended-on inside this repo

asArray
called by 4
src/documentUtil.js
fileUrl
called by 3
test/integrationTest.js
doReject
called by 3
src/browser.js
urlForSvg
called by 3
src/svg2image.js
asArray
called by 3
src/documentHelper.js
triggerHandlers
called by 2
test/testHelper.js
screenshotToDataUri
called by 2
test/integrationTest.js
renderPage
called by 2
test/integrationTest.js

Shape

Function 94
Interface 4

Languages

TypeScript100%

Modules by API surface

src/browser.js16 symbols
src/svg2image.js9 symbols
test/specs/rasterize.test.js8 symbols
src/documentUtil.js8 symbols
test/specs/browser.test.js7 symbols
src/document2svg.js7 symbols
test/integrationTest.js5 symbols
src/rasterize.js5 symbols
src/index.js5 symbols
test/specs/index.test.js4 symbols
src/typings.d.ts4 symbols
test/testHelper.js3 symbols

For agents

$ claude mcp add rasterizeHTML.js \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact