Add optional watermark on export (#1365)
* Add optional watermark on export * Address init PR feedback * Add SVG export with refactoring * Update export.ts * Move addWatermark to appState * Update snapshots * Fit watermark in small scene * Rename watermark things Co-authored-by: Lipis <lipiridis@gmail.com>
This commit is contained in:
parent
13cea081f3
commit
5822117e23
@ -42,6 +42,26 @@ export const actionChangeExportBackground = register({
|
|||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const actionChangeShouldAddWatermark = register({
|
||||||
|
name: "changeShouldAddWatermark",
|
||||||
|
perform: (_elements, appState, value) => {
|
||||||
|
return {
|
||||||
|
appState: { ...appState, shouldAddWatermark: value },
|
||||||
|
commitToHistory: false,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
PanelComponent: ({ appState, updateData }) => (
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={appState.shouldAddWatermark}
|
||||||
|
onChange={(event) => updateData(event.target.checked)}
|
||||||
|
/>{" "}
|
||||||
|
{t("labels.addWatermark")}
|
||||||
|
</label>
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
export const actionSaveScene = register({
|
export const actionSaveScene = register({
|
||||||
name: "saveScene",
|
name: "saveScene",
|
||||||
perform: (elements, appState, value) => {
|
perform: (elements, appState, value) => {
|
||||||
|
@ -39,6 +39,7 @@ export type ActionName =
|
|||||||
| "finalize"
|
| "finalize"
|
||||||
| "changeProjectName"
|
| "changeProjectName"
|
||||||
| "changeExportBackground"
|
| "changeExportBackground"
|
||||||
|
| "changeShouldAddWatermark"
|
||||||
| "saveScene"
|
| "saveScene"
|
||||||
| "loadScene"
|
| "loadScene"
|
||||||
| "duplicateSelection"
|
| "duplicateSelection"
|
||||||
|
@ -17,6 +17,7 @@ export function getDefaultAppState(): AppState {
|
|||||||
elementType: "selection",
|
elementType: "selection",
|
||||||
elementLocked: false,
|
elementLocked: false,
|
||||||
exportBackground: true,
|
exportBackground: true,
|
||||||
|
shouldAddWatermark: false,
|
||||||
currentItemStrokeColor: oc.black,
|
currentItemStrokeColor: oc.black,
|
||||||
currentItemBackgroundColor: "transparent",
|
currentItemBackgroundColor: "transparent",
|
||||||
currentItemFillStyle: "hachure",
|
currentItemFillStyle: "hachure",
|
||||||
@ -73,6 +74,7 @@ export function clearAppStatePropertiesForHistory(
|
|||||||
return {
|
return {
|
||||||
selectedElementIds: appState.selectedElementIds,
|
selectedElementIds: appState.selectedElementIds,
|
||||||
exportBackground: appState.exportBackground,
|
exportBackground: appState.exportBackground,
|
||||||
|
shouldAddWatermark: appState.shouldAddWatermark,
|
||||||
currentItemStrokeColor: appState.currentItemStrokeColor,
|
currentItemStrokeColor: appState.currentItemStrokeColor,
|
||||||
currentItemBackgroundColor: appState.currentItemBackgroundColor,
|
currentItemBackgroundColor: appState.currentItemBackgroundColor,
|
||||||
currentItemFillStyle: appState.currentItemFillStyle,
|
currentItemFillStyle: appState.currentItemFillStyle,
|
||||||
|
@ -48,7 +48,11 @@ function ExportModal({
|
|||||||
const [scale, setScale] = useState(defaultScale);
|
const [scale, setScale] = useState(defaultScale);
|
||||||
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
|
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
|
||||||
const previewRef = useRef<HTMLDivElement>(null);
|
const previewRef = useRef<HTMLDivElement>(null);
|
||||||
const { exportBackground, viewBackgroundColor } = appState;
|
const {
|
||||||
|
exportBackground,
|
||||||
|
viewBackgroundColor,
|
||||||
|
shouldAddWatermark,
|
||||||
|
} = appState;
|
||||||
|
|
||||||
const exportedElements = exportSelected
|
const exportedElements = exportSelected
|
||||||
? getSelectedElements(elements, appState)
|
? getSelectedElements(elements, appState)
|
||||||
@ -65,6 +69,7 @@ function ExportModal({
|
|||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
exportPadding,
|
exportPadding,
|
||||||
scale,
|
scale,
|
||||||
|
shouldAddWatermark,
|
||||||
});
|
});
|
||||||
previewNode?.appendChild(canvas);
|
previewNode?.appendChild(canvas);
|
||||||
return () => {
|
return () => {
|
||||||
@ -77,6 +82,7 @@ function ExportModal({
|
|||||||
exportPadding,
|
exportPadding,
|
||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
scale,
|
scale,
|
||||||
|
shouldAddWatermark,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -150,6 +156,7 @@ function ExportModal({
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{actionManager.renderAction("changeShouldAddWatermark")}
|
||||||
</Stack.Col>
|
</Stack.Col>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -79,6 +79,7 @@ const LayerUI = ({
|
|||||||
name: appState.name,
|
name: appState.name,
|
||||||
viewBackgroundColor: appState.viewBackgroundColor,
|
viewBackgroundColor: appState.viewBackgroundColor,
|
||||||
scale,
|
scale,
|
||||||
|
shouldAddWatermark: appState.shouldAddWatermark,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -294,12 +294,14 @@ export async function exportCanvas(
|
|||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
name,
|
name,
|
||||||
scale = 1,
|
scale = 1,
|
||||||
|
shouldAddWatermark,
|
||||||
}: {
|
}: {
|
||||||
exportBackground: boolean;
|
exportBackground: boolean;
|
||||||
exportPadding?: number;
|
exportPadding?: number;
|
||||||
viewBackgroundColor: string;
|
viewBackgroundColor: string;
|
||||||
name: string;
|
name: string;
|
||||||
scale?: number;
|
scale?: number;
|
||||||
|
shouldAddWatermark: boolean;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
if (elements.length === 0) {
|
if (elements.length === 0) {
|
||||||
@ -310,6 +312,7 @@ export async function exportCanvas(
|
|||||||
exportBackground,
|
exportBackground,
|
||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
exportPadding,
|
exportPadding,
|
||||||
|
shouldAddWatermark,
|
||||||
});
|
});
|
||||||
if (type === "svg") {
|
if (type === "svg") {
|
||||||
await fileSave(new Blob([tempSvg.outerHTML], { type: "image/svg+xml" }), {
|
await fileSave(new Blob([tempSvg.outerHTML], { type: "image/svg+xml" }), {
|
||||||
@ -327,6 +330,7 @@ export async function exportCanvas(
|
|||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
exportPadding,
|
exportPadding,
|
||||||
scale,
|
scale,
|
||||||
|
shouldAddWatermark,
|
||||||
});
|
});
|
||||||
tempCanvas.style.display = "none";
|
tempCanvas.style.display = "none";
|
||||||
document.body.appendChild(tempCanvas);
|
document.body.appendChild(tempCanvas);
|
||||||
|
@ -63,6 +63,7 @@ const canvas = exportToCanvas(
|
|||||||
{
|
{
|
||||||
exportBackground: true,
|
exportBackground: true,
|
||||||
viewBackgroundColor: "#ffffff",
|
viewBackgroundColor: "#ffffff",
|
||||||
|
shouldAddWatermark: false,
|
||||||
scale: 1,
|
scale: 1,
|
||||||
},
|
},
|
||||||
createCanvas,
|
createCanvas,
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
"fontFamily": "Font family",
|
"fontFamily": "Font family",
|
||||||
"onlySelected": "Only selected",
|
"onlySelected": "Only selected",
|
||||||
"withBackground": "With Background",
|
"withBackground": "With Background",
|
||||||
|
"addWatermark": "Add \"Made with Excalidraw\"",
|
||||||
"handDrawn": "Hand-drawn",
|
"handDrawn": "Hand-drawn",
|
||||||
"normal": "Normal",
|
"normal": "Normal",
|
||||||
"code": "Code",
|
"code": "Code",
|
||||||
@ -52,7 +53,8 @@
|
|||||||
"createRoom": "Share a live-collaboration session",
|
"createRoom": "Share a live-collaboration session",
|
||||||
"duplicateSelection": "Duplicate",
|
"duplicateSelection": "Duplicate",
|
||||||
"untitled": "Untitled",
|
"untitled": "Untitled",
|
||||||
"name": "Name"
|
"name": "Name",
|
||||||
|
"madeWithExcalidraw": "Made with Excalidraw"
|
||||||
},
|
},
|
||||||
"buttons": {
|
"buttons": {
|
||||||
"clearReset": "Reset the canvas",
|
"clearReset": "Reset the canvas",
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import rough from "roughjs/bin/rough";
|
import rough from "roughjs/bin/rough";
|
||||||
|
import oc from "open-color";
|
||||||
|
import { newTextElement } from "../element";
|
||||||
import { NonDeletedExcalidrawElement } from "../element/types";
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
||||||
import { getCommonBounds } from "../element/bounds";
|
import { getCommonBounds } from "../element/bounds";
|
||||||
import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
|
import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
|
||||||
import { distance, SVG_NS } from "../utils";
|
import { distance, SVG_NS, measureText } from "../utils";
|
||||||
import { normalizeScroll } from "./scroll";
|
import { normalizeScroll } from "./scroll";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
|
import { t } from "../i18n";
|
||||||
|
|
||||||
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
||||||
|
|
||||||
@ -16,11 +19,13 @@ export function exportToCanvas(
|
|||||||
exportPadding = 10,
|
exportPadding = 10,
|
||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
scale = 1,
|
scale = 1,
|
||||||
|
shouldAddWatermark,
|
||||||
}: {
|
}: {
|
||||||
exportBackground: boolean;
|
exportBackground: boolean;
|
||||||
exportPadding?: number;
|
exportPadding?: number;
|
||||||
scale?: number;
|
scale?: number;
|
||||||
viewBackgroundColor: string;
|
viewBackgroundColor: string;
|
||||||
|
shouldAddWatermark: boolean;
|
||||||
},
|
},
|
||||||
createCanvas: (width: number, height: number) => any = function (
|
createCanvas: (width: number, height: number) => any = function (
|
||||||
width,
|
width,
|
||||||
@ -32,15 +37,24 @@ export function exportToCanvas(
|
|||||||
return tempCanvas;
|
return tempCanvas;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
let sceneElements = elements;
|
||||||
|
if (shouldAddWatermark) {
|
||||||
|
const [, , maxX, maxY] = getCommonBounds(elements);
|
||||||
|
sceneElements = [...sceneElements, getWatermarkElement(maxX, maxY)];
|
||||||
|
}
|
||||||
|
|
||||||
// calculate smallest area to fit the contents in
|
// calculate smallest area to fit the contents in
|
||||||
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
const [minX, minY, maxX, maxY] = getCommonBounds(sceneElements);
|
||||||
const width = distance(minX, maxX) + exportPadding * 2;
|
const width = distance(minX, maxX) + exportPadding * 2;
|
||||||
const height = distance(minY, maxY) + exportPadding * 2;
|
const height =
|
||||||
|
distance(minY, maxY) +
|
||||||
|
exportPadding +
|
||||||
|
(shouldAddWatermark ? 0 : exportPadding);
|
||||||
|
|
||||||
const tempCanvas: any = createCanvas(width, height);
|
const tempCanvas: any = createCanvas(width, height);
|
||||||
|
|
||||||
renderScene(
|
renderScene(
|
||||||
elements,
|
sceneElements,
|
||||||
appState,
|
appState,
|
||||||
null,
|
null,
|
||||||
scale,
|
scale,
|
||||||
@ -62,6 +76,7 @@ export function exportToCanvas(
|
|||||||
renderOptimizations: false,
|
renderOptimizations: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return tempCanvas;
|
return tempCanvas;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,16 +86,27 @@ export function exportToSvg(
|
|||||||
exportBackground,
|
exportBackground,
|
||||||
exportPadding = 10,
|
exportPadding = 10,
|
||||||
viewBackgroundColor,
|
viewBackgroundColor,
|
||||||
|
shouldAddWatermark,
|
||||||
}: {
|
}: {
|
||||||
exportBackground: boolean;
|
exportBackground: boolean;
|
||||||
exportPadding?: number;
|
exportPadding?: number;
|
||||||
viewBackgroundColor: string;
|
viewBackgroundColor: string;
|
||||||
|
shouldAddWatermark: boolean;
|
||||||
},
|
},
|
||||||
): SVGSVGElement {
|
): SVGSVGElement {
|
||||||
|
let sceneElements = elements;
|
||||||
|
if (shouldAddWatermark) {
|
||||||
|
const [, , maxX, maxY] = getCommonBounds(elements);
|
||||||
|
sceneElements = [...sceneElements, getWatermarkElement(maxX, maxY)];
|
||||||
|
}
|
||||||
|
|
||||||
// calculate canvas dimensions
|
// calculate canvas dimensions
|
||||||
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
const [minX, minY, maxX, maxY] = getCommonBounds(sceneElements);
|
||||||
const width = distance(minX, maxX) + exportPadding * 2;
|
const width = distance(minX, maxX) + exportPadding * 2;
|
||||||
const height = distance(minY, maxY) + exportPadding * 2;
|
const height =
|
||||||
|
distance(minY, maxY) +
|
||||||
|
exportPadding +
|
||||||
|
(shouldAddWatermark ? 0 : exportPadding);
|
||||||
|
|
||||||
// initialze SVG root
|
// initialze SVG root
|
||||||
const svgRoot = document.createElementNS(SVG_NS, "svg");
|
const svgRoot = document.createElementNS(SVG_NS, "svg");
|
||||||
@ -116,9 +142,30 @@ export function exportToSvg(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const rsvg = rough.svg(svgRoot);
|
const rsvg = rough.svg(svgRoot);
|
||||||
renderSceneToSvg(elements, rsvg, svgRoot, {
|
renderSceneToSvg(sceneElements, rsvg, svgRoot, {
|
||||||
offsetX: -minX + exportPadding,
|
offsetX: -minX + exportPadding,
|
||||||
offsetY: -minY + exportPadding,
|
offsetY: -minY + exportPadding,
|
||||||
});
|
});
|
||||||
|
|
||||||
return svgRoot;
|
return svgRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getWatermarkElement(maxX: number, maxY: number) {
|
||||||
|
const text = t("labels.madeWithExcalidraw");
|
||||||
|
const font = "16px Virgil";
|
||||||
|
const { width: textWidth } = measureText(text, font);
|
||||||
|
|
||||||
|
return newTextElement({
|
||||||
|
text,
|
||||||
|
font,
|
||||||
|
textAlign: "center",
|
||||||
|
x: maxX - textWidth / 2,
|
||||||
|
y: maxY + 16,
|
||||||
|
strokeColor: oc.gray[5],
|
||||||
|
backgroundColor: "transparent",
|
||||||
|
fillStyle: "hachure",
|
||||||
|
strokeWidth: 1,
|
||||||
|
roughness: 1,
|
||||||
|
opacity: 100,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -25,6 +25,7 @@ export type AppState = {
|
|||||||
elementType: typeof SHAPES[number]["value"];
|
elementType: typeof SHAPES[number]["value"];
|
||||||
elementLocked: boolean;
|
elementLocked: boolean;
|
||||||
exportBackground: boolean;
|
exportBackground: boolean;
|
||||||
|
shouldAddWatermark: boolean;
|
||||||
currentItemStrokeColor: string;
|
currentItemStrokeColor: string;
|
||||||
currentItemBackgroundColor: string;
|
currentItemBackgroundColor: string;
|
||||||
currentItemFillStyle: string;
|
currentItemFillStyle: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user