Add options to define window dimensions

This commit is contained in:
Niklas von Hertzen 2017-08-06 17:53:38 +08:00
parent 965f850e68
commit 8da77eb689
2 changed files with 28 additions and 13 deletions

View File

@ -14,9 +14,11 @@ export default class ImageLoader {
cache: ImageCache;
logger: Logger;
_index: number;
_window: WindowProxy;
constructor(options: Options, logger: Logger) {
constructor(options: Options, logger: Logger, window: WindowProxy) {
this.options = options;
this._window = window;
this.origin = this.getOrigin(window.location.href);
this.cache = {};
this.logger = logger;
@ -70,7 +72,7 @@ export default class ImageLoader {
}
getOrigin(url: string): string {
const link = this._link || (this._link = document.createElement('a'));
const link = this._link || (this._link = this._window.document.createElement('a'));
link.href = url;
link.href = link.href; // IE9, LOL! - http://jsfiddle.net/niklasvh/2e48b/
return link.protocol + link.hostname + link.port;

View File

@ -17,7 +17,10 @@ export type Options = {
imageTimeout: ?number,
proxy: ?string,
removeContainer: ?boolean,
type: ?string
scale: number,
type: ?string,
windowWidth: number,
windowHeight: number
};
const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasElement> => {
@ -25,12 +28,6 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
const ownerDocument = element.ownerDocument;
const defaultView = ownerDocument.defaultView;
const windowBounds = new Bounds(
defaultView.pageXOffset,
defaultView.pageYOffset,
defaultView.innerWidth,
defaultView.innerHeight
);
const defaultOptions = {
async: true,
@ -39,12 +36,21 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
imageTimeout: 10000,
proxy: null,
removeContainer: true,
scale: defaultView.devicePixelRatio,
type: null
scale: defaultView.devicePixelRatio || 1,
type: null,
windowWidth: defaultView.innerWidth,
windowHeight: defaultView.innerHeight
};
const options = {...defaultOptions, ...config};
const windowBounds = new Bounds(
defaultView.pageXOffset,
defaultView.pageYOffset,
options.windowWidth,
options.windowHeight
);
const canvas = options.canvas;
if (!(canvas instanceof HTMLCanvasElement)) {
@ -61,7 +67,12 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
if (__DEV__) {
logger.log(`Document cloned`);
}
const imageLoader = new ImageLoader(options, logger);
const imageLoader = new ImageLoader(
options,
logger,
clonedElement.ownerDocument.defaultView
);
const stack = NodeParser(clonedElement, imageLoader, logger);
const clonedDocument = clonedElement.ownerDocument;
const size = options.type === 'view' ? windowBounds : parseDocumentSize(clonedDocument);
@ -92,7 +103,9 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
}
const fontMetrics = new FontMetrics(clonedDocument);
if (__DEV__) {
logger.log(`Starting renderer`);
}
const renderer = new CanvasRenderer(canvas, {
scale: options.scale,
backgroundColor,