Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add usePageCss option, add toCanvasList and toImage method for large html #473

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ All the top level functions accept DOM node and rendering options, and return a
- [toSvg](#toSvg)
- [toJpeg](#toJpeg)
- [toBlob](#toBlob)
- [toImage](#toImage)
- [toCanvas](#toCanvas)
- [toCanvasList](#toCanvasList)
- [toPixelData](#toPixelData)

Go with the following examples.
Expand Down Expand Up @@ -114,6 +116,16 @@ htmlToImage.toBlob(document.getElementById('my-node'))
});
```

#### toImage
Get a HTMLImageElement, which is a svg image that you can scale it to a big size and it will not blurred.

```js
htmlToImage.toImage(document.getElementById('my-node'))
.then(function (img) {
document.body.appendChild(img);
});
```

#### toCanvas
Get a HTMLCanvasElement, and display it right away:

Expand All @@ -124,6 +136,17 @@ htmlToImage.toCanvas(document.getElementById('my-node'))
});
```

#### toCanvasList
Get a array of HTMLCanvasElement. Not like `toCanvas` which is limited by [canvas size](https://jhildenbiddle.github.io/canvas-size/#/?id=test-results),
`toCanvasList` can get rid of the limitation of canvas size, so this can export a very large html:

```js
htmlToImage.toCanvasList(document.getElementById('my-node'))
.then(function (canvasList) {
canvasList.map(canvas => document.body.appendChild(canvas));
});
```

#### toPixelData
Get the raw pixel data as a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) with every 4 array elements representing the RGBA data of a pixel:

Expand Down Expand Up @@ -292,6 +315,15 @@ A string indicating the image format. The default type is image/png; that type i
When supplied, the toCanvas function will return a blob matching the given image type and quality.

Defaults to `image/png`


### usePageCss

Use `true` to add a `<style>` tag in svg content which imports all styles of current html page, and do not add computed styles to every node any more(this make svg content so large that Firefox throw errors while load svg as image).
This will make the svg content much smaller, to resolve problems which caused by html that has large amount of sub nodes.


Defaults to `false`

## Browsers

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "html-to-image",
"version": "1.11.11",
"description": "Generates an image from a DOM node using HTML5 canvas and SVG.",
"name": "html-to-image-big",
"version": "1.11.12",
"description": "Generates an image from a DOM node using HTML5 canvas and SVG. Support big html page.",
"main": "lib/index.js",
"module": "es/index.js",
"unpkg": "dist/html-to-image.js",
Expand All @@ -23,7 +23,7 @@
"svg"
],
"scripts": {
"lint": "eslint 'src/**/*.{js,ts}?(x)' --fix",
"lint": "eslint src/**/*.{js,ts} --fix",
"clean": "rimraf dist es lib",
"build:esm": "tsc --module esnext --target es2017 --outDir ./es",
"build:cjs": "tsc --module commonjs --target es5 --outDir ./lib",
Expand Down
12 changes: 6 additions & 6 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ function cloneCSSStyle<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
) {
value = 'block'
}

if (name === 'd' && clonedNode.getAttribute('d')) {
value = `path(${clonedNode.getAttribute('d')})`
}

targetStyle.setProperty(
name,
value,
Expand Down Expand Up @@ -170,10 +170,10 @@ function cloneSelectValue<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
}
}

function decorate<T extends HTMLElement>(nativeNode: T, clonedNode: T): T {
function decorate<T extends HTMLElement>(nativeNode: T, clonedNode: T, usePageCss?: boolean): T {
if (isInstanceOfElement(clonedNode, Element)) {
cloneCSSStyle(nativeNode, clonedNode)
clonePseudoElements(nativeNode, clonedNode)
if (!usePageCss) cloneCSSStyle(nativeNode, clonedNode)
if (!usePageCss) clonePseudoElements(nativeNode, clonedNode)
cloneInputValue(nativeNode, clonedNode)
cloneSelectValue(nativeNode, clonedNode)
}
Expand Down Expand Up @@ -240,6 +240,6 @@ export async function cloneNode<T extends HTMLElement>(
return Promise.resolve(node)
.then((clonedNode) => cloneSingleNode(clonedNode, options) as Promise<T>)
.then((clonedNode) => cloneChildren(node, clonedNode, options))
.then((clonedNode) => decorate(node, clonedNode))
.then((clonedNode) => decorate(node, clonedNode, options.usePageCss))
.then((clonedNode) => ensureSVGSymbols(clonedNode, options))
}
96 changes: 0 additions & 96 deletions src/embed-images.ts

This file was deleted.

92 changes: 0 additions & 92 deletions src/embed-resources.ts

This file was deleted.

Loading
Loading