-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dl-asset): add downloadCanvas()
- Loading branch information
1 parent
e3c490c
commit ca657d4
Showing
3 changed files
with
32 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { downloadWithMime } from "./raw.js"; | ||
|
||
/** | ||
* Triggers canvas-to-blob conversion for given file type (and opt. `quality`), | ||
* then triggers download via {@link downloadWithMime}. Default file type is | ||
* `png`. Default quality is 0.95 (only used for JPEG/WebP). | ||
* | ||
* @param canvas | ||
* @param baseName | ||
* @param type | ||
* @param quality | ||
*/ | ||
export const downloadCanvas = ( | ||
canvas: HTMLCanvasElement, | ||
baseName: string, | ||
type: "png" | "jpeg" | "webp" = "png", | ||
quality = 0.95 | ||
) => { | ||
const mime = `image/${type}`; | ||
canvas.toBlob( | ||
(blob) => | ||
blob | ||
? downloadWithMime(`${baseName}.${type}`, blob, { mime }) | ||
: console.warn("can't download canvas"), | ||
mime, | ||
quality | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./api.js"; | ||
export * from "./canvas.js"; | ||
export * from "./download.js"; | ||
export * from "./raw.js"; |