html2canvas/tests/assets/jquery.plugin.html2canvas.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2011-07-16 05:13:38 +04:00
/*
* jQuery helper plugin for examples and tests
*/
(function( $ ){
2012-12-22 18:28:34 +04:00
$.fn.html2canvas = function(options) {
if (options && options.profile && window.console && window.console.profile) {
console.profile();
}
var date = new Date(),
html2obj,
$message = null,
timeoutTimer = false,
timer = date.getTime();
options = options || {};
2011-07-17 03:42:45 +04:00
2012-12-22 18:28:34 +04:00
options.onrendered = options.onrendered || function( canvas ) {
var $canvas = $(canvas),
finishTime = new Date();
2012-12-22 18:28:34 +04:00
if (options && options.profile && window.console && window.console.profileEnd) {
console.profileEnd();
}
$canvas.addClass("html2canvas")
.css({
position: 'absolute',
left: 0,
top: 0
}).appendTo(document.body);
2012-12-22 18:28:34 +04:00
if (window.location.search !== "?selenium") {
$canvas.siblings().toggle();
$(window).click(function(){
$canvas.toggle().siblings().toggle();
throwMessage("Canvas Render " + ($canvas.is(':visible') ? "visible" : "hidden"));
});
throwMessage('Screenshot created in '+ ((finishTime.getTime()-timer)) + " ms<br />",4000);
} else {
$canvas.css('display', 'none');
}
// test if canvas is read-able
try {
$canvas[0].toDataURL();
} catch(e) {
if ($canvas[0].nodeName.toLowerCase() === "canvas") {
// TODO, maybe add a bit less offensive way to present this, but still something that can easily be noticed
alert("Canvas is tainted, unable to read data");
2011-07-16 05:13:38 +04:00
}
2012-12-22 18:28:34 +04:00
}
2011-07-16 05:13:38 +04:00
};
2012-12-22 18:28:34 +04:00
html2obj = html2canvas(this, options);
function throwMessage(msg,duration){
window.clearTimeout(timeoutTimer);
timeoutTimer = window.setTimeout(function(){
$message.fadeOut(function(){
$message.remove();
$message = null;
});
},duration || 2000);
if ($message)
$message.remove();
$message = $('<div />').html(msg).css({
margin:0,
padding:10,
background: "#000",
opacity:0.7,
position:"fixed",
top:10,
right:10,
fontFamily: 'Tahoma',
color:'#fff',
fontSize:12,
borderRadius:12,
width:'auto',
height:'auto',
textAlign:'center',
textDecoration:'none',
display:'none'
}).appendTo(document.body).fadeIn();
html2obj.log(msg);
}
};
2011-07-16 05:13:38 +04:00
})( jQuery );