From 8d0dccd1e39e664e94d17d51a637c2ec48ecbcf6 Mon Sep 17 00:00:00 2001 From: Yasuo Date: Fri, 3 Sep 2021 20:06:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ package.json | 2 +- src/index.ts | 85 +++++++++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 81c3fb1..add3aee 100644 --- a/README.md +++ b/README.md @@ -72,3 +72,7 @@ json2canvas({ > 2021年08月18日 - 优化图片跨域 + +> 2021年09月03日 + +- 图片加载失败之后,继续绘制,跳过当前失败 diff --git a/package.json b/package.json index 794f163..0f7b54e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-to-canvas", - "version": "1.0.3", + "version": "1.0.4", "description": "json-to-canvas", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index 9c0d0b8..3e25dac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,47 +31,50 @@ export const json2canvas: Json2canvas = async (props) => { if (!err) sourceItem.url = res; }); } - const Image = await loadImage(sourceItem.url); - - if (callback) { - callback( - ctx, - { - width: canvas.width, - height: canvas.height - }, - { - x, - y, - width, - height, - borderColor, - lineWidth, - url: sourceItem.url, - image: Image - } - ); - } else if (name === ImageName.Avatar) { - // 在 clip() 之前保存canvas状态 - ctx.save(); - ctx.strokeStyle = borderColor; - ctx.lineWidth = lineWidth * scale; - ctx.beginPath(); - ctx.arc( - x + width / 2, - y + width / 2, - width / 2, - 0, - 2 * Math.PI, - false - ); - ctx.stroke(); - ctx.clip(); - ctx.drawImage(Image, x, y, width, height); - // 恢复到上面save()时的状态 - ctx.restore(); - } else { - ctx.drawImage(Image, x, y, width, height); + try { + const Image = await loadImage(sourceItem.url); + if (callback) { + callback( + ctx, + { + width: canvas.width, + height: canvas.height + }, + { + x, + y, + width, + height, + borderColor, + lineWidth, + url: sourceItem.url, + image: Image + } + ); + } else if (name === ImageName.Avatar) { + // 在 clip() 之前保存canvas状态 + ctx.save(); + ctx.strokeStyle = borderColor; + ctx.lineWidth = lineWidth * scale; + ctx.beginPath(); + ctx.arc( + x + width / 2, + y + width / 2, + width / 2, + 0, + 2 * Math.PI, + false + ); + ctx.stroke(); + ctx.clip(); + ctx.drawImage(Image, x, y, width, height); + // 恢复到上面save()时的状态 + ctx.restore(); + } else { + ctx.drawImage(Image, x, y, width, height); + } + } catch (e) { + console.error(e, `图片地址:${ sourceItem.url }`); } } else if (sourceItem.type === SourceItemType.Text) { const {