Skip to content

Commit

Permalink
fix: unable to display image in new version of chrome (close #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
u3u committed Aug 3, 2018
1 parent d4bcf77 commit 4d93227
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
console.img = function(url) {
console.img = function(url, ...args) {
if (!/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(url)) {
console.warn('Image URL is incorrect')
return
Expand All @@ -14,16 +14,27 @@ console.img = function(url) {
})
}

img.onload = evt => {
img.addEventListener('load', evt => {
const image = evt.target
const width = (arguments[1] || image.width) / 2
const height = (arguments[2] || image.height) / 2
const width = (args[0] || image.width) / 2
const height = (args[1] || image.height) / 2
const lineHeight = height * 2

const [, version] = navigator.userAgent.match(/Chrome\/([\d|.]+) /) || []
const needLineHeight = version ? version < '68.0.3440.84' : true

console.log(
'%c',
`font-size: 0; padding: ${height}px ${width}px; line-height: ${lineHeight}px; background: url(${url}) no-repeat 50% / 100%`
'%c ',
[
'font-size: 0',
`padding: ${height}px ${width}px`,
needLineHeight && `line-height: ${lineHeight}px`,
`background: url(${url}) no-repeat 50% / 100%`
]
.filter(Boolean)
.join('; ')
)
}
})
img.src = url

return promise
Expand Down

0 comments on commit 4d93227

Please sign in to comment.