Created
November 5, 2022 02:02
-
-
Save taozhiyu/9045d392340b7a21814ebe67d99d66e9 to your computer and use it in GitHub Desktop.
print image in console
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
console.image = function(url, scale) { | |
scale = scale || 1; | |
const img = new Image(), | |
canvas = document.createElement('CANVAS'); | |
img.crossOrigin = 'Anonymous'; | |
img.onload = function() { | |
const {width, height} = img; | |
canvas.height = height; | |
canvas.width = width; | |
canvas.getContext('2d').drawImage(img, 0, 0); | |
const dataURL = canvas.toDataURL('image/png'); | |
const styleJson = { | |
padding: Math.floor(height / 2*scale) + "px " + Math.floor(width / 2*scale) + "px", | |
background:`url(${dataURL}) center center no-repeat`, | |
'background-size':`${width * scale}px ${height * scale}px`, | |
color: "transparent", | |
} | |
console.log("%c+",Object.entries(styleJson).map(a=>a.join(":")).join(";")); | |
}; | |
img.src = url; | |
}; | |
console.image("https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy the code above, open the console, and run it directly to see the github logo in the console