mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Add options to define window dimensions
This commit is contained in:
parent
965f850e68
commit
8da77eb689
@ -14,9 +14,11 @@ export default class ImageLoader {
|
|||||||
cache: ImageCache;
|
cache: ImageCache;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
_index: number;
|
_index: number;
|
||||||
|
_window: WindowProxy;
|
||||||
|
|
||||||
constructor(options: Options, logger: Logger) {
|
constructor(options: Options, logger: Logger, window: WindowProxy) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this._window = window;
|
||||||
this.origin = this.getOrigin(window.location.href);
|
this.origin = this.getOrigin(window.location.href);
|
||||||
this.cache = {};
|
this.cache = {};
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
@ -70,7 +72,7 @@ export default class ImageLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getOrigin(url: string): string {
|
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 = url;
|
||||||
link.href = link.href; // IE9, LOL! - http://jsfiddle.net/niklasvh/2e48b/
|
link.href = link.href; // IE9, LOL! - http://jsfiddle.net/niklasvh/2e48b/
|
||||||
return link.protocol + link.hostname + link.port;
|
return link.protocol + link.hostname + link.port;
|
||||||
|
35
src/index.js
35
src/index.js
@ -17,7 +17,10 @@ export type Options = {
|
|||||||
imageTimeout: ?number,
|
imageTimeout: ?number,
|
||||||
proxy: ?string,
|
proxy: ?string,
|
||||||
removeContainer: ?boolean,
|
removeContainer: ?boolean,
|
||||||
type: ?string
|
scale: number,
|
||||||
|
type: ?string,
|
||||||
|
windowWidth: number,
|
||||||
|
windowHeight: number
|
||||||
};
|
};
|
||||||
|
|
||||||
const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasElement> => {
|
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 ownerDocument = element.ownerDocument;
|
||||||
const defaultView = ownerDocument.defaultView;
|
const defaultView = ownerDocument.defaultView;
|
||||||
const windowBounds = new Bounds(
|
|
||||||
defaultView.pageXOffset,
|
|
||||||
defaultView.pageYOffset,
|
|
||||||
defaultView.innerWidth,
|
|
||||||
defaultView.innerHeight
|
|
||||||
);
|
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
async: true,
|
async: true,
|
||||||
@ -39,12 +36,21 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
|
|||||||
imageTimeout: 10000,
|
imageTimeout: 10000,
|
||||||
proxy: null,
|
proxy: null,
|
||||||
removeContainer: true,
|
removeContainer: true,
|
||||||
scale: defaultView.devicePixelRatio,
|
scale: defaultView.devicePixelRatio || 1,
|
||||||
type: null
|
type: null,
|
||||||
|
windowWidth: defaultView.innerWidth,
|
||||||
|
windowHeight: defaultView.innerHeight
|
||||||
};
|
};
|
||||||
|
|
||||||
const options = {...defaultOptions, ...config};
|
const options = {...defaultOptions, ...config};
|
||||||
|
|
||||||
|
const windowBounds = new Bounds(
|
||||||
|
defaultView.pageXOffset,
|
||||||
|
defaultView.pageYOffset,
|
||||||
|
options.windowWidth,
|
||||||
|
options.windowHeight
|
||||||
|
);
|
||||||
|
|
||||||
const canvas = options.canvas;
|
const canvas = options.canvas;
|
||||||
|
|
||||||
if (!(canvas instanceof HTMLCanvasElement)) {
|
if (!(canvas instanceof HTMLCanvasElement)) {
|
||||||
@ -61,7 +67,12 @@ const html2canvas = (element: HTMLElement, config: Options): Promise<HTMLCanvasE
|
|||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
logger.log(`Document cloned`);
|
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 stack = NodeParser(clonedElement, imageLoader, logger);
|
||||||
const clonedDocument = clonedElement.ownerDocument;
|
const clonedDocument = clonedElement.ownerDocument;
|
||||||
const size = options.type === 'view' ? windowBounds : parseDocumentSize(clonedDocument);
|
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);
|
const fontMetrics = new FontMetrics(clonedDocument);
|
||||||
|
if (__DEV__) {
|
||||||
|
logger.log(`Starting renderer`);
|
||||||
|
}
|
||||||
const renderer = new CanvasRenderer(canvas, {
|
const renderer = new CanvasRenderer(canvas, {
|
||||||
scale: options.scale,
|
scale: options.scale,
|
||||||
backgroundColor,
|
backgroundColor,
|
||||||
|
Loading…
Reference in New Issue
Block a user