Assign default options values

This commit is contained in:
Niklas von Hertzen 2017-08-01 22:36:51 +08:00
parent 478155af64
commit c5135f4839
3 changed files with 42 additions and 18 deletions

View File

@ -1,3 +1,4 @@
{ {
"plugins": ["transform-object-rest-spread"],
"presets": ["es2015", "flow"] "presets": ["es2015", "flow"]
} }

View File

@ -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",

View File

@ -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);
}); });
}); });