Skip to content

Instantly share code, notes, and snippets.

@spite
Last active May 28, 2020 14:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spite/93883bb7ad7de7b9bc62 to your computer and use it in GitHub Desktop.
Save spite/93883bb7ad7de7b9bc62 to your computer and use it in GitHub Desktop.
console.log for HTMLCanvasElement
/*
add this code
run console.log( canvas );
see the canvas
if the canvas is running a webgl context, it'll need the preserveDrawingBuffer flag set to true
*/
( function() {
var _oldConsole = console.log;
// Code from https://github.com/adriancooney/console.image
function getBox(width, height) {
return {
string: "+",
style: "font-size: 1px; padding: " + Math.floor(height/2) + "px " + Math.floor(width/2) + "px; line-height: " + height + "px;"
}
}
function logImage(url, scale) {
scale = scale || 1;
var img = new Image();
img.onload = function() {
var dim = getBox(this.width * scale, this.height * scale);
console.log("%c" + dim.string, dim.style + "background: url(" + url + "); background-size: " + (this.width * scale) + "px " + (this.height * scale) + "px; color: transparent;");
};
img.src = url;
};
console.log = function() {
var special = false;
[].forEach.call( arguments, function( a ) {
if( a instanceof HTMLCanvasElement ) special = true;;
} );
if( special ) {
[].forEach.call( arguments, function( a ) {
_oldConsole.apply( console, [ a ] );
if( a instanceof HTMLCanvasElement ) {
logImage( a.toDataURL() );
}
} );
} else {
_oldConsole.apply( console, arguments );
}
}
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment