From 7cc2b856cb7d32ada3f04283e3eec9c69961d9c1 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 1 Aug 2017 23:41:12 +0800 Subject: [PATCH] Use correct canvas size for full document render --- src/Bounds.js | 22 ++++++++++++++++++++++ src/index.js | 14 ++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Bounds.js b/src/Bounds.js index aaba24c..1388fdc 100644 --- a/src/Bounds.js +++ b/src/Bounds.js @@ -78,6 +78,28 @@ export const calculateContentBox = ( ); }; +export const parseDocumentSize = (document: Document): Bounds => { + const body = document.body; + const documentElement = document.documentElement; + + if (!body || !documentElement) { + throw new Error(__DEV__ ? `Unable to get document size` : ''); + } + const width = Math.max( + Math.max(body.scrollWidth, documentElement.scrollWidth), + Math.max(body.offsetWidth, documentElement.offsetWidth), + Math.max(body.clientWidth, documentElement.clientWidth) + ); + + const height = Math.max( + Math.max(body.scrollHeight, documentElement.scrollHeight), + Math.max(body.offsetHeight, documentElement.offsetHeight), + Math.max(body.clientHeight, documentElement.clientHeight) + ); + + return new Bounds(0, 0, width, height); +}; + export const parsePathForBorder = (curves: BoundCurves, borderSide: BorderSide): Path => { switch (borderSide) { case TOP: diff --git a/src/index.js b/src/index.js index 1104a62..d423cbb 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ import {NodeParser} from './NodeParser'; import CanvasRenderer from './CanvasRenderer'; import Logger from './Logger'; import ImageLoader from './ImageLoader'; -import {Bounds} from './Bounds'; +import {Bounds, parseDocumentSize} from './Bounds'; import {cloneWindow} from './Clone'; import Color from './Color'; @@ -62,8 +62,10 @@ const html2canvas = (element: HTMLElement, config: Options): Promise