Skip to content

Commit

Permalink
fix: 替换文本
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Jan 9, 2025
1 parent 710d612 commit 168ed61
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions models/canvas/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ export function toImage (canvas) {
* @param {number} maxWidth
* @param {string} replace
*/
export function shortenText (ctx, text, maxWidth, replace = '') {
export function shortenText (ctx, text, maxWidth, replace = '...') {
if (!text) return ''
if (ctx.measureText(text).width <= maxWidth) return text
maxWidth += ctx.measureText(replace).width
if (ctx.measureText(text).width < maxWidth) return text
while (ctx.measureText(text).width > maxWidth) {
text = text.slice(0, -1)
}
Expand Down

0 comments on commit 168ed61

Please sign in to comment.