|
@@ -34,11 +34,16 @@ const blobToBase64 = (blob: any) => {
|
|
|
reader.readAsDataURL(blob)
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
+let canvas = null as any
|
|
|
export const svgtopng = async (svg: any, width: any, height: any) => {
|
|
|
- const canvas = new OffscreenCanvas(width, height)
|
|
|
+ console.log(canvas, +new Date() + '-----')
|
|
|
+
|
|
|
+ if (!canvas) {
|
|
|
+ canvas = new OffscreenCanvas(width, height)
|
|
|
+ }
|
|
|
+
|
|
|
const ctx = canvas.getContext('2d')!
|
|
|
- const v = await Canvg.fromString(ctx!, svg, preset)
|
|
|
+ let v: any = await Canvg.fromString(ctx!, svg, preset)
|
|
|
|
|
|
|
|
|
* Resize SVG to fit in given size.
|
|
@@ -53,17 +58,23 @@ export const svgtopng = async (svg: any, width: any, height: any) => {
|
|
|
const blob = await canvas.convertToBlob()
|
|
|
const base64 = await blobToBase64(blob)
|
|
|
|
|
|
- releaseCanvas(canvas)
|
|
|
+
|
|
|
+
|
|
|
+ ctx.clearRect(0, 0, canvas.width, canvas.height)
|
|
|
+
|
|
|
+ console.log(canvas, 'draw')
|
|
|
+ await v.stop()
|
|
|
+ v = null
|
|
|
return base64
|
|
|
}
|
|
|
|
|
|
-function releaseCanvas(canvasElement) {
|
|
|
-
|
|
|
- const ctx = canvasElement.getContext('2d')
|
|
|
- ctx.clearRect(0, 0, canvasElement.width, canvasElement.height)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- cancelAnimationFrame(canvasElement.animationId)
|
|
|
-
|
|
|
- canvasElement = null
|
|
|
-}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|