From 93e2c0046b6c9b0e7b6f257d791432f4aa12bdd0 Mon Sep 17 00:00:00 2001
From: MoyuScript <i@moyu.moe>
Date: Sun, 6 Aug 2017 17:53:38 +0800
Subject: [PATCH] Add options to define window dimensions

---
 src/ImageLoader.js |  6 ++++--
 src/index.js       | 35 ++++++++++++++++++++++++-----------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/ImageLoader.js b/src/ImageLoader.js
index d7dd706..523e318 100644
--- a/src/ImageLoader.js
+++ b/src/ImageLoader.js
@@ -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;
diff --git a/src/index.js b/src/index.js
index 8af8bc3..423e597 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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,