-
-
Notifications
You must be signed in to change notification settings - Fork 272
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
Showing
5 changed files
with
93 additions
and
154 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 was deleted.
Oops, something went wrong.
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,9 @@ | ||
export function dataUrlToBlob(dataurl: string) { | ||
const parts = dataurl.split(',') | ||
const type = parts[0].split(':')[1].split(';')[0] | ||
const base64 = atob(parts[1]) | ||
const arr = new Uint8Array(base64.length) | ||
for (let i = 0; i < base64.length; i++) | ||
arr[i] = base64.charCodeAt(i) | ||
return new Blob([arr], { type }) | ||
} |
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,53 @@ | ||
import { dataUrlToBlob } from './dataUrlToBlob' | ||
Check failure on line 1 in src/utils/svgToPng.ts GitHub Actions / typecheck
|
||
|
||
export async function svgToPngDataUrl(svg: string) { | ||
const scaleFactor = 16 | ||
|
||
const canvas = document.createElement('canvas') | ||
const imgPreview = document.createElement('img') | ||
imgPreview.setAttribute('style', 'position: absolute; top: -9999px') | ||
document.body.appendChild(imgPreview) | ||
const canvasCtx = canvas.getContext('2d')! | ||
|
||
const svgBlob: Blob = new Blob([svg], { type: 'image/svg+xml;charset=utf-8' }) | ||
const svgDataUrl = URL.createObjectURL(svgBlob) | ||
|
||
return new Promise<string>((resolve) => { | ||
imgPreview.onload = async () => { | ||
const img = new Image() | ||
const dimensions: { width: number, height: number } = await getDimensions(imgPreview.src) | ||
|
||
Object.assign(canvas, { | ||
width: dimensions.width * scaleFactor, | ||
height: dimensions.height * scaleFactor, | ||
}) | ||
|
||
img.crossOrigin = 'anonymous' | ||
img.src = imgPreview.src | ||
img.onload = () => { | ||
canvasCtx.drawImage(img, 0, 0, canvas.width, canvas.height) | ||
const imgData = canvas.toDataURL('image/png') | ||
resolve(imgData) | ||
} | ||
|
||
function getDimensions( | ||
src: string, | ||
): Promise<{ width: number, height: number }> { | ||
return new Promise((resolve) => { | ||
const _img = new Image() | ||
_img.src = src | ||
_img.onload = () => { | ||
resolve({ | ||
width: _img.naturalWidth, | ||
height: _img.naturalHeight, | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
imgPreview.src = svgDataUrl | ||
}) | ||
.finally(() => { | ||
document.body.removeChild(imgPreview) | ||
}) | ||
} |