Skip to content

Commit

Permalink
feat: 添加canvas转图片方法
Browse files Browse the repository at this point in the history
  • Loading branch information
l-x-f committed Dec 10, 2021
1 parent 00bee13 commit f7aed9f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/auto-drawing.esm.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions dist/auto-drawing.js
Original file line number Diff line number Diff line change
Expand Up @@ -19183,6 +19183,30 @@
});
return res;
}
/**
* canvas转成图片
*/

var canvasToImage = function canvasToImage(zr) {
return new Promise(function (resolve, reject) {
try {
if (zr) {
var canvas = zr.painter.getRenderedCanvas();
var base64 = canvas.toDataURL('image/jpeg', 1.0);
canvas.toBlob(function (blob) {
resolve({
blob: blob,
base64: base64
});
});
} else {
reject();
}
} catch (error) {
reject();
}
});
};

/**
* 注册画布,解决打包后出现 `Renderer 'undefined' is not imported. Please import it first` 的问题
Expand Down Expand Up @@ -19421,6 +19445,7 @@
zr.add(group);
}

exports.canvasToImage = canvasToImage;
exports.copyArrayByCount = copyArrayByCount;
exports.createArc = createArc;
exports.createBezierCurve = createBezierCurve;
Expand Down
2 changes: 1 addition & 1 deletion dist/auto-drawing.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dist/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ export declare const getMiddle: ([x1, y1]: [number, number], [x2, y2]: [number,
* @returns
*/
export declare function copyArrayByCount<T>(arr: T[], count?: number): T[];
/**
* canvas转成图片
*/
export declare const canvasToImage: (zr: ZRenderType) => Promise<{
blob: Blob;
base64: string;
}>;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-drawing",
"version": "0.0.4",
"version": "0.0.5",
"description": "auto-drawing based zrender",
"types": "dist/index.d.ts",
"module": "dist/auto-drawing.esm.js",
Expand Down
21 changes: 21 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,24 @@ export function copyArrayByCount<T>(arr: T[], count = 1): T[] {
})
return res
}

/**
* canvas转成图片
*/
export const canvasToImage = (zr: ZRenderType): Promise<{ blob: Blob; base64: string }> => {
return new Promise((resolve, reject) => {
try {
if (zr) {
const canvas = (zr.painter as any).getRenderedCanvas()
const base64 = canvas.toDataURL('image/jpeg', 1.0)
canvas.toBlob((blob: Blob) => {
resolve({ blob, base64 })
})
} else {
reject()
}
} catch (error) {
reject()
}
})
}

0 comments on commit f7aed9f

Please sign in to comment.