-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4051a1d
commit 3e4f623
Showing
5 changed files
with
73 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Crop } from "@/types"; | ||
import { unref, type MaybeRef } from "vue"; | ||
import { toImgSrc } from "./toImgSrc"; | ||
|
||
// Inspired by https://jsfiddle.net/Wijmo5/h2L3gw88/ | ||
export function toCanvas(svg: MaybeRef<SVGGraphicsElement>, canvas: MaybeRef<HTMLCanvasElement>, crop?: Crop) { | ||
const image64 = toImgSrc(svg) | ||
|
||
// set it as the source of the img element | ||
const img = new Image() | ||
img.onload = () => { | ||
// draw the image onto the canvas | ||
const unreffedCanvas = unref(canvas) | ||
unreffedCanvas.height = crop?.height ?? Number(unref(svg).getAttribute('height')) | ||
unreffedCanvas.width = crop?.width ?? Number(unref(svg).getAttribute('width')) | ||
unreffedCanvas.getContext('2d')?.drawImage(img, | ||
crop?.x ?? 0, crop?.y ?? 0, unreffedCanvas.width, unreffedCanvas.height, | ||
0, 0, unreffedCanvas.width, unreffedCanvas.height | ||
); | ||
} | ||
img.src = image64; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { unref, type MaybeRef } from "vue"; | ||
|
||
// Inspired by https://jsfiddle.net/Wijmo5/h2L3gw88/ | ||
export function toImgSrc(svg: MaybeRef<SVGElement>) { | ||
// get svg data | ||
const xml = new XMLSerializer().serializeToString(unref(svg)); | ||
|
||
// make it base64 | ||
const svg64 = btoa(xml); | ||
const b64Start = 'data:image/svg+xml;base64,'; | ||
|
||
// prepend a "header" | ||
return b64Start + svg64; | ||
} |