Skip to content

Commit

Permalink
fix: canvas加载图片增加超时时间
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Jan 9, 2025
1 parent 3404ca0 commit 710d612
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions models/canvas/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,29 @@ export const hasCanvas = !!canvasPKG

const startTimeMap = new Map()

export const loadImage = hasCanvas && canvasPKG.loadImage
/**
* 加载图片
* @param {any} source
* @param {import('@napi-rs/canvas').LoadImageOptions} options
* @returns {Promise<import('@napi-rs/canvas').Image>}
*/
export const loadImage = async (source, options = {}) => {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), 5000)
try {
const Image = await canvasPKG.loadImage(source, {
requestOptions: {
signal: controller.signal,
...options.requestOptions
},
...options
})
clearTimeout(timer)
return Image
} catch {
return null
}
}

export function createCanvas (width, height) {
if (!hasCanvas) throw new Error('请先pnpm i 安装依赖')
Expand Down Expand Up @@ -63,7 +85,7 @@ 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
while (ctx.measureText(text).width > maxWidth) {
Expand Down

0 comments on commit 710d612

Please sign in to comment.