Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 2x 2x 2x 2x | import { IAllLines, IFormatLyricsResponse, LineType } from "types/lyrics"; export function getLinesFittingCanvas( ctx: CanvasRenderingContext2D, text: string, maxWidth: number ): string[] { const words = text.split(" "); const lines = []; let currentLine = words[0]; for (let i = 1; i < words.length; i++) { const word = words[i]; const width = ctx.measureText(currentLine + " " + word).width; if (width < maxWidth) { currentLine += " " + word; } else { lines.push(currentLine); currentLine = word; } } lines.push(currentLine); return lines; } interface IGetLineInfo { currentLine: IFormatLyricsResponse["lines"][0]; nextLine: IFormatLyricsResponse["lines"][0]; lyricLineColor: string; lyricTextColor: string; lyricsProgressMs: number; index: number; } interface IGetLineType { currentLine: IFormatLyricsResponse["lines"][0]; nextLine: IFormatLyricsResponse["lines"][0]; lyricsProgressMs: number; index: number; } export function getLineType({ currentLine, lyricsProgressMs, nextLine, index, }: IGetLineType): LineType { const isPreviousLine = currentLine.startTimeMs && Number(currentLine.startTimeMs) <= lyricsProgressMs; const isNextLine = nextLine?.startTimeMs && Number(nextLine?.startTimeMs) >= lyricsProgressMs; const isCurrentLine = isPreviousLine && isNextLine; const isFirstLine = index === 0; const lineTypes: Record<LineType, boolean> = { current: !!isCurrentLine, previous: !!isPreviousLine, first: isFirstLine, next: !!isNextLine, }; const type = Object.keys(lineTypes).find( (key) => lineTypes[key as keyof typeof lineTypes] ) as keyof typeof lineTypes; return type; } export function getLineInfo({ currentLine, lyricLineColor, lyricTextColor, lyricsProgressMs, nextLine, index, }: IGetLineInfo): { color: string; text: string; type: LineType; } { const type = getLineType({ currentLine, lyricsProgressMs, nextLine, index, }); const lineColors: Record<LineType, string> = { current: "#fff", previous: lyricLineColor + "80", next: lyricTextColor, first: lyricTextColor, }; return { color: lineColors[type], text: currentLine.words, type, }; } interface IgetAllLinesFittingWidth { lines: IFormatLyricsResponse["lines"]; ctx: CanvasRenderingContext2D; lyricLineColor: string; lyricTextColor: string; lyricsProgressMs: number; canvasWidth?: number; } export function getAllLinesFittingWidth({ ctx, lines, lyricLineColor, lyricTextColor, lyricsProgressMs, canvasWidth, }: IgetAllLinesFittingWidth): IAllLines[] { const allLines: IAllLines[] = []; lines.forEach((line, i) => { const lineInfo = getLineInfo({ currentLine: line, lyricLineColor, lyricsProgressMs, lyricTextColor, nextLine: lines[i + 1], index: i, }); const linesText = getLinesFittingCanvas( ctx, line.words ?? "", (canvasWidth ?? 10) - 20 ); linesText.forEach((lineText) => { allLines.push({ color: lineInfo.color, text: lineText, type: lineInfo.type, }); }); }); return allLines; } |