@@ -18,8 +18,8 @@ const ratingLevels: any = [
1818
1919const canvasRef = ref <HTMLCanvasElement | null >(null )
2020
21- const getNum = (id : string ) => new URL (` ../../assets/icons/rating/num/UI_CMN_Num_26p_${id }.png ` , import . meta . url ). href
22- const getBase = (id : string ) => new URL (` ../../assets/icons/rating/UI_CMA_Rating_Base_${id }.png ` , import . meta . url ). href
21+ const getNum = async (id : string ) => ( await import (` ../../assets/icons/rating/num/UI_CMN_Num_26p_${id }.png ` )). default
22+ const getBase = async (id : string ) => ( await import (` ../../assets/icons/rating/UI_CMA_Rating_Base_${id }.png ` )). default
2323
2424function countOccurrences(str : string , searchTerm : string ) {
2525 let count = 0
@@ -31,20 +31,19 @@ function countOccurrences(str: string, searchTerm: string) {
3131 return count
3232}
3333
34- const numImages = computed (() => {
34+ const numImageIds = computed (() => {
3535 const numValue = props .rating ?.replace (/ \+ / g , ' ' ).replace (/ -/ g , ' ' )
3636 if (! numValue || Number .isNaN (Number (numValue )))
3737 return []
3838 const arr = numValue .split (' ' )
3939 while (arr .length < 5 ) arr .unshift (' 10' )
40- const finalValue = arr .map (num => getNum (num ))
41- return finalValue .slice (0 , 5 )
40+ return arr .slice (0 , 5 )
4241})
4342
44- const baseImage = computed (() => {
43+ const baseImageId = computed (() => {
4544 const numValue = props .rating ?.replace (/ \+ / g , ' ' ).replace (/ -/ g , ' ' )
4645 if (! numValue || Number .isNaN (Number (numValue )))
47- return getBase ( ' 0' )
46+ return ' 0'
4847 let rating = Number .parseInt (numValue )
4948 rating = Math .max (ratingLevels [0 ], Math .min (rating , ratingLevels [9 ]))
5049 let stage = 0
@@ -53,7 +52,7 @@ const baseImage = computed(() => {
5352 const sideEffect = countOccurrences (props .rating || ' ' , ' +' ) - countOccurrences (props .rating || ' ' , ' -' )
5453 const finalValue = Math .max (Math .min (stage + 1 + sideEffect , 10 ), 0 )
5554
56- return getBase ( String (finalValue ) )
55+ return String (finalValue )
5756})
5857
5958async function drawCanvas() {
@@ -68,14 +67,19 @@ async function drawCanvas() {
6867
6968 ctx .clearRect (0 , 0 , canvas .width , canvas .height )
7069
70+ const [baseSrc, numSrcs] = await Promise .all ([
71+ getBase (baseImageId .value ),
72+ Promise .all (numImageIds .value .map (id => getNum (id ))),
73+ ])
74+
7175 const baseImg = new Image ()
7276 baseImg .fetchPriority = ' high'
73- baseImg .src = baseImage . value
77+ baseImg .src = baseSrc
7478
7579 baseImg .onload = () => {
7680 ctx .drawImage (baseImg , 0 , 0 , canvas .width , canvas .height )
7781
78- const numImgPromises = numImages . value .map ((src ) => {
82+ const numImgPromises = numSrcs .map ((src ) => {
7983 const img = new Image ()
8084 img .fetchPriority = ' high'
8185 img .src = src
@@ -96,7 +100,7 @@ async function drawCanvas() {
96100}
97101
98102onMounted (drawCanvas )
99- watch ([() => props .rating , baseImage , numImages ], drawCanvas )
103+ watch ([() => props .rating , baseImageId , numImageIds ], drawCanvas )
100104 </script >
101105
102106<template >
0 commit comments