2023-12-21 00:18:21 +01:00
|
|
|
<template>
|
|
|
|
<div>
|
2023-12-23 22:29:34 +01:00
|
|
|
<div v-if="processing" class="max-w-[800px] h-80 md:h-[800px] mx-auto flex items-center justify-center">
|
2023-12-21 00:18:21 +01:00
|
|
|
<widgets-loading-spinner />
|
|
|
|
</div>
|
2023-12-23 22:29:34 +01:00
|
|
|
<img v-else-if="dataUrl" :src="dataUrl" class="mx-auto" />
|
2023-12-21 00:18:21 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
2023-12-23 22:29:34 +01:00
|
|
|
variant: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
year: Number,
|
2023-12-21 00:18:21 +01:00
|
|
|
processing: Boolean
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-12-23 22:29:34 +01:00
|
|
|
canvas: null,
|
2023-12-21 00:18:21 +01:00
|
|
|
dataUrl: null,
|
|
|
|
yearStats: null
|
|
|
|
}
|
|
|
|
},
|
2023-12-23 22:29:34 +01:00
|
|
|
watch: {
|
|
|
|
variant() {
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
},
|
2023-12-21 00:18:21 +01:00
|
|
|
methods: {
|
|
|
|
async initCanvas() {
|
|
|
|
if (!this.yearStats) return
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas')
|
2023-12-23 00:01:07 +01:00
|
|
|
canvas.width = 800
|
|
|
|
canvas.height = 800
|
2023-12-21 00:18:21 +01:00
|
|
|
const ctx = canvas.getContext('2d')
|
|
|
|
|
|
|
|
const createRoundedRect = (x, y, w, h) => {
|
2023-12-23 00:01:07 +01:00
|
|
|
const grd1 = ctx.createLinearGradient(x, y, x + w, y + h)
|
2023-12-23 23:39:56 +01:00
|
|
|
grd1.addColorStop(0, '#44444455')
|
|
|
|
grd1.addColorStop(1, '#ffffff11')
|
2023-12-23 00:01:07 +01:00
|
|
|
ctx.fillStyle = grd1
|
2023-12-23 23:39:56 +01:00
|
|
|
ctx.strokeStyle = '#C0C0C088'
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.beginPath()
|
|
|
|
ctx.roundRect(x, y, w, h, [20])
|
|
|
|
ctx.fill()
|
|
|
|
ctx.stroke()
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:44:37 +01:00
|
|
|
const addText = (text, fontSize, fontWeight, color, letterSpacing, x, y, maxWidth = 0) => {
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.fillStyle = color
|
|
|
|
ctx.font = `${fontWeight} ${fontSize} Source Sans Pro`
|
|
|
|
ctx.letterSpacing = letterSpacing
|
2023-12-21 16:44:37 +01:00
|
|
|
|
|
|
|
// If maxWidth is specified then continue to remove chars until under maxWidth and add ellipsis
|
|
|
|
if (maxWidth) {
|
|
|
|
let txtWidth = ctx.measureText(text).width
|
|
|
|
while (txtWidth > maxWidth) {
|
|
|
|
console.warn(`Text "${text}" is greater than max width ${maxWidth} (width:${txtWidth})`)
|
|
|
|
if (text.endsWith('...')) text = text.slice(0, -4) // Repeated checks remove 1 char at a time
|
|
|
|
else text = text.slice(0, -3) // First check remove last 3 chars
|
|
|
|
text += '...'
|
|
|
|
txtWidth = ctx.measureText(text).width
|
|
|
|
console.log(`Checking text "${text}" (width:${txtWidth})`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.fillText(text, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
const addIcon = (icon, color, fontSize, x, y) => {
|
|
|
|
ctx.fillStyle = color
|
2024-08-16 23:57:17 +02:00
|
|
|
ctx.font = `${fontSize} Material Symbols Rounded`
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.fillText(icon, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bg color
|
|
|
|
ctx.fillStyle = '#232323'
|
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
|
|
|
|
|
|
// Cover image tiles
|
2023-12-23 22:29:34 +01:00
|
|
|
const bookCovers = this.yearStats.finishedBooksWithCovers
|
|
|
|
bookCovers.push(...this.yearStats.booksWithCovers)
|
|
|
|
|
|
|
|
let finishedBookCoverImgs = {}
|
|
|
|
|
|
|
|
if (bookCovers.length) {
|
2023-12-21 00:18:21 +01:00
|
|
|
let index = 0
|
|
|
|
ctx.globalAlpha = 0.25
|
2023-12-23 00:01:07 +01:00
|
|
|
ctx.save()
|
|
|
|
ctx.translate(canvas.width / 2, canvas.height / 2)
|
|
|
|
ctx.rotate((-Math.PI / 180) * 25)
|
|
|
|
ctx.translate(-canvas.width / 2, -canvas.height / 2)
|
|
|
|
ctx.translate(-130, -120)
|
|
|
|
for (let x = 0; x < 5; x++) {
|
|
|
|
for (let y = 0; y < 5; y++) {
|
2023-12-23 22:29:34 +01:00
|
|
|
const coverIndex = index % bookCovers.length
|
|
|
|
let libraryItemId = bookCovers[coverIndex]
|
2023-12-21 00:18:21 +01:00
|
|
|
index++
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
const img = new Image()
|
|
|
|
img.crossOrigin = 'anonymous'
|
|
|
|
img.addEventListener('load', () => {
|
2023-12-23 00:01:07 +01:00
|
|
|
let sw = img.width
|
|
|
|
if (img.width > img.height) {
|
|
|
|
sw = img.height
|
|
|
|
}
|
|
|
|
let sx = -(sw - img.width) / 2
|
|
|
|
let sy = -(sw - img.height) / 2
|
|
|
|
ctx.drawImage(img, sx, sy, sw, sw, 215 * x, 215 * y, 215, 215)
|
2023-12-21 00:18:21 +01:00
|
|
|
resolve()
|
2023-12-23 22:29:34 +01:00
|
|
|
if (this.yearStats.finishedBooksWithCovers.includes(libraryItemId) && !finishedBookCoverImgs[libraryItemId]) {
|
|
|
|
finishedBookCoverImgs[libraryItemId] = {
|
|
|
|
img,
|
|
|
|
sx,
|
|
|
|
sy,
|
|
|
|
sw
|
|
|
|
}
|
|
|
|
}
|
2023-12-21 00:18:21 +01:00
|
|
|
})
|
|
|
|
img.addEventListener('error', () => {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
img.src = this.$store.getters['globals/getLibraryItemCoverSrcById'](libraryItemId)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 00:01:07 +01:00
|
|
|
ctx.restore()
|
2023-12-21 00:18:21 +01:00
|
|
|
}
|
|
|
|
|
2024-08-09 19:24:43 +02:00
|
|
|
const twoColumnWidth = 210
|
|
|
|
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.globalAlpha = 1
|
|
|
|
ctx.textBaseline = 'middle'
|
|
|
|
|
|
|
|
// Create gradient
|
2023-12-23 00:01:07 +01:00
|
|
|
const grd1 = ctx.createLinearGradient(0, 0, canvas.width, canvas.height)
|
2023-12-21 00:18:21 +01:00
|
|
|
grd1.addColorStop(0, '#000000aa')
|
|
|
|
grd1.addColorStop(1, '#cd9d49aa')
|
|
|
|
ctx.fillStyle = grd1
|
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
|
|
|
|
|
|
// Top Abs icon
|
|
|
|
let tanColor = '#ffdb70'
|
|
|
|
ctx.fillStyle = tanColor
|
2023-12-23 00:01:07 +01:00
|
|
|
ctx.font = '42px absicons'
|
|
|
|
ctx.fillText('\ue900', 15, 36)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
|
|
|
// Top text
|
2023-12-23 00:01:07 +01:00
|
|
|
addText('audiobookshelf', '28px', 'normal', tanColor, '0px', 65, 28)
|
2024-08-16 23:57:17 +02:00
|
|
|
addText(`${this.year} ${this.$strings.StatsYearInReview}`, '18px', 'bold', 'white', '1px', 65, 51)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
|
|
|
// Top left box
|
2023-12-23 00:01:07 +01:00
|
|
|
createRoundedRect(50, 100, 340, 160)
|
|
|
|
addText(this.yearStats.numBooksFinished, '64px', 'bold', 'white', '0px', 160, 165)
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsBooksFinished, '28px', 'normal', tanColor, '0px', 160, 210, twoColumnWidth)
|
2023-12-21 00:18:21 +01:00
|
|
|
const readIconPath = new Path2D()
|
2023-12-23 00:01:07 +01:00
|
|
|
readIconPath.addPath(new Path2D('M19 1H5c-1.1 0-1.99.9-1.99 2L3 15.93c0 .69.35 1.3.88 1.66L12 23l8.11-5.41c.53-.36.88-.97.88-1.66L21 3c0-1.1-.9-2-2-2zm-9 15l-5-5 1.41-1.41L10 13.17l7.59-7.59L19 7l-9 9z'), { a: 2, d: 2, e: 100, f: 160 })
|
2023-12-21 00:18:21 +01:00
|
|
|
ctx.fillStyle = '#ffffff'
|
|
|
|
ctx.fill(readIconPath)
|
|
|
|
|
|
|
|
// Box top right
|
2023-12-23 00:01:07 +01:00
|
|
|
createRoundedRect(410, 100, 340, 160)
|
|
|
|
addText(this.$elapsedPrettyExtended(this.yearStats.totalListeningTime, true, false), '40px', 'bold', 'white', '0px', 500, 165)
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsSpentListening, '28px', 'normal', tanColor, '0px', 500, 205, twoColumnWidth)
|
2023-12-23 00:01:07 +01:00
|
|
|
addIcon('watch_later', 'white', '52px', 440, 180)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
|
|
|
// Box bottom left
|
2023-12-23 00:01:07 +01:00
|
|
|
createRoundedRect(50, 280, 340, 160)
|
|
|
|
addText(this.yearStats.totalListeningSessions, '64px', 'bold', 'white', '0px', 160, 345)
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsSessions, '28px', 'normal', tanColor, '1px', 160, 390, twoColumnWidth)
|
2023-12-23 00:01:07 +01:00
|
|
|
addIcon('headphones', 'white', '52px', 95, 360)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
|
|
|
// Box bottom right
|
2023-12-23 00:01:07 +01:00
|
|
|
createRoundedRect(410, 280, 340, 160)
|
|
|
|
addText(this.yearStats.numBooksListened, '64px', 'bold', 'white', '0px', 500, 345)
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsBooksListenedTo, '28px', 'normal', tanColor, '0px', 500, 390, twoColumnWidth)
|
2023-12-23 00:01:07 +01:00
|
|
|
addIcon('local_library', 'white', '52px', 440, 360)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
2023-12-23 22:29:34 +01:00
|
|
|
if (!this.variant) {
|
|
|
|
// Text stats
|
|
|
|
const topNarrator = this.yearStats.mostListenedNarrator
|
|
|
|
if (topNarrator) {
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsTopNarrator, '24px', 'normal', tanColor, '1px', 70, 520, 330)
|
2023-12-23 22:29:34 +01:00
|
|
|
addText(topNarrator.name, '36px', 'bolder', 'white', '0px', 70, 564, 330)
|
|
|
|
addText(this.$elapsedPrettyExtended(topNarrator.time, true, false), '24px', 'lighter', 'white', '1px', 70, 599)
|
|
|
|
}
|
2023-12-21 00:18:21 +01:00
|
|
|
|
2023-12-23 22:29:34 +01:00
|
|
|
const topGenre = this.yearStats.topGenres[0]
|
|
|
|
if (topGenre) {
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsTopGenre, '24px', 'normal', tanColor, '1px', 430, 520, 330)
|
2023-12-23 22:29:34 +01:00
|
|
|
addText(topGenre.genre, '36px', 'bolder', 'white', '0px', 430, 564, 330)
|
|
|
|
addText(this.$elapsedPrettyExtended(topGenre.time, true, false), '24px', 'lighter', 'white', '1px', 430, 599)
|
|
|
|
}
|
|
|
|
|
|
|
|
const topAuthor = this.yearStats.topAuthors[0]
|
|
|
|
if (topAuthor) {
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsTopAuthor, '24px', 'normal', tanColor, '1px', 70, 670, 330)
|
2023-12-23 22:29:34 +01:00
|
|
|
addText(topAuthor.name, '36px', 'bolder', 'white', '0px', 70, 714, 330)
|
|
|
|
addText(this.$elapsedPrettyExtended(topAuthor.time, true, false), '24px', 'lighter', 'white', '1px', 70, 749)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.yearStats.mostListenedMonth?.time) {
|
|
|
|
const jsdate = new Date(this.year, this.yearStats.mostListenedMonth.month, 1)
|
|
|
|
const monthName = this.$formatJsDate(jsdate, 'LLLL')
|
2024-08-09 19:24:43 +02:00
|
|
|
addText(this.$strings.StatsTopMonth, '24px', 'normal', tanColor, '1px', 430, 670, 330)
|
2023-12-23 22:29:34 +01:00
|
|
|
addText(monthName, '36px', 'bolder', 'white', '0px', 430, 714, 330)
|
|
|
|
addText(this.$elapsedPrettyExtended(this.yearStats.mostListenedMonth.time, true, false), '24px', 'lighter', 'white', '1px', 430, 749)
|
|
|
|
}
|
|
|
|
} else if (this.variant === 1) {
|
|
|
|
// Bottom images
|
|
|
|
finishedBookCoverImgs = Object.values(finishedBookCoverImgs)
|
|
|
|
if (finishedBookCoverImgs.length > 0) {
|
|
|
|
ctx.textAlign = 'center'
|
2024-08-09 18:48:29 +02:00
|
|
|
addText(this.$strings.StatsBooksFinishedThisYear, '28px', 'normal', tanColor, '0px', canvas.width / 2, 530)
|
2023-12-21 00:18:21 +01:00
|
|
|
|
2023-12-23 22:29:34 +01:00
|
|
|
for (let i = 0; i < Math.min(5, finishedBookCoverImgs.length); i++) {
|
|
|
|
let imgToAdd = finishedBookCoverImgs[i]
|
|
|
|
ctx.drawImage(imgToAdd.img, imgToAdd.sx, imgToAdd.sy, imgToAdd.sw, imgToAdd.sw, 40 + 145 * i, 570, 140, 140)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (this.variant === 2) {
|
|
|
|
// Text stats
|
|
|
|
if (this.yearStats.topAuthors.length) {
|
2024-08-09 18:48:29 +02:00
|
|
|
addText(this.$strings.StatsTopAuthors, '24px', 'normal', tanColor, '1px', 70, 524)
|
2023-12-23 22:29:34 +01:00
|
|
|
for (let i = 0; i < this.yearStats.topAuthors.length; i++) {
|
|
|
|
addText(this.yearStats.topAuthors[i].name, '36px', 'bolder', 'white', '0px', 70, 584 + i * 60, 330)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.yearStats.topGenres.length) {
|
2024-08-09 18:48:29 +02:00
|
|
|
addText(this.$strings.StatsTopGenres, '24px', 'normal', tanColor, '1px', 430, 524)
|
2023-12-23 22:29:34 +01:00
|
|
|
for (let i = 0; i < this.yearStats.topGenres.length; i++) {
|
|
|
|
addText(this.yearStats.topGenres[i].genre, '36px', 'bolder', 'white', '0px', 430, 584 + i * 60, 330)
|
|
|
|
}
|
|
|
|
}
|
2023-12-21 00:18:21 +01:00
|
|
|
}
|
|
|
|
|
2023-12-23 22:29:34 +01:00
|
|
|
this.canvas = canvas
|
2023-12-21 00:18:21 +01:00
|
|
|
this.dataUrl = canvas.toDataURL('png')
|
|
|
|
},
|
|
|
|
refresh() {
|
|
|
|
this.init()
|
|
|
|
},
|
2023-12-23 22:29:34 +01:00
|
|
|
share() {
|
|
|
|
this.canvas.toBlob((blob) => {
|
|
|
|
const file = new File([blob], 'yearinreview.png', { type: blob.type })
|
|
|
|
const shareData = {
|
|
|
|
files: [file]
|
|
|
|
}
|
|
|
|
if (navigator.canShare(shareData)) {
|
|
|
|
navigator
|
|
|
|
.share(shareData)
|
|
|
|
.then(() => {
|
|
|
|
console.log('Share success')
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed to share', error)
|
2023-12-23 23:39:56 +01:00
|
|
|
if (error.name !== 'AbortError') {
|
2024-08-31 00:47:49 +02:00
|
|
|
this.$toast.error(this.$strings.ToastFailedToShare + ': ' + error.message)
|
2023-12-23 23:39:56 +01:00
|
|
|
}
|
2023-12-23 22:29:34 +01:00
|
|
|
})
|
|
|
|
} else {
|
2024-08-09 18:48:29 +02:00
|
|
|
this.$toast.error(this.$strings.ToastErrorCannotShare)
|
2023-12-23 22:29:34 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2023-12-21 00:18:21 +01:00
|
|
|
async init() {
|
|
|
|
this.$emit('update:processing', true)
|
2023-12-23 22:29:34 +01:00
|
|
|
this.yearStats = await this.$axios.$get(`/api/me/stats/year/${this.year}`).catch((err) => {
|
2023-12-21 00:18:21 +01:00
|
|
|
console.error('Failed to load stats for year', err)
|
2024-05-13 23:58:41 +02:00
|
|
|
this.$toast.error(this.$strings.ToastFailedToLoadData)
|
2023-12-21 00:18:21 +01:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
await this.initCanvas()
|
|
|
|
this.$emit('update:processing', false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 23:58:41 +02:00
|
|
|
</script>
|