GithubHelp home page GithubHelp logo

hhy5277 / rasterizehtml.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cburgmer/rasterizehtml.js

0.0 1.0 0.0 7.43 MB

Renders HTML into the browser's canvas

Home Page: http://cburgmer.github.io/rasterizeHTML.js

License: MIT License

JavaScript 92.42% HTML 7.58%

rasterizehtml.js's Introduction

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. There's also a vagrant image that installs all necessary build dependencies.

For tests against individual browsers run python3 -m http.server and open http://localhost:8000/test/SpecRunner.html, for integration tests under Safari open http://localhost:8000/test/manualIntegrationTestForWebkit.html.

Build Status

Where is it used?

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

rasterizehtml.js's People

Contributors

cburgmer avatar greenkeeperio-bot avatar kuranes avatar orentrutner avatar arturkulig avatar kevinoid avatar qingwei-li avatar

Watchers

 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.