Last active
June 21, 2017 21:36
-
-
Save mcansh/71248a12fa17f61bc075e54b64e29960 to your computer and use it in GitHub Desktop.
Lightweight console.image
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 = (url, scale = 1) => { | |
if (!url) return; | |
const img = new Image(); | |
img.onload = function () { | |
// way easier to see whats going on when its an array of styles | |
const styles = [ | |
`background: url("${url}")`, | |
`background-size: ${(this.width * this.scale)}px ${(this.height * this.scale)}px`, | |
'color: transparent', | |
'font-size: 1px', | |
`padding: ${Math.floor(this.height / 2)}px ${Math.floor(this.width / 2)}px`, | |
`line-height: ${this.height}px; ` | |
].join('; '); | |
console.log('%c+', styles); | |
}; | |
img.src = url; | |
}; | |
// Business Baboon is my spirit animal | |
console.image('https://media1.giphy.com/media/l41lVsYDBC0UVQJCE/200.gif#1-grid1'); | |
// usage with es6 isnt super great, but you can use | |
import * as anythingYouWant from './path/to/console.image.js'; | |
// then because its attached to the console you can still use | |
console.image('https://media1.giphy.com/media/l41lVsYDBC0UVQJCE/200.gif#1-grid1'); | |
// and it'll work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment