Last active
September 19, 2017 09:49
-
-
Save simonmysun/c6da13ce2827a374e71ccf07d9f08e2d to your computer and use it in GitHub Desktop.
render hidden / invisible area with html2canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var toImg = function toImg(targetNode) { | |
var tmpNode = function recreateNode(node) { | |
switch (node.nodeType) { | |
case 1: | |
{ | |
var result = document.createElement(node.nodeName); | |
var style = getComputedStyle(node); | |
for (var i = style.length - 1; i >= 0; i -= 1) { | |
result.style[style[i]] = style[style[i]]; | |
} | |
switch (node.nodeName) { | |
case 'IMG': | |
{ | |
result.setAttribute('src', node.getAttribute('src')); | |
break; | |
} | |
default: | |
{ | |
break; | |
} | |
} | |
var child = node.firstChild; | |
while (child !== null) { | |
result.appendChild(recreateNode(child)); | |
child = child.nextSibling; | |
} | |
return result; | |
} | |
case 3: | |
{ | |
return document.createTextNode(node.textContent); | |
} | |
default: | |
{ | |
break; | |
} | |
} | |
return false; | |
}(targetNode); | |
var tmpNodeContainer = document.createElement('iframe'); | |
tmpNodeContainer.src = 'about:blank'; | |
document.body.append(tmpNodeContainer); | |
tmpNodeContainer.contentWindow.document.body.append(tmpNode); | |
tmpNode.style.overflow = 'visible'; | |
tmpNode.style.height = 'auto'; | |
tmpNodeContainer.style.transform = 'scale(0)'; | |
html2canvas(tmpNode).then(function (canvas) { | |
document.body.removeChild(tmpNodeContainer); | |
var img = document.createElement('img'); | |
img.src = canvas.toDataURL('image/png'); | |
document.body.append(img); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
其实这个只满足我当时的需求,比方说只处理了 img 的属性,别的都没管