Skip to content

Instantly share code, notes, and snippets.

@taozhiyu
Created November 5, 2022 02:02
Show Gist options
  • Save taozhiyu/9045d392340b7a21814ebe67d99d66e9 to your computer and use it in GitHub Desktop.
Save taozhiyu/9045d392340b7a21814ebe67d99d66e9 to your computer and use it in GitHub Desktop.
print image in console
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)
@taozhiyu
Copy link
Author

taozhiyu commented Nov 5, 2022

Copy the code above, open the console, and run it directly to see the github logo in the console

image

Because of csp restrictions, github can only load some of the images from github's sites, maybe you can try outside

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment