mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Assign default options values
This commit is contained in:
parent
478155af64
commit
c5135f4839
1
.babelrc
1
.babelrc
@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
|
"plugins": ["transform-object-rest-spread"],
|
||||||
"presets": ["es2015", "flow"]
|
"presets": ["es2015", "flow"]
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
"babel-core": "6.25.0",
|
"babel-core": "6.25.0",
|
||||||
"babel-eslint": "7.2.3",
|
"babel-eslint": "7.2.3",
|
||||||
"babel-loader": "7.1.1",
|
"babel-loader": "7.1.1",
|
||||||
|
"babel-plugin-transform-object-rest-spread": "6.23.0",
|
||||||
"babel-preset-es2015": "6.24.1",
|
"babel-preset-es2015": "6.24.1",
|
||||||
"babel-preset-flow": "6.23.0",
|
"babel-preset-flow": "6.23.0",
|
||||||
"base64-arraybuffer": "0.1.5",
|
"base64-arraybuffer": "0.1.5",
|
||||||
|
50
src/index.js
50
src/index.js
@ -10,15 +10,19 @@ import {cloneWindow} from './Clone';
|
|||||||
import Color from './Color';
|
import Color from './Color';
|
||||||
|
|
||||||
export type Options = {
|
export type Options = {
|
||||||
async: boolean,
|
async: ?boolean,
|
||||||
imageTimeout: number,
|
allowTaint: ?boolean,
|
||||||
proxy: string,
|
canvas: ?HTMLCanvasElement,
|
||||||
canvas: HTMLCanvasElement,
|
imageTimeout: ?number,
|
||||||
allowTaint: true,
|
proxy: ?string,
|
||||||
type: string
|
removeContainer: ?boolean,
|
||||||
|
type: ?string
|
||||||
};
|
};
|
||||||
|
|
||||||
const html2canvas = (element: HTMLElement, options: Options): Promise<HTMLCanvasElement> => {
|
const html2canvas = (
|
||||||
|
element: HTMLElement,
|
||||||
|
config: Options
|
||||||
|
): Promise<HTMLCanvasElement> => {
|
||||||
const logger = new Logger();
|
const logger = new Logger();
|
||||||
|
|
||||||
const ownerDocument = element.ownerDocument;
|
const ownerDocument = element.ownerDocument;
|
||||||
@ -30,6 +34,25 @@ const html2canvas = (element: HTMLElement, options: Options): Promise<HTMLCanvas
|
|||||||
defaultView.innerHeight
|
defaultView.innerHeight
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const defaultOptions = {
|
||||||
|
async: true,
|
||||||
|
allowTaint: false,
|
||||||
|
canvas: ownerDocument.createElement('canvas'),
|
||||||
|
imageTimeout: 10000,
|
||||||
|
proxy: null,
|
||||||
|
removeContainer: true,
|
||||||
|
scale: defaultView.devicePixelRatio,
|
||||||
|
type: null
|
||||||
|
};
|
||||||
|
|
||||||
|
const options = {...defaultOptions, ...config};
|
||||||
|
|
||||||
|
const canvas = options.canvas;
|
||||||
|
|
||||||
|
if (!(canvas instanceof HTMLCanvasElement)) {
|
||||||
|
return Promise.reject(__DEV__ ? `Invalid canvas element provided in options` : '');
|
||||||
|
}
|
||||||
|
|
||||||
return cloneWindow(
|
return cloneWindow(
|
||||||
ownerDocument,
|
ownerDocument,
|
||||||
ownerDocument,
|
ownerDocument,
|
||||||
@ -40,15 +63,12 @@ const html2canvas = (element: HTMLElement, options: Options): Promise<HTMLCanvas
|
|||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
logger.log(`Document cloned`);
|
logger.log(`Document cloned`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageLoader = new ImageLoader(options, logger);
|
const imageLoader = new ImageLoader(options, logger);
|
||||||
const stack = NodeParser(clonedElement, imageLoader, logger);
|
const stack = NodeParser(clonedElement, imageLoader, logger);
|
||||||
const canvas = ownerDocument.createElement('canvas');
|
|
||||||
const scale = defaultView.devicePixelRatio;
|
|
||||||
const width = window.innerWidth;
|
const width = window.innerWidth;
|
||||||
const height = window.innerHeight;
|
const height = window.innerHeight;
|
||||||
canvas.width = Math.floor(width * scale);
|
canvas.width = Math.floor(width * options.scale);
|
||||||
canvas.height = Math.floor(height * scale);
|
canvas.height = Math.floor(height * options.scale);
|
||||||
canvas.style.width = `${width}px`;
|
canvas.style.width = `${width}px`;
|
||||||
canvas.style.height = `${height}px`;
|
canvas.style.height = `${height}px`;
|
||||||
|
|
||||||
@ -56,20 +76,22 @@ const html2canvas = (element: HTMLElement, options: Options): Promise<HTMLCanvas
|
|||||||
const backgroundColor =
|
const backgroundColor =
|
||||||
clonedElement === ownerDocument.documentElement
|
clonedElement === ownerDocument.documentElement
|
||||||
? stack.container.style.background.backgroundColor.isTransparent()
|
? stack.container.style.background.backgroundColor.isTransparent()
|
||||||
? ownerDocument.body instanceof HTMLElement
|
? ownerDocument.body
|
||||||
? new Color(getComputedStyle(ownerDocument.body).backgroundColor)
|
? new Color(getComputedStyle(ownerDocument.body).backgroundColor)
|
||||||
: null
|
: null
|
||||||
: stack.container.style.background.backgroundColor
|
: stack.container.style.background.backgroundColor
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
return imageLoader.ready().then(imageStore => {
|
return imageLoader.ready().then(imageStore => {
|
||||||
|
if (options.removeContainer === true) {
|
||||||
if (container.parentNode) {
|
if (container.parentNode) {
|
||||||
container.parentNode.removeChild(container);
|
container.parentNode.removeChild(container);
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
logger.log(`Cannot detach cloned iframe as it is not in the DOM anymore`);
|
logger.log(`Cannot detach cloned iframe as it is not in the DOM anymore`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const renderer = new CanvasRenderer(canvas, {scale, backgroundColor, imageStore});
|
const renderer = new CanvasRenderer(canvas, {scale: options.scale, backgroundColor, imageStore});
|
||||||
return renderer.render(stack);
|
return renderer.render(stack);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user