rasterizehtml.js

repository·master·Indexed 25 days ago

https://github.com/cburgmer/rasterizehtml.js

A JavaScript library (version 1.4.1) that renders HTML content into a browser's canvas element. It provides functions such as drawHTML(), drawDocument(), and drawURL() to convert HTML strings, DOM documents, or web pages into canvas drawings by embedding content into an SVG <foreignObject>. The library handles external resource conversion to data URIs and supports Chrome, Firefox, Safari, and Microsoft Edge, while requiring Same-Origin or CORS configuration for external resources.

Tokens
1.3K
Snippets
4
Records
16
Agent score
82%

What's inside rasterizehtml.js

  1. Understand Same-Origin and CORS limitations

    master

    All resources required for drawing (the HTML page, CSS, images, fonts, and JS) must be from the same origin as the current page.

    If you need to load resources from a different domain, you must use CORS (Cross-Origin Resource Sharing). For example, drawURL() can only load pages from the same domain as the current page unless CORS is properly configured on the target server.

  2. How rasterizeHTML.js works

    master

    Because browsers restrict direct HTML-to-canvas rendering for security, rasterizeHTML.js uses a workaround: it embeds the HTML into an SVG <foreignObject> and then draws that SVG image onto the canvas using ctx.drawImage().

    Since SVG <foreignObject> elements are not allowed to link to external resources, the library automatically loads external images, fonts, and stylesheets and converts them into inline data: URIs or inline style elements before rendering.

  3. Browser compatibility and limitations

    master

    Supported Browsers

    • Chrome
    • Firefox
    • Safari
    • Microsoft Edge (basic support)

    Unsupported Browsers

    • Internet Explorer: Does not work under any version of IE.

    General Limitations

    • Individual browsers may have issues rendering SVGs with embedded HTML to the canvas.
    • Resource loading is restricted by the Same-Origin policy unless CORS is used.
  4. Draw HTML into a canvas using drawHTML()

    master

    Use rasterizeHTML.drawHTML() to render a string of HTML directly onto a specified HTML5 canvas element. The library handles embedding the HTML into an SVG <foreignObject> and drawing it via ctx.drawImage().

    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);
  5. Configure viewport dimensions in options

    master

    When calling drawHTML, drawDocument, or drawURL, you can pass an options object to control the viewport size.

    • width: The desired width of the rasterized output.
    • height: The desired height of the rasterized output.

    If a canvas is provided, the library will attempt to use the canvas's existing width and height as fallbacks if options.width or options.height are undefined.

  6. Render an HTML string to a canvas with drawHTML()

    master
    Use drawHTML(html, [canvas], [options]) to convert an HTML string into a canvas drawing. This method parses the provided HTML string and uses the internal rasterization engine to draw it onto the specified canvas. If no canvas is provided, the dimensions will be determined by the default viewport or the fallback logic. Returns a Promise that resolves with the result of the rasterization.
  7. Render a URL to a canvas with drawURL()

    master
    Use drawURL(url, [canvas], [options]) to fetch a web page from a URL and render it onto a canvas. This method handles loading the document from the network and includes a workaround for Firefox to ensure stylesheets are loaded correctly. Returns a Promise.
  8. Render a DOM Document to a canvas with drawDocument()

    master
    Use drawDocument(document, [canvas], [options]) to render an existing DOM Document object to a canvas. This is useful when you have already parsed or manipulated a document in memory and want to rasterize it directly without re-parsing an HTML string.
  9. Use the rasterizeHTML drawing functions

    master

    The rasterizeHTML namespace provides three primary functions to render HTML content onto an HTML5 HTMLCanvasElement or OffscreenCanvas. All functions return a Promise that resolves with a RenderResult on success or rejects with an Error on failure.

    • drawHTML(html, canvas, options): Renders a raw HTML string.
    • drawURL(url, canvas, options): Renders a document from a specific URL.
    • drawDocument(document, canvas, options): Renders an existing Document object. Note that executeJs is not available when using drawDocument.