Last active
May 28, 2020 14:15
-
-
Save spite/93883bb7ad7de7b9bc62 to your computer and use it in GitHub Desktop.
console.log for HTMLCanvasElement
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
/* | |
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