Skip to content

Commit

Permalink
Make pixel art more reliably get pixelated rendering (#1077)
Browse files Browse the repository at this point in the history
* Make pixel art more reliably get pixelated rendering Closes #289

* Run prettier
  • Loading branch information
Prospector authored Apr 8, 2023
1 parent 8d85158 commit c731515
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions components/ui/Avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,20 +63,19 @@ 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()
data() {
return {
pixelated: false,
}
},
methods: {
updatePixelated() {
if (this.$refs.img && this.$refs.img.naturalWidth && this.$refs.img.naturalWidth <= 96) {
this.pixelated = true
} else {
this.$refs.img.onload = isPixelated
this.pixelated = false
}
}
},
},
}
</script>
Expand Down Expand Up @@ -116,5 +118,9 @@ export default {
&.no-shadow {
box-shadow: none;
}
&.pixelated {
image-rendering: pixelated;
}
}
</style>

0 comments on commit c731515

Please sign in to comment.