yandere_fe/src/components/still-image/still-image.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-28 23:47:26 -08:00
const StillImage = {
props: [
'src',
'referrerpolicy',
'mimetype',
'imageLoadError',
2019-02-17 21:03:26 -08:00
'imageLoadHandler',
2022-02-03 12:50:32 -08:00
'alt',
'height',
'width',
'dataSrc'
2018-01-28 23:47:26 -08:00
],
data () {
return {
// for lazy loading, see loadLazy()
realSrc: this.src,
stopGifs: this.$store.getters.mergedConfig.stopGifs
}
},
2018-01-28 23:47:26 -08:00
computed: {
animated () {
if (!this.realSrc) {
return false
}
return this.stopGifs && (this.mimetype === 'image/gif' || this.realSrc.endsWith('.gif'))
2022-02-03 12:50:32 -08:00
},
style () {
const appendPx = (str) => /\d$/.test(str) ? str + 'px' : str
return {
height: this.height ? appendPx(this.height) : null,
width: this.width ? appendPx(this.width) : null
}
2018-01-28 23:47:26 -08:00
}
},
methods: {
loadLazy () {
if (this.dataSrc) {
this.realSrc = this.dataSrc
}
},
onLoad () {
if (!this.realSrc) {
return
}
2020-09-29 03:18:37 -07:00
const image = this.$refs.src
if (!image) return
this.imageLoadHandler && this.imageLoadHandler(image)
2018-01-28 23:47:26 -08:00
const canvas = this.$refs.canvas
if (!canvas) return
2020-09-29 03:18:37 -07:00
const width = image.naturalWidth
const height = image.naturalHeight
canvas.width = width
canvas.height = height
2020-09-29 03:18:37 -07:00
canvas.getContext('2d').drawImage(image, 0, 0, width, height)
2019-02-05 07:17:50 -08:00
},
onError () {
this.imageLoadError && this.imageLoadError()
2018-01-28 23:47:26 -08:00
}
2022-01-17 20:41:11 -08:00
},
watch: {
src () {
this.realSrc = this.src
},
dataSrc () {
this.$el.removeAttribute('data-loaded')
}
2018-01-28 23:47:26 -08:00
}
}
export default StillImage