Skip to content

Commit

Permalink
Update Core.js - Add like jQuery.offset() method
Browse files Browse the repository at this point in the history
Update Canvas.js - Avoid Indexes Zero Error
Update Parse.js - Add scrollback after parse
Update package.json , rename package in bower.json , Build dist files
  • Loading branch information
tansautn committed Nov 3, 2017
1 parent 454524c commit 84284cc
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bower.json
@@ -1,6 +1,6 @@
{
"name": "html2canvas",
"version": "0.4.1",
"name": "z-html2canvas",
"version": "0.4.3",
"description": "Screenshots with JavaScript",
"main": "build/html2canvas.js",
"ignore": [
Expand Down
62 changes: 52 additions & 10 deletions build/html2canvas.js
@@ -1,8 +1,8 @@
/*
html2canvas 0.4.1 <http://html2canvas.hertzen.com>
Copyright (c) 2013 Niklas von Hertzen
<%= pkg.title || pkg.name %> <%= pkg.version %><%= pkg.homepage ? " <" + pkg.homepage + ">" : "" %>
Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>
Released under MIT License
Released under <%= _.pluck(pkg.licenses, "type").join(", ") %> License
*/

(function(window, document, undefined){
Expand Down Expand Up @@ -174,6 +174,22 @@ _html2canvas.Util.Bounds = function (element) {
return bounds;
};

/**
* Just like jQuery.offset() method
*
* @param {HTMLElement} element
* @return {{top: number, left: number}}
* @see https://github.com/oneuijs/You-Dont-Need-jQuery#2.3
* @constructor
*/
_html2canvas.Util.Offset = function (element) {
var box = element.getBoundingClientRect();
return {
top: box.top + window.pageYOffset - document.documentElement.clientTop,
left: box.left + window.pageXOffset - document.documentElement.clientLeft
};
};

// TODO ideally, we'd want everything to go through this function instead of Util.Bounds,
// but would require further work to calculate the correct positions for elements with offsetParents
_html2canvas.Util.OffsetBounds = function (element) {
Expand Down Expand Up @@ -1032,7 +1048,9 @@ function h2cRenderContext(width, height) {
};
}
_html2canvas.Parse = function (images, options, cb) {
window.scroll(0,0);
var top = window.pageYOffset || document.documentElement.scrollTop;
var left = window.pageXOffset || document.documentElement.scrollLeft;
window.scrollTo(0,0);

var element = (( options.elements === undefined ) ? document.body : options.elements[0]), // select body by default
numDraws = 0,
Expand Down Expand Up @@ -1069,6 +1087,9 @@ _html2canvas.Parse = function (images, options, cb) {
}

removePseudoElements();
Util.log('Parse patch, scrollback to : ',left,top);

window.scrollTo(left, top);

Util.log('Done parsing, moving to Render.');

Expand Down Expand Up @@ -1714,9 +1735,19 @@ _html2canvas.Parse = function (images, options, cb) {
brh = borderRadius[2][0],
brv = borderRadius[2][1],
blh = borderRadius[3][0],
blv = borderRadius[3][1],

topWidth = width - trh,
blv = borderRadius[3][1];

var halfHeight = Math.floor(height / 2);
tlh = tlh > halfHeight ? halfHeight : tlh;
tlv = tlv > halfHeight ? halfHeight : tlv;
trh = trh > halfHeight ? halfHeight : trh;
trv = trv > halfHeight ? halfHeight : trv;
brh = brh > halfHeight ? halfHeight : brh;
brv = brv > halfHeight ? halfHeight : brv;
blh = blh > halfHeight ? halfHeight : blh;
blv = blv > halfHeight ? halfHeight : blv;

var topWidth = width - trh,
rightHeight = height - brv,
bottomWidth = width - brh,
leftHeight = height - blv;
Expand Down Expand Up @@ -2945,6 +2976,7 @@ _html2canvas.Renderer.Canvas = function(options) {
return function(parsedData, options, document, queue, _html2canvas) {
var ctx = canvas.getContext("2d"),
newCanvas,
offset,
bounds,
fstyle,
zStack = parsedData.stack;
Expand Down Expand Up @@ -2982,7 +3014,6 @@ _html2canvas.Renderer.Canvas = function(options) {
ctx.restore();
});

Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");

if (options.elements.length === 1) {
if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {
Expand All @@ -2993,13 +3024,24 @@ _html2canvas.Renderer.Canvas = function(options) {
newCanvas.height = Math.ceil(bounds.height);
ctx = newCanvas.getContext("2d");

ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
canvas = null;
offset = _html2canvas.Util.Offset(options.elements[0]);
if (newCanvas.width !== 0 && newCanvas.height !== 0) {
Util.log('Draw Image Info : ',offset.left,offset.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
ctx = newCanvas.getContext("2d");
ctx.drawImage(canvas, offset.left,offset.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
canvas = null;
Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
}else{
//TODO : else return ?
Util.log("html2canvas: Renderer: Canvas renderer error, newCanvas has illegal w & h - returning parsed canvas obj");
return canvas;
}
return newCanvas;
}
}

return canvas;
};
};

})(window,document);
10 changes: 5 additions & 5 deletions build/html2canvas.min.js

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions package.json
@@ -1,20 +1,27 @@
{
"title": "html2canvas",
"name": "html2canvas",
"description": "Screenshots with JavaScript",
"version": "0.4.1",
"description": "A Forked version of Screenshots with JavaScript. Added some modified to restore scroll after Parsed , capture non-visible part on screen",
"version": "0.4.3",
"author": {
"name": "Niklas von Hertzen",
"email": "niklasvh@gmail.com",
"url": "http://hertzen.com"
},
"contributors": [
{
"name": "Zuko",
"email": "zuko.pro.vn@gmail.com",
"url": "http://zuko.pw"
}
],
"engines": {
"node": ">=0.8.0"
},
"dependencies": {},
"repository": {
"type": "git",
"url": "git@github.com:niklasvh/html2canvas.git"
"url": "git@github.com:Z-Programing/html2canvas.git"
},
"bugs": {
"url": "https://github.com/niklasvh/html2canvas/issues"
Expand Down
16 changes: 16 additions & 0 deletions src/Core.js
Expand Up @@ -165,6 +165,22 @@ _html2canvas.Util.Bounds = function (element) {
return bounds;
};

/**
* Just like jQuery.offset() method
*
* @param {HTMLElement} element
* @return {{top: number, left: number}}
* @see https://github.com/oneuijs/You-Dont-Need-jQuery#2.3
* @constructor
*/
_html2canvas.Util.Offset = function (element) {
var box = element.getBoundingClientRect();
return {
top: box.top + window.pageYOffset - document.documentElement.clientTop,
left: box.left + window.pageXOffset - document.documentElement.clientLeft
};
};

// TODO ideally, we'd want everything to go through this function instead of Util.Bounds,
// but would require further work to calculate the correct positions for elements with offsetParents
_html2canvas.Util.OffsetBounds = function (element) {
Expand Down
7 changes: 6 additions & 1 deletion src/Parse.js
@@ -1,5 +1,7 @@
_html2canvas.Parse = function (images, options, cb) {
window.scroll(0,0);
var top = window.pageYOffset || document.documentElement.scrollTop;
var left = window.pageXOffset || document.documentElement.scrollLeft;
window.scrollTo(0,0);

var element = (( options.elements === undefined ) ? document.body : options.elements[0]), // select body by default
numDraws = 0,
Expand Down Expand Up @@ -36,6 +38,9 @@ _html2canvas.Parse = function (images, options, cb) {
}

removePseudoElements();
Util.log('Parse patch, scrollback to : ',left,top);

window.scrollTo(left, top);

Util.log('Done parsing, moving to Render.');

Expand Down
18 changes: 13 additions & 5 deletions src/renderers/Canvas.js
Expand Up @@ -67,6 +67,7 @@ _html2canvas.Renderer.Canvas = function(options) {
return function(parsedData, options, document, queue, _html2canvas) {
var ctx = canvas.getContext("2d"),
newCanvas,
offset,
bounds,
fstyle,
zStack = parsedData.stack;
Expand Down Expand Up @@ -104,7 +105,6 @@ _html2canvas.Renderer.Canvas = function(options) {
ctx.restore();
});

Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");

if (options.elements.length === 1) {
if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {
Expand All @@ -115,10 +115,18 @@ _html2canvas.Renderer.Canvas = function(options) {
newCanvas.height = Math.ceil(bounds.height);
ctx = newCanvas.getContext("2d");

var imgData = canvas.getContext("2d").getImageData(bounds.left, bounds.top, bounds.width, bounds.height);
ctx.putImageData(imgData, 0, 0);

canvas = null;
offset = _html2canvas.Util.Offset(options.elements[0]);
if (newCanvas.width !== 0 && newCanvas.height !== 0) {
Util.log('Draw Image Info : ',offset.left,offset.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
ctx = newCanvas.getContext("2d");
ctx.drawImage(canvas, offset.left,offset.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
canvas = null;
Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
}else{
//TODO : else return ?
Util.log("html2canvas: Renderer: Canvas renderer error, newCanvas has illegal w & h - returning parsed canvas obj");
return canvas;
}
return newCanvas;
}
}
Expand Down

0 comments on commit 84284cc

Please sign in to comment.