Make pixel art more reliably get pixelated rendering (#1077)

* Make pixel art more reliably get pixelated rendering Closes #289

* Run prettier
This commit is contained in:
Prospector 2023-04-07 18:38:21 -07:00 committed by GitHub
parent 8d85158f91
commit c731515b94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,13 @@
<img
v-if="src"
ref="img"
:class="`avatar size-${size} ${circle ? 'circle' : ''} ${noShadow ? 'no-shadow' : ''}`"
:class="`avatar size-${size} ${circle ? 'circle' : ''} ${noShadow ? 'no-shadow' : ''} ${
pixelated ? 'pixelated' : ''
}`"
:src="src"
:alt="alt"
:loading="loading"
@load="updatePixelated"
/>
<svg
v-else
@ -60,21 +63,20 @@ export default {
default: 'eager',
},
},
mounted() {
if (this.$refs.img && this.$refs.img.naturalWidth) {
const isPixelated = () => {
if (this.$refs.img.naturalWidth < 96 && this.$refs.img.naturalWidth > 0) {
this.$refs.img.style.imageRendering = 'pixelated'
}
}
if (this.$refs.img.naturalWidth) {
isPixelated()
} else {
this.$refs.img.onload = isPixelated
}
data() {
return {
pixelated: false,
}
},
methods: {
updatePixelated() {
if (this.$refs.img && this.$refs.img.naturalWidth && this.$refs.img.naturalWidth <= 96) {
this.pixelated = true
} else {
this.pixelated = false
}
},
},
}
</script>
@ -116,5 +118,9 @@ export default {
&.no-shadow {
box-shadow: none;
}
&.pixelated {
image-rendering: pixelated;
}
}
</style>