-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/rzx007/nav
- Loading branch information
Showing
11 changed files
with
538 additions
and
238 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function svgToPng(svg: string, callback?: (imgData: string) => void) { | ||
const url = getSvgUrl(svg) | ||
svgUrlToPng(url, (imgData: string) => { | ||
callback && callback(imgData) | ||
URL.revokeObjectURL(url) | ||
}) | ||
} | ||
|
||
function getSvgUrl(svg: BlobPart) { | ||
return URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' })) | ||
} | ||
|
||
function svgUrlToPng(svgUrl: string, callback: (imgData: string) => void) { | ||
const svgImage = document.createElement('img') | ||
document.body.appendChild(svgImage) | ||
svgImage.onload = function () { | ||
const canvas = document.createElement('canvas') | ||
canvas.width = svgImage.clientWidth | ||
canvas.height = svgImage.clientHeight | ||
const canvasCtx = canvas.getContext('2d') | ||
canvasCtx!.drawImage(svgImage, 0, 0) | ||
const imgData = canvas.toDataURL('image/png') | ||
callback(imgData) | ||
} | ||
svgImage.src = svgUrl | ||
} |
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,13 @@ | ||
# svg 转 png | ||
|
||
主要思路: | ||
|
||
- 将 svg 转为`Blob`, | ||
- 然后使用`URL.createObjectURL`创建一个临时的 URL, | ||
- 然后使用`<img>`标签加载这个 URL, | ||
- 最后使用`<canvas>`标签将图片绘制到画布上, | ||
- 最后使用`canvas.toDataURL`将画布上的内容转为 base64 格式的图片。 | ||
|
||
<<< @/code/demo/SvgToPNG.ts{ts} | ||
<!-- 添加一张图片 --> | ||
<img src="/huggingface_logo-noborder.svg" alt="svg 转 png" height="200px" /> |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.