feat: set appState.exportBackground
to true
when exporting to jpg
(#4342)
This commit is contained in:
parent
1ee8d7d082
commit
afa7932c9b
@ -13,6 +13,10 @@ Please add the latest change on the top under the correct section.
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
## Excalidraw API
|
||||||
|
|
||||||
|
- [`exportToBlob`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#exportToBlob) now automatically sets `appState.exportBackground` to `true` if exporting to `image/jpeg` MIME type (to ensure that alpha channel is not compressed to black color).
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- #### BREAKING CHANGE
|
- #### BREAKING CHANGE
|
||||||
|
@ -828,6 +828,8 @@ import { exportToBlob } from "@excalidraw/excalidraw";
|
|||||||
|
|
||||||
Returns a promise which resolves with a [blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob). It internally uses [canvas.ToBlob](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob).
|
Returns a promise which resolves with a [blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob). It internally uses [canvas.ToBlob](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob).
|
||||||
|
|
||||||
|
Note: `appState.exportBackground` is always set to `true` if exporting to `image/jpeg` to ensure the alpha channel isn't compressed to black.
|
||||||
|
|
||||||
#### `exportToSvg`
|
#### `exportToSvg`
|
||||||
|
|
||||||
**_Signature_**
|
**_Signature_**
|
||||||
|
@ -80,8 +80,6 @@ export const exportToBlob = async (
|
|||||||
quality?: number;
|
quality?: number;
|
||||||
},
|
},
|
||||||
): Promise<Blob | null> => {
|
): Promise<Blob | null> => {
|
||||||
const canvas = await exportToCanvas(opts);
|
|
||||||
|
|
||||||
let { mimeType = MIME_TYPES.png, quality } = opts;
|
let { mimeType = MIME_TYPES.png, quality } = opts;
|
||||||
|
|
||||||
if (mimeType === MIME_TYPES.png && typeof quality === "number") {
|
if (mimeType === MIME_TYPES.png && typeof quality === "number") {
|
||||||
@ -93,6 +91,18 @@ export const exportToBlob = async (
|
|||||||
mimeType = MIME_TYPES.jpg;
|
mimeType = MIME_TYPES.jpg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mimeType === MIME_TYPES.jpg && !opts.appState?.exportBackground) {
|
||||||
|
console.warn(
|
||||||
|
`Defaulting "exportBackground" to "true" for "${MIME_TYPES.jpg}" mimeType`,
|
||||||
|
);
|
||||||
|
opts = {
|
||||||
|
...opts,
|
||||||
|
appState: { ...opts.appState, exportBackground: true },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const canvas = await exportToCanvas(opts);
|
||||||
|
|
||||||
quality = quality ? quality : /image\/jpe?g/.test(mimeType) ? 0.92 : 0.8;
|
quality = quality ? quality : /image\/jpe?g/.test(mimeType) ? 0.92 : 0.8;
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -42,6 +42,9 @@ describe("exportToBlob", () => {
|
|||||||
getDimensions: (width, height) => ({ width, height, scale: 1 }),
|
getDimensions: (width, height) => ({ width, height, scale: 1 }),
|
||||||
// testing typo in MIME type (jpg → jpeg)
|
// testing typo in MIME type (jpg → jpeg)
|
||||||
mimeType: "image/jpg",
|
mimeType: "image/jpg",
|
||||||
|
appState: {
|
||||||
|
exportBackground: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
expect(blob?.type).toBe(MIME_TYPES.jpg);
|
expect(blob?.type).toBe(MIME_TYPES.jpg);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user