Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Replace plain `<img>` tag with `VAudioThumbnail` component (#647)
Dhruv Bhanushalib77cb4f138ec
diff --git a/src/components/AudioDetails/VAudioDetails.vue b/src/components/AudioDetails/VAudioDetails.vueindex fb3a4ff3b..7718fd06a 100644--- a/src/components/AudioDetails/VAudioDetails.vue+++ b/src/components/AudioDetails/VAudioDetails.vue@@ -3,10 +3,10 @@<section class="audio-info"><h4 class="b-header mb-6">Audio information</h4><div class="mb-6 audio-info__grid">- <img :src="audio.thumbnail" alt="thumbnail" width="110" height="110" />+ <VAudioThumbnail :audio="audio" /><div class="audio-info__data">- <p>{{ audio.description }}</p>- <VAudioTags :tags="audio.tags" :show-header="false" class="mt-6 mb-6" />+ <p v-if="audio.description" class="mb-6">{{ audio.description }}</p>+ <VAudioTags :tags="audio.tags" :show-header="false" class="mb-6" /><dl v-if="audio"><div v-if="audio.audio_set"><dt>{{ $t('audio-details.table.album') }}</dt>diff --git a/src/components/VAudioThumbnail/VAudioThumbnail.vue b/src/components/VAudioThumbnail/VAudioThumbnail.vueindex 46d1a3e48..2685e7dc0 100644--- a/src/components/VAudioThumbnail/VAudioThumbnail.vue+++ b/src/components/VAudioThumbnail/VAudioThumbnail.vue@@ -1,38 +1,37 @@<template><!-- Should be wrapped by a fixed-width parent -->- <div class="relative h-0 w-full pt-full" :title="helpText">- <div class="thumbnail absolute inset-0 bg-yellow">+ <div class="relative h-0 w-full pt-full bg-yellow" :title="helpText">+ <!-- Programmatic thumbnail -->+ <svg class="absolute inset-0" :viewBox="`0 0 ${canvasSize} ${canvasSize}`">+ <template v-for="i in dotCount">+ <circle+ v-for="j in dotCount"+ :key="`${i}-${j}`"+ class="fill-dark-charcoal"+ :cx="offset(j)"+ :cy="offset(i)"+ :r="radius(i, j)"+ />+ </template>+ </svg>++ <div v-show="audio.thumbnail && isOk" class="thumbnail absolute inset-0"><img- v-if="audio.thumbnail && ok"+ ref="imgEl"class="h-full w-full object-cover object-center overflow-clip":src="audio.thumbnail":alt="helpText"- @error="handleError"+ @load="handleLoad"/>-- <!-- Programmatic thumbnail -->- <svg- v-else- class="h-full w-full"- :viewBox="`0 0 ${canvasSize} ${canvasSize}`"- >- <template v-for="i in dotCount">- <circle- v-for="j in dotCount"- :key="`${i}-${j}`"- class="fill-dark-charcoal"- :cx="offset(j)"- :cy="offset(i)"- :r="radius(i, j)"- />- </template>- </svg></div></div></template><script>-import { ref } from '@nuxtjs/composition-api'+import { ref, onMounted } from '@nuxtjs/composition-api'++import { rand, hash } from '~/utils/prng'+import { lerp, dist, bezier } from '~/utils/math'/*** Displays the cover art for the audio in a square aspect ratio.@@ -49,51 +48,17 @@ export default {required: true,},},- setup() {+ setup(props) {/* Switching */- const ok = ref(true)- const handleError = () => {- ok.value = false+ const imgEl = ref(null)+ const isOk = ref(false)+ const handleLoad = () => {+ isOk.value = true}-- /* Math utilities */-- /**- * Perform linear interpolation to find a value that is fractionally between- * the low and high limits of the given range.- *- * @param {number} low - the lower limit of the range- * @param {number} high - the upper limit of the range- * @param {number} frac - fraction controlling position of interpolated number- * @returns {number} the interpolated number- */- const lerp = (low, high, frac) => low + (high - low) * frac-- /**- * Interpolate twice to solve the Bézier equation for three points P0, P1- * and P2.- *- * @param {[number, number]} p0 - point #0- * @param {[number, number]} p1 - point #1- * @param {[number, number]} p2 - point #2- * @param {number} frac - the fraction at which to solve the Bézier equation- * @returns {[number,number]} a solution to the 3-point Bézier equation- */- const doubleLerp = (p0, p1, p2, frac) => [- lerp(lerp(p0[0], p1[0], frac), lerp(p1[0], p2[0], frac), frac),- lerp(lerp(p0[1], p1[1], frac), lerp(p1[1], p2[1], frac), frac),- ]-- /**- * Find the distance between two points P0 and P1.- *- * @param {[number, number]} p0 - point #0- * @param {[number, number]} p1 - point #1- * @returns {number} the distance between the two points- */- const dist = (p0, p1) =>- Math.sqrt(Math.pow(p0[0] - p1[0], 2) + Math.pow(p0[1] - p1[1], 2))+ onMounted(() => {+ isOk.value = imgEl.value.complete && imgEl.value.naturalWidth+ })/* Artwork */@@ -102,20 +67,14 @@ export default {const minRadius = 2const maxRadius = 27+ const random = rand(hash(props.audio.title ?? ''))const ctrlPts = Array.from({ length: 4 }, (_, idx) => [- Math.random() * canvasSize,+ random() * canvasSize,(idx / 3) * canvasSize,])const pointCount = dotCount + 1- const bezierPoints = []- for (let i = 0; i <= pointCount; i++) {- const frac = i / pointCount- const a = doubleLerp(ctrlPts[0], ctrlPts[1], ctrlPts[2], frac)- const b = doubleLerp(ctrlPts[1], ctrlPts[2], ctrlPts[3], frac)- const x = lerp(a[0], b[0], frac)- bezierPoints.push(x)- }+ const bezierPoints = bezier(ctrlPts, pointCount)const offset = (i) => {return i * (canvasSize / (dotCount + 1))@@ -128,8 +87,9 @@ export default {}return {- ok,- handleError,+ imgEl,+ isOk,+ handleLoad,canvasSize,dotCount,diff --git a/src/utils/math.js b/src/utils/math.jsnew file mode 100644index 000000000..f09e0cb2b--- /dev/null+++ b/src/utils/math.js@@ -0,0 +1,59 @@+/**+ * Math utilities+ */++/**+ * Perform linear interpolation to find a value that is fractionally between+ * the low and high limits of the given range.+ *+ * @param {number} low - the lower limit of the range+ * @param {number} high - the upper limit of the range+ * @param {number} frac - fraction controlling position of interpolated number+ * @returns {number} the interpolated number+ */+export const lerp = (low, high, frac) => low + (high - low) * frac++/**+ * Interpolate twice to solve the Bézier equation for three points P0, P1+ * and P2.+ *+ * @param {[number, number]} p0 - point #0+ * @param {[number, number]} p1 - point #1+ * @param {[number, number]} p2 - point #2+ * @param {number} frac - the fraction at which to solve the Bézier equation+ * @returns {[number,number]} a solution to the 3-point Bézier equation+ */+export const doubleLerp = (p0, p1, p2, frac) => [+ lerp(lerp(p0[0], p1[0], frac), lerp(p1[0], p2[0], frac), frac),+ lerp(lerp(p0[1], p1[1], frac), lerp(p1[1], p2[1], frac), frac),+]++/**+ * Find the distance between two points P0 and P1.+ *+ * @param {[number, number]} p0 - point #0+ * @param {[number, number]} p1 - point #1+ * @returns {number} the distance between the two points+ */+export const dist = (p0, p1) =>+ Math.sqrt(Math.pow(p0[0] - p1[0], 2) + Math.pow(p0[1] - p1[1], 2))++/**+ * Get the required number of x-coordinates of Bézier points using 4 control+ * points.+ *+ * @param {[number, number][]} ctrlPts - the array of control points+ * @param {number} pointCount - the number of Bézier points to calculate+ * @return {[number]} the list of x-coordinates of Bézier points+ */+export const bezier = (ctrlPts, pointCount) => {+ const bezierPoints = []+ for (let i = 0; i <= pointCount; i++) {+ const frac = i / pointCount+ const a = doubleLerp(ctrlPts[0], ctrlPts[1], ctrlPts[2], frac)+ const b = doubleLerp(ctrlPts[1], ctrlPts[2], ctrlPts[3], frac)+ const x = lerp(a[0], b[0], frac)+ bezierPoints.push(x)+ }+ return bezierPoints+}diff --git a/src/utils/prng.js b/src/utils/prng.jsnew file mode 100644index 000000000..b3bc9c036--- /dev/null+++ b/src/utils/prng.js@@ -0,0 +1,57 @@+/**+ * Utilities for generating pseudo-random numbers.+ */++/**+ * Small Fast Counter is a seedable pseudo-random number generator. It accepts 4+ * numbers as seeds.+ * @see {@link https://github.com/bryc/code/blob/master/jshash/PRNGs.md#sfc32}+ *+ * @param {number} a - first seed for SFC32+ * @param {number} b - second seed for SFC32+ * @param {number} c - third seed for SFC32+ * @param {number} d - fourth seed for SFC32+ * @return {() => number} the seeded PRNG+ */+const sfc32 = (a, b, c, d) => () => {+ a |= 0+ b |= 0+ c |= 0+ d |= 0+ let t = (((a + b) | 0) + d) | 0+ d = (d + 1) | 0+ a = b ^ (b >>> 9)+ b = (c + (c << 3)) | 0+ c = (c << 21) | (c >>> 11)+ c = (c + t) | 0+ return (t >>> 0) / 4294967296+}++/**+ * Get a seedable pseudo-random number generator based on SFC32 that accepts 1+ * number as the seed. It uses three other arbitrary seeds.+ *+ * @param {number} d - fourth seed for SFC32+ * @returns {() => number} the seeded PRNG+ */+export const rand = (d) => sfc32(0x9e3779b9, 0x243f6a88, 0xb7e15162, d)++/**+ * Hash a string into a 32-bit number that can be used as the seed for a+ * pseudo-random number generator.+ *+ * @param {string} str - the string to hash+ * @return {number} the number generated from hashing the string+ */+export const hash = (str) => {+ let hash = 0+ if (str.length === 0) {+ return hash+ }+ for (let i = 0; i < str.length; i++) {+ let chr = str.charCodeAt(i)+ hash = (hash << 5) - hash + chr+ hash |= 0 // Convert to 32-bit integer+ }+ return hash+}