Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

html3canvas

niklasvh71MIT1.1.5TypeScript support: included

Screenshots with JavaScript

readme

html3canvas

html2canvas修正版,解决绘制多个元素重复创建iframe的问题.

使用

import html3canvas from "html3canvas";
const resultCanvas = await html2canvas(el, opt);

文档

* 若opations参数没有elements字段, 则用法和返回和html2canvas 原库一摸一样

html3canvas(a1, {}).then((canvass)=> {
  document.body.appendChild(canvas);
});

* 复用1个iframe节省资源用法, useOnlyIFrame = true

const a1 = document.getElementById("a1");
const a2 = document.getElementById("baz");
const a3 = document.getElementById("a3");
const array = [a1, a2, a3];
const getRanDom = () => array[Math.floor(Math.random() * array.length)];

document.getElementById("btn").onclick = function() {
  html3canvas(getRanDom(), {
    useOnlyIFrame: true,
    removeContainer: false
  }).then(function(canvas) {
    document.body.appendChild(canvas);
  });
  alert("截图");
};

* 单frame渲染多个dom, 必须传elements字段, 返回canvass为三个结果canvas数组

const a1 = document.getElementById('a1');
const a2 = document.getElementById('a2');
const a3 = document.getElementById('a3');

html3canvas(a1, {
  elements: [a1, a2, a3],
  removeContainer: true
}).then((canvass)=> {
  canvass.forEach(canvas => {
    document.body.appendChild(canvas);
  });
});

* 预传size, 指定尺寸

const a1 = document.getElementById('a1');
const a2 = document.getElementById('a2');
const a3 = document.getElementById('a3');

html3canvas(a1, {
  elements: [a1, a2, a3],
  size: [[100,100], [120,100], [50, 70]]
}).then((canvass)=> {
  canvass.forEach(canvas => {
    document.body.appendChild(canvas);
  });
});

* 预传canvas

const a1 = document.getElementById('a1');
const a2 = document.getElementById('a2');
const a3 = document.getElementById('a3');

const c1 = createCanvas(100, 100);
const c2 = createCanvas(120, 100);
const c3 = createCanvas(50, 70);

html3canvas(a1, {
  elements: [a1, a2, a3],
  canvass: [c1, c2, c3],
  size: [[100,100], [120,100], [50, 70]]
}).then((canvass)=> {
  canvass.forEach(canvas => {
    document.body.appendChild(canvas);
  });
});

参数

Name Default Description
allowTaint false Whether to allow cross-origin images to taint the canvas
backgroundColor #ffffff Canvas background color, if none is specified in DOM. Set null for transparent
canvas null Existing canvas element to use as a base for drawing on
foreignObjectRendering false Whether to use ForeignObject rendering if the browser supports it
imageTimeout 15000 Timeout for loading an image (in milliseconds). Set to 0 to disable timeout.
ignoreElements (element) => false Predicate function which removes the matching elements from the render.
logging true Enable logging for debug purposes
onclone null Callback function which is called when the Document has been cloned for rendering,
original source document.
proxy null Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.
removeContainer true Whether to cleanup the cloned DOM elements html2canvas creates temporarily
scale window.devicePixelRatio The scale to use for rendering. Defaults to the browsers device pixel ratio.
useCORS false Whether to attempt to load images from a server using CORS
width Element width The width of the canvas
height Element height The height of the canvas
x Element x-offset Crop canvas x-coordinate
y Element y-offset Crop canvas y-coordinate
scrollX Element scrollX The x-scroll position to used when rendering element, (for example if the Element uses position: fixed)
scrollY Element scrollY The y-scroll position to used when rendering element, (for example if the Element uses position: fixed)
windowWidth Window.innerWidth Window width to use when rendering Element, which may affect things like Media queries
windowHeight Window.innerHeight Window height to use when rendering Element, which may affect things like Media queries

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.0-rc.5 (2019-09-27)

fix

1.0.0-rc.4 (2019-09-22)

docs

feat

fix

1.0.0-rc.3 (2019-05-30)

fix

1.0.0-rc.2 (2019-05-29)

ci

docs

fix

test

1.0.0-rc.1 (2019-04-10)

ci

docs

fix

test

1.0.0-rc.0 (2019-04-07)

build

ci

docs

fix

1.0.0-alpha.12 (2018-04-05)

  • Fix white space appearing on element rendering (Fix #1438)
  • Reset canvas transform on finish (Fix #1494)

v1.0.0-alpha.11 - 1.4.2018

  • Fix IE11 member not found error
  • Support blob image resources in non-foreignObjectRendering mode

v1.0.0-alpha.10 - 15.2.2018

  • Re-introduce onclone option (Fix #1434)
  • Add ignoreElements predicate function option
  • Fix version console logging

v1.0.0-alpha.9 - 7.1.2018

  • Fix dynamic style sheets
  • Fix > 50% border-radius values

v1.0.0-alpha.8 - 2.1.2018

  • Use correct doctype in cloned Document (Fix #1298)
  • Fix individual border rendering (Fix #1349)

v1.0.0-alpha.7 - 31.12.2017

  • Fix form input rendering (#1338)
  • Improve word line breaking algorithm

v1.0.0-alpha.6 - 28.12.2017

  • Fix list-style: none (#1340)
  • Extend supported values for pseudo element content

v1.0.0-alpha.5 - 21.12.2017

  • Fix underline positioning
  • Fix canvas rendering on Chrome
  • Fix overflow: auto
  • Added support for rendering list-style

    v1.0.0-alpha.4 - 12.12.2017

  • Fix rendering with multiple fonts defined (Fix #796)
  • Add support for radial-gradients
  • Fix logging option (#1302)
  • Add support for rendering webgl canvas content (#646)
  • Fix external SVG loading with proxies (#802)

v1.0.0-alpha.3 - 9.12.2017

  • Disable foreignObjectRendering by default (#1295)
  • Fix background-size when using background-origin and background-size: cover/contain (#1299)
  • Added support for background-origin: content-box (#1299)

v1.0.0-alpha.2 - 7.12.2017

  • Fix scroll positions for CanvasRenderer (#1259)
  • Fix data-html2canvas-ignore attribute (#1253)
  • Fix decimal letter-spacing values (#1293)

v1.0.0-alpha.1 - 5.12.2017

  • Complete rewrite of library
    Breaking Changes
  • Remove deprecated onrendered callback, calling html2canvas returns a Promise<HTMLCanvasElement>
  • Removed option type, same results can be achieved by assigning x, y, scrollX, scrollY, width and height properties.

    New featues / fixes

  • Add support for scaling canvas (defaults to device pixel ratio)
  • Add support for multiple text-shadows
  • Add support for multiple text-decorations
  • Add support for text-decoration-color
  • Add support for percentage values for border-radius
  • Correctly handle px and percentage values in linear-gradients
  • Correctly support all angle types for linear-gradients
  • Add support for multiple values for background-repeat, background-position and background-size

v0.5.0-beta4 - 23.1.2016

  • Fix logger requiring access to window object
  • Derequire browserify build
  • Fix rendering of specific elements when window is scrolled and type isn't set to view

v0.5.0-beta3 - 6.12.2015

  • Handle color names in linear gradients

v0.5.0-beta2 - 20.10.2015

  • Remove Promise polyfill (use native or provide it yourself)

v0.5.0-beta1 - 19.10.2015

  • Fix bug with unmatched color stops in gradients
  • Fix scrolling issues with iOS
  • Correctly handle named colors in gradients
  • Accept matrix3d transforms
  • Fix transparent colors breaking gradients
  • Preserve scrolling positions on render

v0.5.0-alpha2 - 3.2.2015

  • Switch to using browserify for building
  • Fix (#517) Chrome stretches background images with 'auto' or single attributes

v0.5.0-alpha - 19.1.2015

  • Complete rewrite of library
  • Switched interface to return Promise
  • Uses hidden iframe window to perform rendering, allowing async rendering and doesn't force the viewport to be scrolled to the top anymore.
  • Better support for unicode
  • Checkbox/radio button rendering
  • SVG rendering
  • iframe rendering
  • Changed format for proxy requests, permitting binary responses with CORS headers as well
  • Fixed many layering issues (see z-index tests)

v0.4.1 - 7.9.2013

  • Added support for bower
  • Improved z-index ordering
  • Basic implementation for CSS transformations
  • Fixed inline text in top element
  • Basic implementation for text-shadow

v0.4.0 - 30.1.2013

  • Added rendering tests with webdriver
  • Switched to using grunt for building
  • Removed support for IE<9, including any FlashCanvas bits
  • Support for border-radius
  • Support for multiple background images, size, and clipping
  • Support for :before and :after pseudo elements
  • Support for placeholder rendering
  • Reformatted all tests to small units to test specific features

v0.3.4 - 26.6.2012

  • Removed (last?) jQuery dependencies (niklasvh)
  • SVG-powered rendering (niklasvh)
  • Radial gradients (SunboX)
  • Split renderers to their own objects (niklasvh)
  • Simplified API, cleaned up code (niklasvh)

v0.3.3 - 2.3.2012

  • SVG taint fix, and additional taint testing options for rendering (niklasvh)
  • Added support for CORS images and option to create canvas as tainted (niklasvh)
  • Improved minification saved ~1K! (cobexer)
  • Added integrated support for Flashcanvas (niklasvh)
  • Fixed a variety of legacy IE bugs (niklasvh)

v0.3.2 - 20.2.2012

  • Added changelog!
  • Added bookmarklet (cobexer)
  • Option to select single element to render (niklasvh)
  • Fixed closure compiler warnings (cobexer)
  • Enable profiling in FF (cobexer)