Skip to content

Commit

Permalink
feat: 优化图片跨域
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasuo committed Sep 3, 2021
1 parent e754efe commit 8d0dccd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ json2canvas({
> 2021年08月18日
- 优化图片跨域

> 2021年09月03日
- 图片加载失败之后,继续绘制,跳过当前失败
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
85 changes: 44 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d0dccd

Please sign in to comment.