fix padding and refactor
This commit is contained in:
parent
7762b925ae
commit
cd8bfb06b5
@ -309,6 +309,7 @@ export const MAX_DECIMALS_FOR_SVG_EXPORT = 2;
|
|||||||
|
|
||||||
export const EXPORT_SCALES = [1, 2, 3];
|
export const EXPORT_SCALES = [1, 2, 3];
|
||||||
export const DEFAULT_EXPORT_PADDING = 10; // px
|
export const DEFAULT_EXPORT_PADDING = 10; // px
|
||||||
|
export const DEFAULT_SMALLEST_EXPORT_SIZE = 20; // px
|
||||||
|
|
||||||
export const DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT = 1440;
|
export const DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT = 1440;
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ import { arrayToMap, distance, getFontString, toBrandedType } from "../utils";
|
|||||||
import type { AppState, BinaryFiles } from "../types";
|
import type { AppState, BinaryFiles } from "../types";
|
||||||
import {
|
import {
|
||||||
COLOR_WHITE,
|
COLOR_WHITE,
|
||||||
DEFAULT_EXPORT_PADDING,
|
|
||||||
DEFAULT_ZOOM_VALUE,
|
DEFAULT_ZOOM_VALUE,
|
||||||
FRAME_STYLE,
|
FRAME_STYLE,
|
||||||
FONT_FAMILY,
|
FONT_FAMILY,
|
||||||
@ -22,6 +21,7 @@ import {
|
|||||||
THEME,
|
THEME,
|
||||||
THEME_FILTER,
|
THEME_FILTER,
|
||||||
MIME_TYPES,
|
MIME_TYPES,
|
||||||
|
DEFAULT_SMALLEST_EXPORT_SIZE,
|
||||||
} from "../constants";
|
} from "../constants";
|
||||||
import { getDefaultAppState } from "../appState";
|
import { getDefaultAppState } from "../appState";
|
||||||
import { serializeAsJSON } from "../data/json";
|
import { serializeAsJSON } from "../data/json";
|
||||||
@ -335,11 +335,7 @@ export type ExportToCanvasConfig = {
|
|||||||
loadFonts?: () => Promise<void>;
|
loadFonts?: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
const configExportDimension = async ({
|
||||||
* This API is usually used as a precursor to searializing to Blob or PNG,
|
|
||||||
* but can also be used to create a canvas for other purposes.
|
|
||||||
*/
|
|
||||||
export const exportToCanvas = async ({
|
|
||||||
data,
|
data,
|
||||||
config,
|
config,
|
||||||
}: {
|
}: {
|
||||||
@ -349,7 +345,6 @@ export const exportToCanvas = async ({
|
|||||||
// clone
|
// clone
|
||||||
const cfg = Object.assign({}, config);
|
const cfg = Object.assign({}, config);
|
||||||
|
|
||||||
const { files } = data;
|
|
||||||
const { exportingFrame } = cfg;
|
const { exportingFrame } = cfg;
|
||||||
|
|
||||||
const elements = data.elements;
|
const elements = data.elements;
|
||||||
@ -432,7 +427,7 @@ export const exportToCanvas = async ({
|
|||||||
// make the canvas fit into the frame (e.g. for `cfg.fit` set to `contain`).
|
// make the canvas fit into the frame (e.g. for `cfg.fit` set to `contain`).
|
||||||
// If `cfg.scale` is set, we multiply the resulting canvasScale by it to
|
// If `cfg.scale` is set, we multiply the resulting canvasScale by it to
|
||||||
// scale the output further.
|
// scale the output further.
|
||||||
let canvasScale = 1;
|
let exportScale = 1;
|
||||||
|
|
||||||
const origCanvasSize = getCanvasSize(
|
const origCanvasSize = getCanvasSize(
|
||||||
exportingFrame ? [exportingFrame] : getRootElements(elementsForRender),
|
exportingFrame ? [exportingFrame] : getRootElements(elementsForRender),
|
||||||
@ -446,6 +441,45 @@ export const exportToCanvas = async ({
|
|||||||
// variables for target bounding box
|
// variables for target bounding box
|
||||||
let [x, y, width, height] = origCanvasSize;
|
let [x, y, width, height] = origCanvasSize;
|
||||||
|
|
||||||
|
if (cfg.fit === "contain") {
|
||||||
|
if (cfg.width != null) {
|
||||||
|
cfg.padding = Math.min(
|
||||||
|
cfg.padding,
|
||||||
|
(cfg.width - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cfg.height != null) {
|
||||||
|
cfg.padding = Math.min(
|
||||||
|
cfg.padding,
|
||||||
|
(cfg.height - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cfg.getDimensions != null) {
|
||||||
|
const ret = cfg.getDimensions(width, height);
|
||||||
|
|
||||||
|
width = ret.width;
|
||||||
|
height = ret.height;
|
||||||
|
|
||||||
|
cfg.padding = Math.min(
|
||||||
|
cfg.padding,
|
||||||
|
(width - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
(height - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
);
|
||||||
|
} else if (cfg.widthOrHeight != null) {
|
||||||
|
cfg.padding = Math.min(
|
||||||
|
cfg.padding,
|
||||||
|
(cfg.widthOrHeight - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
);
|
||||||
|
} else if (cfg.maxWidthOrHeight != null) {
|
||||||
|
cfg.padding = Math.min(
|
||||||
|
cfg.padding,
|
||||||
|
(cfg.maxWidthOrHeight - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cfg.width != null) {
|
if (cfg.width != null) {
|
||||||
width = cfg.width;
|
width = cfg.width;
|
||||||
|
|
||||||
@ -475,7 +509,7 @@ export const exportToCanvas = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cfg.maxWidthOrHeight != null || cfg.widthOrHeight != null) {
|
if (cfg.maxWidthOrHeight != null || cfg.widthOrHeight != null) {
|
||||||
if (containPadding && cfg.padding) {
|
if (cfg.padding) {
|
||||||
if (cfg.maxWidthOrHeight != null) {
|
if (cfg.maxWidthOrHeight != null) {
|
||||||
cfg.maxWidthOrHeight -= cfg.padding * 2;
|
cfg.maxWidthOrHeight -= cfg.padding * 2;
|
||||||
} else if (cfg.widthOrHeight != null) {
|
} else if (cfg.widthOrHeight != null) {
|
||||||
@ -487,13 +521,13 @@ export const exportToCanvas = async ({
|
|||||||
if (cfg.widthOrHeight != null) {
|
if (cfg.widthOrHeight != null) {
|
||||||
// calculate by how much do we need to scale the canvas to fit into the
|
// calculate by how much do we need to scale the canvas to fit into the
|
||||||
// target dimension (e.g. target: max 50px, actual: 70x100px => scale: 0.5)
|
// target dimension (e.g. target: max 50px, actual: 70x100px => scale: 0.5)
|
||||||
canvasScale = cfg.widthOrHeight / max;
|
exportScale = cfg.widthOrHeight / max;
|
||||||
} else if (cfg.maxWidthOrHeight != null) {
|
} else if (cfg.maxWidthOrHeight != null) {
|
||||||
canvasScale = cfg.maxWidthOrHeight < max ? cfg.maxWidthOrHeight / max : 1;
|
exportScale = cfg.maxWidthOrHeight < max ? cfg.maxWidthOrHeight / max : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
width *= canvasScale;
|
width *= exportScale;
|
||||||
height *= canvasScale;
|
height *= exportScale;
|
||||||
} else if (cfg.getDimensions) {
|
} else if (cfg.getDimensions) {
|
||||||
const ret = cfg.getDimensions(width, height);
|
const ret = cfg.getDimensions(width, height);
|
||||||
|
|
||||||
@ -519,11 +553,11 @@ export const exportToCanvas = async ({
|
|||||||
const wRatio = width / origWidth;
|
const wRatio = width / origWidth;
|
||||||
const hRatio = height / origHeight;
|
const hRatio = height / origHeight;
|
||||||
// scale the orig canvas to fit in the target frame
|
// scale the orig canvas to fit in the target frame
|
||||||
canvasScale = Math.min(wRatio, hRatio);
|
exportScale = Math.min(wRatio, hRatio);
|
||||||
} else {
|
} else {
|
||||||
const wRatio = (width - cfg.padding * 2) / width;
|
const wRatio = (width - cfg.padding * 2) / width;
|
||||||
const hRatio = (height - cfg.padding * 2) / height;
|
const hRatio = (height - cfg.padding * 2) / height;
|
||||||
canvasScale = Math.min(wRatio, hRatio);
|
exportScale = Math.min(wRatio, hRatio);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -546,38 +580,83 @@ export const exportToCanvas = async ({
|
|||||||
// aspect ratio dimensions.
|
// aspect ratio dimensions.
|
||||||
if (cfg.position === "center") {
|
if (cfg.position === "center") {
|
||||||
x -=
|
x -=
|
||||||
width / canvasScale / 2 -
|
width / exportScale / 2 -
|
||||||
(cfg.x == null ? origWidth : width + cfg.padding * 2) / 2;
|
(cfg.x == null ? origWidth : width + cfg.padding * 2) / 2;
|
||||||
y -=
|
y -=
|
||||||
height / canvasScale / 2 -
|
height / exportScale / 2 -
|
||||||
(cfg.y == null ? origHeight : height + cfg.padding * 2) / 2;
|
(cfg.y == null ? origHeight : height + cfg.padding * 2) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rescale padding based on current canvasScale factor so that the resulting
|
||||||
|
// padding is kept the same as supplied by user (with the exception of
|
||||||
|
// `cfg.scale` being set, which also scales the padding)
|
||||||
|
const normalizedPadding = cfg.padding / exportScale;
|
||||||
|
|
||||||
|
// scale the whole frame by cfg.scale (on top of whatever canvasScale we
|
||||||
|
// calculated above)
|
||||||
|
exportScale *= cfg.scale;
|
||||||
|
|
||||||
|
width *= cfg.scale;
|
||||||
|
height *= cfg.scale;
|
||||||
|
|
||||||
|
const exportWidth = width + cfg.padding * 2 * cfg.scale;
|
||||||
|
const exportHeight = height + cfg.padding * 2 * cfg.scale;
|
||||||
|
|
||||||
|
return {
|
||||||
|
config: cfg,
|
||||||
|
normalizedPadding,
|
||||||
|
contentWidth: width,
|
||||||
|
contentHeight: height,
|
||||||
|
exportWidth,
|
||||||
|
exportHeight,
|
||||||
|
exportScale,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
elementsForRender,
|
||||||
|
appState,
|
||||||
|
frameRendering,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This API is usually used as a precursor to searializing to Blob or PNG,
|
||||||
|
* but can also be used to create a canvas for other purposes.
|
||||||
|
*/
|
||||||
|
export const exportToCanvas = async ({
|
||||||
|
data,
|
||||||
|
config,
|
||||||
|
}: {
|
||||||
|
data: ExportToCanvasData;
|
||||||
|
config?: ExportToCanvasConfig;
|
||||||
|
}) => {
|
||||||
|
const {
|
||||||
|
config: cfg,
|
||||||
|
normalizedPadding,
|
||||||
|
contentWidth: width,
|
||||||
|
contentHeight: height,
|
||||||
|
exportWidth,
|
||||||
|
exportHeight,
|
||||||
|
exportScale,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
elementsForRender,
|
||||||
|
appState,
|
||||||
|
frameRendering,
|
||||||
|
} = await configExportDimension({ data, config });
|
||||||
|
|
||||||
const canvas = cfg.createCanvas
|
const canvas = cfg.createCanvas
|
||||||
? cfg.createCanvas()
|
? cfg.createCanvas()
|
||||||
: document.createElement("canvas");
|
: document.createElement("canvas");
|
||||||
|
|
||||||
// rescale padding based on current canvasScale factor so that the resulting
|
canvas.width = exportWidth;
|
||||||
// padding is kept the same as supplied by user (with the exception of
|
canvas.height = exportHeight;
|
||||||
// `cfg.scale` being set, which also scales the padding)
|
|
||||||
const normalizedPadding = cfg.padding / canvasScale;
|
|
||||||
|
|
||||||
// scale the whole frame by cfg.scale (on top of whatever canvasScale we
|
|
||||||
// calculated above)
|
|
||||||
canvasScale *= cfg.scale;
|
|
||||||
|
|
||||||
width *= cfg.scale;
|
|
||||||
height *= cfg.scale;
|
|
||||||
|
|
||||||
canvas.width = width + cfg.padding * 2 * cfg.scale;
|
|
||||||
canvas.height = height + cfg.padding * 2 * cfg.scale;
|
|
||||||
|
|
||||||
const { imageCache } = await updateImageCache({
|
const { imageCache } = await updateImageCache({
|
||||||
imageCache: new Map(),
|
imageCache: new Map(),
|
||||||
fileIds: getInitializedImageElements(elementsForRender).map(
|
fileIds: getInitializedImageElements(elementsForRender).map(
|
||||||
(element) => element.fileId,
|
(element) => element.fileId,
|
||||||
),
|
),
|
||||||
files: files || {},
|
files: data.files || {},
|
||||||
});
|
});
|
||||||
|
|
||||||
renderStaticScene({
|
renderStaticScene({
|
||||||
@ -587,7 +666,7 @@ export const exportToCanvas = async ({
|
|||||||
arrayToMap(elementsForRender),
|
arrayToMap(elementsForRender),
|
||||||
),
|
),
|
||||||
allElementsMap: toBrandedType<NonDeletedSceneElementsMap>(
|
allElementsMap: toBrandedType<NonDeletedSceneElementsMap>(
|
||||||
arrayToMap(syncInvalidIndices(elements)),
|
arrayToMap(syncInvalidIndices(data.elements)),
|
||||||
),
|
),
|
||||||
visibleElements: elementsForRender,
|
visibleElements: elementsForRender,
|
||||||
appState: {
|
appState: {
|
||||||
@ -604,7 +683,7 @@ export const exportToCanvas = async ({
|
|||||||
shouldCacheIgnoreZoom: false,
|
shouldCacheIgnoreZoom: false,
|
||||||
theme: cfg.theme || THEME.LIGHT,
|
theme: cfg.theme || THEME.LIGHT,
|
||||||
},
|
},
|
||||||
scale: canvasScale,
|
scale: exportScale,
|
||||||
renderConfig: {
|
renderConfig: {
|
||||||
canvasBackgroundColor:
|
canvasBackgroundColor:
|
||||||
cfg.canvasBackgroundColor === false
|
cfg.canvasBackgroundColor === false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user