Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4330f3ec9d | ||
|
|
d5aad6202d | ||
|
|
db73e30eae | ||
|
|
f472af04a9 |
@@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import * as excalidrawLib from "@excalidraw/excalidraw";
|
import * as excalidrawLib from "@excalidraw/excalidraw";
|
||||||
import { Excalidraw } from "@excalidraw/excalidraw";
|
import { Excalidraw } from "@excalidraw/excalidraw";
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ const ExcalidrawWrapper: React.FC = () => {
|
|||||||
appTitle={"Excalidraw with Nextjs Example"}
|
appTitle={"Excalidraw with Nextjs Example"}
|
||||||
useCustom={(api: any, args?: any[]) => {}}
|
useCustom={(api: any, args?: any[]) => {}}
|
||||||
excalidrawLib={excalidrawLib}
|
excalidrawLib={excalidrawLib}
|
||||||
|
showFadeDemo={true}
|
||||||
>
|
>
|
||||||
<Excalidraw />
|
<Excalidraw />
|
||||||
</App>
|
</App>
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ export interface AppProps {
|
|||||||
customArgs?: any[];
|
customArgs?: any[];
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
excalidrawLib: typeof TExcalidraw;
|
excalidrawLib: typeof TExcalidraw;
|
||||||
|
showFadeDemo?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ExampleApp({
|
export default function ExampleApp({
|
||||||
@@ -78,6 +79,7 @@ export default function ExampleApp({
|
|||||||
customArgs,
|
customArgs,
|
||||||
children,
|
children,
|
||||||
excalidrawLib,
|
excalidrawLib,
|
||||||
|
showFadeDemo = false,
|
||||||
}: AppProps) {
|
}: AppProps) {
|
||||||
const {
|
const {
|
||||||
exportToCanvas,
|
exportToCanvas,
|
||||||
@@ -116,6 +118,19 @@ export default function ExampleApp({
|
|||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
const [comment, setComment] = useState<Comment | null>(null);
|
const [comment, setComment] = useState<Comment | null>(null);
|
||||||
|
const [hideAllForFadeDemo, setHideAllForFadeDemo] = useState(false);
|
||||||
|
const [fadeDemoNextIndex, setFadeDemoNextIndex] = useState(0);
|
||||||
|
const [fadeDemoElementIds, setFadeDemoElementIds] = useState<string[]>([]);
|
||||||
|
const [demoAnimationType, setDemoAnimationType] = useState<"fade" | "fly">(
|
||||||
|
"fade",
|
||||||
|
);
|
||||||
|
const [demoAnimationDuration, setDemoAnimationDuration] = useState(500);
|
||||||
|
const [demoFlyFrom, setDemoFlyFrom] = useState<
|
||||||
|
"left" | "right" | "top" | "bottom"
|
||||||
|
>("left");
|
||||||
|
const [demoAnimationEasing, setDemoAnimationEasing] = useState<
|
||||||
|
"linear" | "easeOut" | "easeInOut"
|
||||||
|
>("easeOut");
|
||||||
|
|
||||||
const initialStatePromiseRef = useRef<{
|
const initialStatePromiseRef = useRef<{
|
||||||
promise: ResolvablePromise<ExcalidrawInitialDataState | null>;
|
promise: ResolvablePromise<ExcalidrawInitialDataState | null>;
|
||||||
@@ -178,7 +193,8 @@ export default function ExampleApp({
|
|||||||
const newElement = cloneElement(
|
const newElement = cloneElement(
|
||||||
Excalidraw,
|
Excalidraw,
|
||||||
{
|
{
|
||||||
excalidrawAPI: (api: ExcalidrawImperativeAPI) => setExcalidrawAPI(api),
|
onExcalidrawAPI: (api: ExcalidrawImperativeAPI | null) =>
|
||||||
|
setExcalidrawAPI(api),
|
||||||
initialData: initialStatePromiseRef.current.promise,
|
initialData: initialStatePromiseRef.current.promise,
|
||||||
onChange: (
|
onChange: (
|
||||||
elements: NonDeletedExcalidrawElement[],
|
elements: NonDeletedExcalidrawElement[],
|
||||||
@@ -208,6 +224,10 @@ export default function ExampleApp({
|
|||||||
onPointerDown,
|
onPointerDown,
|
||||||
onScrollChange: rerenderCommentIcons,
|
onScrollChange: rerenderCommentIcons,
|
||||||
validateEmbeddable: true,
|
validateEmbeddable: true,
|
||||||
|
resolveRenderOpacity: hideAllForFadeDemo
|
||||||
|
? (element: NonDeletedExcalidrawElement) =>
|
||||||
|
fadeDemoElementIds.includes(element.id) ? 0 : undefined
|
||||||
|
: undefined,
|
||||||
},
|
},
|
||||||
<>
|
<>
|
||||||
{excalidrawAPI && (
|
{excalidrawAPI && (
|
||||||
@@ -664,6 +684,229 @@ export default function ExampleApp({
|
|||||||
>
|
>
|
||||||
Reset Scene
|
Reset Scene
|
||||||
</button>
|
</button>
|
||||||
|
{showFadeDemo && (
|
||||||
|
<>
|
||||||
|
<label>
|
||||||
|
Animation type
|
||||||
|
<select
|
||||||
|
value={demoAnimationType}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationType(
|
||||||
|
event.target.value as "fade" | "fly",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="fade">fade</option>
|
||||||
|
<option value="fly">fly</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Duration
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={demoAnimationDuration}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationDuration(Number(event.target.value) || 0)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Easing
|
||||||
|
<select
|
||||||
|
value={demoAnimationEasing}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoAnimationEasing(
|
||||||
|
event.target.value as "linear" | "easeOut" | "easeInOut",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="linear">linear</option>
|
||||||
|
<option value="easeOut">easeOut</option>
|
||||||
|
<option value="easeInOut">easeInOut</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
{demoAnimationType === "fly" && (
|
||||||
|
<label>
|
||||||
|
Fly from
|
||||||
|
<select
|
||||||
|
value={demoFlyFrom}
|
||||||
|
onChange={(event) =>
|
||||||
|
setDemoFlyFrom(
|
||||||
|
event.target.value as
|
||||||
|
| "left"
|
||||||
|
| "right"
|
||||||
|
| "top"
|
||||||
|
| "bottom",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="left">left</option>
|
||||||
|
<option value="right">right</option>
|
||||||
|
<option value="top">top</option>
|
||||||
|
<option value="bottom">bottom</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoElementIds(
|
||||||
|
excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.map((element) => element.id),
|
||||||
|
);
|
||||||
|
excalidrawAPI.clearElementAnimationOverrides();
|
||||||
|
setHideAllForFadeDemo(true);
|
||||||
|
setFadeDemoNextIndex(0);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Hide every element
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextIndex =
|
||||||
|
fadeDemoNextIndex >= elements.length
|
||||||
|
? 0
|
||||||
|
: fadeDemoNextIndex;
|
||||||
|
|
||||||
|
if (nextIndex === 0) {
|
||||||
|
excalidrawAPI.clearElementAnimationOverrides();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[nextIndex].id],
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[nextIndex].id],
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(nextIndex + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate in next element
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements,
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
stagger: 120,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements,
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
stagger: 120,
|
||||||
|
phase: "in",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(elements.length);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate in all
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!excalidrawAPI || !hideAllForFadeDemo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = excalidrawAPI
|
||||||
|
.getSceneElements()
|
||||||
|
.filter((element) =>
|
||||||
|
fadeDemoElementIds.includes(element.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!elements.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prevIndex = Math.min(
|
||||||
|
fadeDemoNextIndex - 1,
|
||||||
|
elements.length - 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (prevIndex < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (demoAnimationType === "fly") {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[prevIndex].id],
|
||||||
|
type: "fly",
|
||||||
|
from: demoFlyFrom,
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "out",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
excalidrawAPI.animateElements({
|
||||||
|
elements: [elements[prevIndex].id],
|
||||||
|
type: "fade",
|
||||||
|
duration: demoAnimationDuration,
|
||||||
|
phase: "out",
|
||||||
|
easing: demoAnimationEasing,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setFadeDemoNextIndex(prevIndex);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Animate out prev element
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const libraryItems: LibraryItems = [
|
const libraryItems: LibraryItems = [
|
||||||
|
|||||||
@@ -2,6 +2,28 @@ import type { ExcalidrawElementSkeleton } from "@excalidraw/excalidraw/element/t
|
|||||||
import type { FileId } from "@excalidraw/excalidraw/element/types";
|
import type { FileId } from "@excalidraw/excalidraw/element/types";
|
||||||
|
|
||||||
const elements: ExcalidrawElementSkeleton[] = [
|
const elements: ExcalidrawElementSkeleton[] = [
|
||||||
|
// {
|
||||||
|
// type: "arrow",
|
||||||
|
// x: 100,
|
||||||
|
// y: 500,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// type: "arrow",
|
||||||
|
// x: 250,
|
||||||
|
// y: 250,
|
||||||
|
// label: {
|
||||||
|
// text: "HELLO WORLD!!",
|
||||||
|
// },
|
||||||
|
// start: {
|
||||||
|
// type: "rectangle",
|
||||||
|
// // x: -100,
|
||||||
|
// },
|
||||||
|
// end: {
|
||||||
|
// type: "ellipse",
|
||||||
|
// // x: 300,
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
|
||||||
{
|
{
|
||||||
type: "rectangle",
|
type: "rectangle",
|
||||||
x: 10,
|
x: 10,
|
||||||
@@ -22,14 +44,14 @@ const elements: ExcalidrawElementSkeleton[] = [
|
|||||||
},
|
},
|
||||||
id: "2",
|
id: "2",
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
type: "arrow",
|
// type: "arrow",
|
||||||
x: 100,
|
// x: 100,
|
||||||
y: 200,
|
// y: 200,
|
||||||
label: { text: "HELLO WORLD!!" },
|
// label: { text: "HELLO WORLD!!" },
|
||||||
start: { type: "rectangle" },
|
// start: { type: "rectangle" },
|
||||||
end: { type: "ellipse" },
|
// end: { type: "ellipse" },
|
||||||
},
|
// },
|
||||||
{
|
{
|
||||||
type: "image",
|
type: "image",
|
||||||
x: 606.1042326312408,
|
x: 606.1042326312408,
|
||||||
@@ -38,11 +60,11 @@ const elements: ExcalidrawElementSkeleton[] = [
|
|||||||
height: 230,
|
height: 230,
|
||||||
fileId: "rocket" as FileId,
|
fileId: "rocket" as FileId,
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
type: "frame",
|
// type: "frame",
|
||||||
children: ["1", "2"],
|
// children: ["1", "2"],
|
||||||
name: "My frame",
|
// name: "My frame",
|
||||||
},
|
// },
|
||||||
];
|
];
|
||||||
export default {
|
export default {
|
||||||
elements,
|
elements,
|
||||||
|
|||||||
@@ -1822,7 +1822,7 @@ exports[`Test Transform > should transform the elements correctly when linear el
|
|||||||
"versionNonce": Any<Number>,
|
"versionNonce": Any<Number>,
|
||||||
"verticalAlign": "middle",
|
"verticalAlign": "middle",
|
||||||
"width": 120,
|
"width": 120,
|
||||||
"x": 187.7545,
|
"x": 187.75450000000004,
|
||||||
"y": 44.5,
|
"y": 44.5,
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -790,41 +790,27 @@ export const getArrowheadPoints = (
|
|||||||
p0 = pointFrom(prevOp.data[4], prevOp.data[5]);
|
p0 = pointFrom(prevOp.data[4], prevOp.data[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We know the last point of the arrow (or the first, if start arrowhead).
|
// B(t) = p0 * (1-t)^3 + 3p1 * t * (1-t)^2 + 3p2 * t^2 * (1-t) + p3 * t^3
|
||||||
|
const equation = (t: number, idx: number) =>
|
||||||
|
Math.pow(1 - t, 3) * p3[idx] +
|
||||||
|
3 * t * Math.pow(1 - t, 2) * p2[idx] +
|
||||||
|
3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
|
||||||
|
p0[idx] * Math.pow(t, 3);
|
||||||
|
|
||||||
|
// Ee know the last point of the arrow (or the first, if start arrowhead).
|
||||||
const [x2, y2] = position === "start" ? p0 : p3;
|
const [x2, y2] = position === "start" ? p0 : p3;
|
||||||
|
|
||||||
// Use the analytic tangent at the Bézier endpoint for a precise arrowhead
|
// By using cubic bezier equation (B(t)) and the given parameters,
|
||||||
// direction. For a cubic Bézier B(t) with control points p0p3:
|
// we calculate a point that is closer to the last point.
|
||||||
// B'(1): (p3 − p2) tangent at the end
|
// The value 0.3 is chosen arbitrarily and it works best for all
|
||||||
// B'(0): (p1 − p0) for start arrowhead, arrow points away: (p0 − p1)
|
// the tested cases.
|
||||||
let dx: number;
|
const [x1, y1] = [equation(0.3, 0), equation(0.3, 1)];
|
||||||
let dy: number;
|
|
||||||
if (position === "end") {
|
// Find the normalized direction vector based on the
|
||||||
dx = p3[0] - p2[0];
|
// previously calculated points.
|
||||||
dy = p3[1] - p2[1];
|
const distance = Math.hypot(x2 - x1, y2 - y1);
|
||||||
if (Math.hypot(dx, dy) < 1e-6) {
|
const nx = (x2 - x1) / distance;
|
||||||
dx = p3[0] - p1[0];
|
const ny = (y2 - y1) / distance;
|
||||||
dy = p3[1] - p1[1];
|
|
||||||
}
|
|
||||||
if (Math.hypot(dx, dy) < 1e-6) {
|
|
||||||
dx = p3[0] - p0[0];
|
|
||||||
dy = p3[1] - p0[1];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
dx = p0[0] - p1[0];
|
|
||||||
dy = p0[1] - p1[1];
|
|
||||||
if (Math.hypot(dx, dy) < 1e-6) {
|
|
||||||
dx = p0[0] - p2[0];
|
|
||||||
dy = p0[1] - p2[1];
|
|
||||||
}
|
|
||||||
if (Math.hypot(dx, dy) < 1e-6) {
|
|
||||||
dx = p0[0] - p3[0];
|
|
||||||
dy = p0[1] - p3[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const distance = Math.hypot(dx, dy);
|
|
||||||
const nx = dx / distance;
|
|
||||||
const ny = dy / distance;
|
|
||||||
|
|
||||||
const size = getArrowheadSize(arrowhead);
|
const size = getArrowheadSize(arrowhead);
|
||||||
|
|
||||||
@@ -894,10 +880,30 @@ export const getArrowheadPoints = (
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (arrowhead === "diamond" || arrowhead === "diamond_outline") {
|
if (arrowhead === "diamond" || arrowhead === "diamond_outline") {
|
||||||
// point opposite to the arrowhead point, just mirrored across the (tx, ty)
|
// point opposite to the arrowhead point
|
||||||
// point
|
let ox;
|
||||||
const ox = tx - nx * minSize * 2;
|
let oy;
|
||||||
const oy = ty - ny * minSize * 2;
|
|
||||||
|
if (position === "start") {
|
||||||
|
const [px, py] = element.points.length > 1 ? element.points[1] : [0, 0];
|
||||||
|
|
||||||
|
[ox, oy] = pointRotateRads(
|
||||||
|
pointFrom(tx + minSize * 2, ty),
|
||||||
|
pointFrom(tx, ty),
|
||||||
|
Math.atan2(py - ty, px - tx) as Radians,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const [px, py] =
|
||||||
|
element.points.length > 1
|
||||||
|
? element.points[element.points.length - 2]
|
||||||
|
: [0, 0];
|
||||||
|
|
||||||
|
[ox, oy] = pointRotateRads(
|
||||||
|
pointFrom(tx - minSize * 2, ty),
|
||||||
|
pointFrom(tx, ty),
|
||||||
|
Math.atan2(ty - py, tx - px) as Radians,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return [tx, ty, x3, y3, ox, oy, x4, y4];
|
return [tx, ty, x3, y3, ox, oy, x4, y4];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -790,20 +790,9 @@ export class LinearElementEditor {
|
|||||||
elementsMap,
|
elementsMap,
|
||||||
);
|
);
|
||||||
|
|
||||||
const [lines, segCurves] = deconstructLinearOrFreeDrawElement(
|
|
||||||
element,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
const segmentCount = lines.length + segCurves.length;
|
|
||||||
|
|
||||||
let index = 0;
|
let index = 0;
|
||||||
const midpoints: (GlobalPoint | null)[] = [];
|
const midpoints: (GlobalPoint | null)[] = [];
|
||||||
while (index < points.length - 1) {
|
while (index < points.length - 1) {
|
||||||
if (segmentCount > 0 && index >= segmentCount) {
|
|
||||||
midpoints.push(null);
|
|
||||||
index++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
LinearElementEditor.isSegmentTooShort(
|
LinearElementEditor.isSegmentTooShort(
|
||||||
element,
|
element,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import rough from "roughjs/bin/rough";
|
import rough from "roughjs/bin/rough";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
clamp,
|
||||||
type GlobalPoint,
|
type GlobalPoint,
|
||||||
isRightAngleRads,
|
isRightAngleRads,
|
||||||
lineSegment,
|
lineSegment,
|
||||||
@@ -105,8 +106,62 @@ const getCanvasPadding = (element: ExcalidrawElement) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const resolveRenderOpacity = (
|
||||||
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<
|
||||||
|
StaticCanvasRenderConfig,
|
||||||
|
"elementOpacityOverrides" | "resolveRenderOpacity"
|
||||||
|
>,
|
||||||
|
) => {
|
||||||
|
const override = renderConfig.elementOpacityOverrides?.get(element.id);
|
||||||
|
|
||||||
|
if (override !== undefined) {
|
||||||
|
return clamp(override, 0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedOpacity = renderConfig.resolveRenderOpacity?.(
|
||||||
|
element as NonDeletedExcalidrawElement,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (resolvedOpacity !== undefined) {
|
||||||
|
return clamp(resolvedOpacity, 0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return element.opacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const resolveRenderPositionOffset = (
|
||||||
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<StaticCanvasRenderConfig, "elementPositionOverrides">,
|
||||||
|
) => {
|
||||||
|
return renderConfig.elementPositionOverrides?.get(element.id) ?? { x: 0, y: 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRenderElementWithPositionOverride = <
|
||||||
|
TElement extends NonDeletedExcalidrawElement,
|
||||||
|
>(
|
||||||
|
element: TElement,
|
||||||
|
renderConfig: Pick<StaticCanvasRenderConfig, "elementPositionOverrides">,
|
||||||
|
): TElement => {
|
||||||
|
const positionOffset = resolveRenderPositionOffset(element, renderConfig);
|
||||||
|
|
||||||
|
if (positionOffset.x === 0 && positionOffset.y === 0) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...element,
|
||||||
|
x: element.x + positionOffset.x,
|
||||||
|
y: element.y + positionOffset.y,
|
||||||
|
} as TElement;
|
||||||
|
};
|
||||||
|
|
||||||
export const getRenderOpacity = (
|
export const getRenderOpacity = (
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
|
renderConfig: Pick<
|
||||||
|
StaticCanvasRenderConfig,
|
||||||
|
"elementOpacityOverrides" | "resolveRenderOpacity"
|
||||||
|
>,
|
||||||
containingFrame: ExcalidrawFrameLikeElement | null,
|
containingFrame: ExcalidrawFrameLikeElement | null,
|
||||||
elementsPendingErasure: ElementsPendingErasure,
|
elementsPendingErasure: ElementsPendingErasure,
|
||||||
pendingNodes: Readonly<PendingExcalidrawElements> | null,
|
pendingNodes: Readonly<PendingExcalidrawElements> | null,
|
||||||
@@ -115,7 +170,8 @@ export const getRenderOpacity = (
|
|||||||
// multiplying frame opacity with element opacity to combine them
|
// multiplying frame opacity with element opacity to combine them
|
||||||
// (e.g. frame 50% and element 50% opacity should result in 25% opacity)
|
// (e.g. frame 50% and element 50% opacity should result in 25% opacity)
|
||||||
let opacity =
|
let opacity =
|
||||||
(((containingFrame?.opacity ?? 100) * element.opacity) / 10000) *
|
(((containingFrame?.opacity ?? 100) * resolveRenderOpacity(element, renderConfig)) /
|
||||||
|
10000) *
|
||||||
globalAlpha;
|
globalAlpha;
|
||||||
|
|
||||||
// if pending erasure, multiply again to combine further
|
// if pending erasure, multiply again to combine further
|
||||||
@@ -791,8 +847,11 @@ export const renderElement = (
|
|||||||
!appState.selectedElementIds[element.id] &&
|
!appState.selectedElementIds[element.id] &&
|
||||||
!appState.hoveredElementIds[element.id];
|
!appState.hoveredElementIds[element.id];
|
||||||
|
|
||||||
|
element = getRenderElementWithPositionOverride(element, renderConfig);
|
||||||
|
|
||||||
context.globalAlpha = getRenderOpacity(
|
context.globalAlpha = getRenderOpacity(
|
||||||
element,
|
element,
|
||||||
|
renderConfig,
|
||||||
getContainingFrame(element, elementsMap),
|
getContainingFrame(element, elementsMap),
|
||||||
renderConfig.elementsPendingErasure,
|
renderConfig.elementsPendingErasure,
|
||||||
renderConfig.pendingFlowchartNodes,
|
renderConfig.pendingFlowchartNodes,
|
||||||
|
|||||||
+52
-305
@@ -78,18 +78,6 @@ import type {
|
|||||||
import type { Drawable, Options } from "roughjs/bin/core";
|
import type { Drawable, Options } from "roughjs/bin/core";
|
||||||
import type { Point as RoughPoint } from "roughjs/bin/geometry";
|
import type { Point as RoughPoint } from "roughjs/bin/geometry";
|
||||||
|
|
||||||
// Controls how handle distance scales with chord length.
|
|
||||||
// At 1.0 handles are exactly h/3 (standard Hermite). Values below 1 make
|
|
||||||
// short segments curvier and long segments more taut (sub-linear scaling).
|
|
||||||
const CP_CHORD_POWER = 1;
|
|
||||||
|
|
||||||
// At curved knots the C2 spline tangent can be tilted away from the
|
|
||||||
// bisector direction, making one side of the knot tight and the other taut.
|
|
||||||
// This factor [0, 1] controls how far the tangent direction is pulled toward
|
|
||||||
// the bisector (the chord-bisector normal) linearly with turn sharpness.
|
|
||||||
// 0 = pure C2 spline; 1 = tangent fully aligned with the bisector.
|
|
||||||
const CP_ANGLE_CORRECTION = 1;
|
|
||||||
|
|
||||||
export class ShapeCache {
|
export class ShapeCache {
|
||||||
private static rg = new RoughGenerator();
|
private static rg = new RoughGenerator();
|
||||||
private static cache = new WeakMap<
|
private static cache = new WeakMap<
|
||||||
@@ -637,144 +625,60 @@ export const generateLinearCollisionShape = (
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate collision ops using the same bisector-based cubic Bézier
|
return generator
|
||||||
// algorithm as generateRoundedSimpleArrowShape so hit-testing matches rendering.
|
.curve(points as unknown as RoughPoint[], options)
|
||||||
const rotateLocal = (lx: number, ly: number): LocalPoint => {
|
.sets[0].ops.slice(0, element.points.length)
|
||||||
const g = pointRotateRads<GlobalPoint>(
|
.map((op, i) => {
|
||||||
pointFrom<GlobalPoint>(element.x + lx, element.y + ly),
|
if (i === 0) {
|
||||||
center,
|
const p = pointRotateRads<GlobalPoint>(
|
||||||
element.angle,
|
pointFrom<GlobalPoint>(
|
||||||
);
|
element.x + op.data[0],
|
||||||
return pointFrom<LocalPoint>(g[0] - element.x, g[1] - element.y);
|
element.y + op.data[1],
|
||||||
};
|
),
|
||||||
|
center,
|
||||||
|
element.angle,
|
||||||
|
);
|
||||||
|
|
||||||
const collisionOps: Array<{
|
return {
|
||||||
op: string;
|
op: "move",
|
||||||
data: number[] | LocalPoint;
|
data: pointFrom<LocalPoint>(p[0] - element.x, p[1] - element.y),
|
||||||
}> = [];
|
};
|
||||||
collisionOps.push({
|
|
||||||
op: "move",
|
|
||||||
data: rotateLocal(points[0][0], points[0][1]),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (points.length === 2) {
|
|
||||||
collisionOps.push({
|
|
||||||
op: "lineTo",
|
|
||||||
data: rotateLocal(points[1][0], points[1][1]),
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Chord-length C2 spline. Mirrors generateRoundedSimpleArrowShape
|
|
||||||
// exactly so hit-testing matches rendering.
|
|
||||||
const n = points.length - 1;
|
|
||||||
const h = new Float64Array(n);
|
|
||||||
for (let i = 0; i < n; i++) {
|
|
||||||
h[i] = Math.max(
|
|
||||||
1e-10,
|
|
||||||
Math.hypot(
|
|
||||||
points[i + 1][0] - points[i][0],
|
|
||||||
points[i + 1][1] - points[i][1],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const mx = new Float64Array(n + 1);
|
|
||||||
const my = new Float64Array(n + 1);
|
|
||||||
const diag = new Float64Array(n + 1);
|
|
||||||
const rhsX = new Float64Array(n + 1);
|
|
||||||
const rhsY = new Float64Array(n + 1);
|
|
||||||
|
|
||||||
diag[0] = 2;
|
|
||||||
rhsX[0] = (3 * (points[1][0] - points[0][0])) / h[0];
|
|
||||||
rhsY[0] = (3 * (points[1][1] - points[0][1])) / h[0];
|
|
||||||
for (let i = 1; i < n; i++) {
|
|
||||||
diag[i] = 2 * (h[i - 1] + h[i]);
|
|
||||||
rhsX[i] =
|
|
||||||
3 *
|
|
||||||
((h[i] * (points[i][0] - points[i - 1][0])) / h[i - 1] +
|
|
||||||
(h[i - 1] * (points[i + 1][0] - points[i][0])) / h[i]);
|
|
||||||
rhsY[i] =
|
|
||||||
3 *
|
|
||||||
((h[i] * (points[i][1] - points[i - 1][1])) / h[i - 1] +
|
|
||||||
(h[i - 1] * (points[i + 1][1] - points[i][1])) / h[i]);
|
|
||||||
}
|
|
||||||
diag[n] = 2;
|
|
||||||
rhsX[n] = (3 * (points[n][0] - points[n - 1][0])) / h[n - 1];
|
|
||||||
rhsY[n] = (3 * (points[n][1] - points[n - 1][1])) / h[n - 1];
|
|
||||||
|
|
||||||
for (let i = 1; i <= n; i++) {
|
|
||||||
const sub = i < n ? h[i] : 1;
|
|
||||||
const supPrev = i === 1 ? 1 : h[i - 2];
|
|
||||||
const w = sub / diag[i - 1];
|
|
||||||
diag[i] -= w * supPrev;
|
|
||||||
rhsX[i] -= w * rhsX[i - 1];
|
|
||||||
rhsY[i] -= w * rhsY[i - 1];
|
|
||||||
}
|
|
||||||
mx[n] = rhsX[n] / diag[n];
|
|
||||||
my[n] = rhsY[n] / diag[n];
|
|
||||||
for (let i = n - 1; i >= 0; i--) {
|
|
||||||
const sup = i === 0 ? 1 : h[i - 1];
|
|
||||||
mx[i] = (rhsX[i] - sup * mx[i + 1]) / diag[i];
|
|
||||||
my[i] = (rhsY[i] - sup * my[i + 1]) / diag[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalised tangent directions; handle length scales sub-linearly with chord.
|
|
||||||
const mlen = new Float64Array(n + 1);
|
|
||||||
for (let i = 0; i <= n; i++) {
|
|
||||||
mlen[i] = Math.max(1e-10, Math.hypot(mx[i], my[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// At interior knots, blend the C2 tangent direction toward the
|
|
||||||
// bisector direction by a factor proportional to turn sharpness *
|
|
||||||
// CP_ANGLE_CORRECTION
|
|
||||||
for (let k = 1; k < n; k++) {
|
|
||||||
const d1x = (points[k][0] - points[k - 1][0]) / h[k - 1];
|
|
||||||
const d1y = (points[k][1] - points[k - 1][1]) / h[k - 1];
|
|
||||||
const d2x = (points[k + 1][0] - points[k][0]) / h[k];
|
|
||||||
const d2y = (points[k + 1][1] - points[k][1]) / h[k];
|
|
||||||
const dot = d1x * d2x + d1y * d2y;
|
|
||||||
const t = ((1 - dot) / 2) * CP_ANGLE_CORRECTION;
|
|
||||||
if (t < 1e-6) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
const bx = d1x + d2x;
|
|
||||||
const by = d1y + d2y;
|
|
||||||
const blen = Math.hypot(bx, by);
|
|
||||||
if (blen < 1e-10) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let px = bx / blen;
|
|
||||||
let py = by / blen;
|
|
||||||
const tx = mx[k] / mlen[k];
|
|
||||||
const ty = my[k] / mlen[k];
|
|
||||||
if (tx * px + ty * py < 0) {
|
|
||||||
px = -px;
|
|
||||||
py = -py;
|
|
||||||
}
|
|
||||||
const blendX = tx + t * (px - tx);
|
|
||||||
const blendY = ty + t * (py - ty);
|
|
||||||
const blendLen = Math.max(1e-10, Math.hypot(blendX, blendY));
|
|
||||||
mx[k] = (blendX / blendLen) * mlen[k];
|
|
||||||
my[k] = (blendY / blendLen) * mlen[k];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < n; i++) {
|
return {
|
||||||
const cpDist = Math.pow(h[i], CP_CHORD_POWER) / 3;
|
|
||||||
const cp1x = points[i][0] + (mx[i] / mlen[i]) * cpDist;
|
|
||||||
const cp1y = points[i][1] + (my[i] / mlen[i]) * cpDist;
|
|
||||||
const cp2x = points[i + 1][0] - (mx[i + 1] / mlen[i + 1]) * cpDist;
|
|
||||||
const cp2y = points[i + 1][1] - (my[i + 1] / mlen[i + 1]) * cpDist;
|
|
||||||
|
|
||||||
const rcp1 = rotateLocal(cp1x, cp1y);
|
|
||||||
const rcp2 = rotateLocal(cp2x, cp2y);
|
|
||||||
const rend = rotateLocal(points[i + 1][0], points[i + 1][1]);
|
|
||||||
|
|
||||||
collisionOps.push({
|
|
||||||
op: "bcurveTo",
|
op: "bcurveTo",
|
||||||
data: [rcp1[0], rcp1[1], rcp2[0], rcp2[1], rend[0], rend[1]],
|
data: [
|
||||||
});
|
pointRotateRads(
|
||||||
}
|
pointFrom<GlobalPoint>(
|
||||||
}
|
element.x + op.data[0],
|
||||||
return collisionOps;
|
element.y + op.data[1],
|
||||||
|
),
|
||||||
|
center,
|
||||||
|
element.angle,
|
||||||
|
),
|
||||||
|
pointRotateRads(
|
||||||
|
pointFrom<GlobalPoint>(
|
||||||
|
element.x + op.data[2],
|
||||||
|
element.y + op.data[3],
|
||||||
|
),
|
||||||
|
center,
|
||||||
|
element.angle,
|
||||||
|
),
|
||||||
|
pointRotateRads(
|
||||||
|
pointFrom<GlobalPoint>(
|
||||||
|
element.x + op.data[4],
|
||||||
|
element.y + op.data[5],
|
||||||
|
),
|
||||||
|
center,
|
||||||
|
element.angle,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
.map((p) =>
|
||||||
|
pointFrom<LocalPoint>(p[0] - element.x, p[1] - element.y),
|
||||||
|
)
|
||||||
|
.flat(),
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
case "freedraw": {
|
case "freedraw": {
|
||||||
if (element.points.length < 2) {
|
if (element.points.length < 2) {
|
||||||
@@ -1016,12 +920,7 @@ const _generateElementShape = (
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
shape = [
|
shape = [generator.curve(points as unknown as RoughPoint[], options)];
|
||||||
generator.path(
|
|
||||||
generateRoundedSimpleArrowShape(points),
|
|
||||||
generateRoughOptions(element, true, isDarkMode),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add lines only in arrow
|
// add lines only in arrow
|
||||||
@@ -1105,162 +1004,10 @@ const _generateElementShape = (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const generateRoundedSimpleArrowShape = (
|
|
||||||
points: readonly LocalPoint[],
|
|
||||||
): string => {
|
|
||||||
if (points.length < 2) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (points.length === 2) {
|
|
||||||
return `M ${points[0][0]} ${points[0][1]} L ${points[1][0]} ${points[1][1]}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chord-length parameterised C2 natural cubic spline (Thomas's algorithm).
|
|
||||||
//
|
|
||||||
// Unknowns: tangent vectors m[0..n] at each knot (n = number of segments).
|
|
||||||
// Chord lengths h[i] = |K[i+1] − K[i]| act as the parameter intervals so
|
|
||||||
// that tightly-spaced knots don't over-influence distant ones.
|
|
||||||
//
|
|
||||||
// Row 0: 2·m₀ + m₁ = 3·(K₁−K₀)/h₀
|
|
||||||
// Row i: h[i]·mᵢ₋₁ + 2·(h[i−1]+h[i])·mᵢ + h[i−1]·mᵢ₊₁
|
|
||||||
// = 3·(h[i]·(Kᵢ−Kᵢ₋₁)/h[i−1]
|
|
||||||
// + h[i−1]·(Kᵢ₊₁−Kᵢ)/h[i]) 1≤i≤n−1
|
|
||||||
// Row n: mₙ₋₁ + 2·mₙ = 3·(Kₙ−Kₙ₋₁)/h[n−1]
|
|
||||||
//
|
|
||||||
// Bézier control points from Hermite→Bézier identity:
|
|
||||||
// cp1ᵢ = Kᵢ + mᵢ · h[i] / 3
|
|
||||||
// cp2ᵢ = Kᵢ₊₁ − mᵢ₊₁ · h[i] / 3
|
|
||||||
const n = points.length - 1; // number of segments
|
|
||||||
const h = new Float64Array(n);
|
|
||||||
for (let i = 0; i < n; i++) {
|
|
||||||
h[i] = Math.max(
|
|
||||||
1e-10,
|
|
||||||
Math.hypot(
|
|
||||||
points[i + 1][0] - points[i][0],
|
|
||||||
points[i + 1][1] - points[i][1],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const mx = new Float64Array(n + 1);
|
|
||||||
const my = new Float64Array(n + 1);
|
|
||||||
const diag = new Float64Array(n + 1);
|
|
||||||
const rhsX = new Float64Array(n + 1);
|
|
||||||
const rhsY = new Float64Array(n + 1);
|
|
||||||
|
|
||||||
// Row 0 – natural BC (zero second derivative at start)
|
|
||||||
diag[0] = 2;
|
|
||||||
rhsX[0] = (3 * (points[1][0] - points[0][0])) / h[0];
|
|
||||||
rhsY[0] = (3 * (points[1][1] - points[0][1])) / h[0];
|
|
||||||
|
|
||||||
// Interior rows
|
|
||||||
for (let i = 1; i < n; i++) {
|
|
||||||
diag[i] = 2 * (h[i - 1] + h[i]);
|
|
||||||
rhsX[i] =
|
|
||||||
3 *
|
|
||||||
((h[i] * (points[i][0] - points[i - 1][0])) / h[i - 1] +
|
|
||||||
(h[i - 1] * (points[i + 1][0] - points[i][0])) / h[i]);
|
|
||||||
rhsY[i] =
|
|
||||||
3 *
|
|
||||||
((h[i] * (points[i][1] - points[i - 1][1])) / h[i - 1] +
|
|
||||||
(h[i - 1] * (points[i + 1][1] - points[i][1])) / h[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Row n – natural BC (zero second derivative at end)
|
|
||||||
diag[n] = 2;
|
|
||||||
rhsX[n] = (3 * (points[n][0] - points[n - 1][0])) / h[n - 1];
|
|
||||||
rhsY[n] = (3 * (points[n][1] - points[n - 1][1])) / h[n - 1];
|
|
||||||
|
|
||||||
// Forward sweep
|
|
||||||
// sub[i] = h[i] for i=1..n−1, sub[n] = 1
|
|
||||||
// sup[i] = 1 for i=0, h[i−1] for i=1..n−1 (never modified)
|
|
||||||
for (let i = 1; i <= n; i++) {
|
|
||||||
const sub = i < n ? h[i] : 1;
|
|
||||||
const supPrev = i === 1 ? 1 : h[i - 2];
|
|
||||||
const w = sub / diag[i - 1];
|
|
||||||
diag[i] -= w * supPrev;
|
|
||||||
rhsX[i] -= w * rhsX[i - 1];
|
|
||||||
rhsY[i] -= w * rhsY[i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Back substitution
|
|
||||||
mx[n] = rhsX[n] / diag[n];
|
|
||||||
my[n] = rhsY[n] / diag[n];
|
|
||||||
for (let i = n - 1; i >= 0; i--) {
|
|
||||||
const sup = i === 0 ? 1 : h[i - 1];
|
|
||||||
mx[i] = (rhsX[i] - sup * mx[i + 1]) / diag[i];
|
|
||||||
my[i] = (rhsY[i] - sup * my[i + 1]) / diag[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalised tangent directions; handle length scales sub-linearly with chord.
|
|
||||||
const mlen = new Float64Array(n + 1);
|
|
||||||
for (let i = 0; i <= n; i++) {
|
|
||||||
mlen[i] = Math.max(1e-10, Math.hypot(mx[i], my[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// At interior knots, blend the C2 tangent direction toward the
|
|
||||||
// perpendicular-to-bisector (the perfectly symmetric tangent) by a factor
|
|
||||||
// proportional to turn sharpness × CP_ANGLE_CORRECTION.
|
|
||||||
// Both cp2 (incoming) and cp1 (outgoing) at the knot share the same adjusted
|
|
||||||
// direction, so collinear (aligned) handles are preserved.
|
|
||||||
for (let k = 1; k < n; k++) {
|
|
||||||
const d1x = (points[k][0] - points[k - 1][0]) / h[k - 1];
|
|
||||||
const d1y = (points[k][1] - points[k - 1][1]) / h[k - 1];
|
|
||||||
const d2x = (points[k + 1][0] - points[k][0]) / h[k];
|
|
||||||
const d2y = (points[k + 1][1] - points[k][1]) / h[k];
|
|
||||||
const dot = d1x * d2x + d1y * d2y;
|
|
||||||
// t: 0 = straight, 1 = hairpin
|
|
||||||
const t = ((1 - dot) / 2) * CP_ANGLE_CORRECTION;
|
|
||||||
if (t < 1e-6) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Bisector of the two chord directions as the "normal" at the knot.
|
|
||||||
// Its perpendicular is the ideal symmetric tangent direction.
|
|
||||||
const bx = d1x + d2x;
|
|
||||||
const by = d1y + d2y;
|
|
||||||
const blen = Math.hypot(bx, by);
|
|
||||||
if (blen < 1e-10) {
|
|
||||||
continue; // 180° hairpin – bisector undefined, skip
|
|
||||||
}
|
|
||||||
// Blend target: bisector direction (pick sign aligning with current tangent)
|
|
||||||
let px = bx / blen;
|
|
||||||
let py = by / blen;
|
|
||||||
const tx = mx[k] / mlen[k];
|
|
||||||
const ty = my[k] / mlen[k];
|
|
||||||
if (tx * px + ty * py < 0) {
|
|
||||||
px = -px;
|
|
||||||
py = -py;
|
|
||||||
}
|
|
||||||
// Linear blend of unit directions, then renormalize to preserve magnitude.
|
|
||||||
const blendX = tx + t * (px - tx);
|
|
||||||
const blendY = ty + t * (py - ty);
|
|
||||||
const blendLen = Math.max(1e-10, Math.hypot(blendX, blendY));
|
|
||||||
mx[k] = (blendX / blendLen) * mlen[k];
|
|
||||||
my[k] = (blendY / blendLen) * mlen[k];
|
|
||||||
}
|
|
||||||
|
|
||||||
const path: string[] = [`M ${points[0][0]} ${points[0][1]}`];
|
|
||||||
for (let i = 0; i < n; i++) {
|
|
||||||
const cpDist = Math.pow(h[i], CP_CHORD_POWER) / 3;
|
|
||||||
const cp1x = points[i][0] + (mx[i] / mlen[i]) * cpDist;
|
|
||||||
const cp1y = points[i][1] + (my[i] / mlen[i]) * cpDist;
|
|
||||||
const cp2x = points[i + 1][0] - (mx[i + 1] / mlen[i + 1]) * cpDist;
|
|
||||||
const cp2y = points[i + 1][1] - (my[i + 1] / mlen[i + 1]) * cpDist;
|
|
||||||
path.push(
|
|
||||||
`C ${cp1x} ${cp1y} ${cp2x} ${cp2y} ${points[i + 1][0]} ${
|
|
||||||
points[i + 1][1]
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return path.join(" ");
|
|
||||||
};
|
|
||||||
|
|
||||||
const generateElbowArrowShape = (
|
const generateElbowArrowShape = (
|
||||||
points: readonly LocalPoint[],
|
points: readonly LocalPoint[],
|
||||||
radius: number,
|
radius: number,
|
||||||
): string => {
|
) => {
|
||||||
const subpoints = [] as [number, number][];
|
const subpoints = [] as [number, number][];
|
||||||
for (let i = 1; i < points.length - 1; i += 1) {
|
for (let i = 1; i < points.length - 1; i += 1) {
|
||||||
const prev = points[i - 1];
|
const prev = points[i - 1];
|
||||||
|
|||||||
@@ -135,9 +135,9 @@ describe("getElementBounds", () => {
|
|||||||
} as ExcalidrawLinearElement;
|
} as ExcalidrawLinearElement;
|
||||||
|
|
||||||
const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
|
const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
|
||||||
expect(x1).toEqual(366.0476290709661);
|
expect(x1).toEqual(360.9291017525165);
|
||||||
expect(y1).toEqual(186.59818534770224);
|
expect(y1).toEqual(185.24770129343722);
|
||||||
expect(x2).toEqual(494.6034220048372);
|
expect(x2).toEqual(481.4815539037601);
|
||||||
expect(y2).toEqual(324.16489799221546);
|
expect(y2).toEqual(319.8162855827246);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ describe("check rotated elements can be hit:", () => {
|
|||||||
] as LocalPoint[],
|
] as LocalPoint[],
|
||||||
});
|
});
|
||||||
const hit = hitElementItself({
|
const hit = hitElementItself({
|
||||||
point: pointFrom<GlobalPoint>(90, -70),
|
point: pointFrom<GlobalPoint>(88, -68),
|
||||||
element: window.h.elements[0],
|
element: window.h.elements[0],
|
||||||
threshold: 10,
|
threshold: 10,
|
||||||
elementsMap: window.h.scene.getNonDeletedElementsMap(),
|
elementsMap: window.h.scene.getNonDeletedElementsMap(),
|
||||||
|
|||||||
@@ -434,12 +434,12 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(midPointsWithRoundEdge).toMatchInlineSnapshot(`
|
expect(midPointsWithRoundEdge).toMatchInlineSnapshot(`
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"51.36383",
|
"54.27552",
|
||||||
"54.86323",
|
"46.16120",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"81.64884",
|
"76.95494",
|
||||||
"43.04575",
|
"44.56052",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
@@ -499,12 +499,12 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(newMidPoints).toMatchInlineSnapshot(`
|
expect(newMidPoints).toMatchInlineSnapshot(`
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"101.36383",
|
"104.27552",
|
||||||
"74.86323",
|
"66.16120",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"131.64884",
|
"126.95494",
|
||||||
"63.04575",
|
"64.56052",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
@@ -707,8 +707,14 @@ describe("Test Linear Elements", () => {
|
|||||||
// This is the expected midpoint for line with round edge
|
// This is the expected midpoint for line with round edge
|
||||||
// hence hardcoding it so if later some bug is introduced
|
// hence hardcoding it so if later some bug is introduced
|
||||||
// this will fail and we can fix it
|
// this will fail and we can fix it
|
||||||
const firstSegmentMidpoint = pointFrom<GlobalPoint>(47.30521, 57.2734);
|
const firstSegmentMidpoint = pointFrom<GlobalPoint>(
|
||||||
const lastSegmentMidpoint = pointFrom<GlobalPoint>(83.70877, 40.46424);
|
55.9697848965255,
|
||||||
|
47.442326230998205,
|
||||||
|
);
|
||||||
|
const lastSegmentMidpoint = pointFrom<GlobalPoint>(
|
||||||
|
76.08587175006699,
|
||||||
|
43.294165939653226,
|
||||||
|
);
|
||||||
let line: ExcalidrawLinearElement;
|
let line: ExcalidrawLinearElement;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -753,16 +759,16 @@ describe("Test Linear Elements", () => {
|
|||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"77.30521",
|
"85.96978",
|
||||||
"87.27340",
|
"77.44233",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
70,
|
70,
|
||||||
50,
|
50,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"113.70877",
|
"106.08587",
|
||||||
"70.46424",
|
"73.29417",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
40,
|
40,
|
||||||
@@ -809,12 +815,12 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(newMidPoints).toMatchInlineSnapshot(`
|
expect(newMidPoints).toMatchInlineSnapshot(`
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"22.32088",
|
"29.28349",
|
||||||
"37.43003",
|
"20.91105",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"81.55727",
|
"78.86048",
|
||||||
"43.21091",
|
"46.12277",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
@@ -898,12 +904,12 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(newMidPoints).toMatchInlineSnapshot(`
|
expect(newMidPoints).toMatchInlineSnapshot(`
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"51.36383",
|
"54.27552",
|
||||||
"54.86323",
|
"46.16120",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"81.64884",
|
"76.95494",
|
||||||
"43.04575",
|
"44.56052",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
@@ -1065,8 +1071,8 @@ describe("Test Linear Elements", () => {
|
|||||||
);
|
);
|
||||||
expect(position).toMatchInlineSnapshot(`
|
expect(position).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"x": "86.53100",
|
"x": "86.17305",
|
||||||
"y": "72.83556",
|
"y": "76.11251",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
@@ -1185,8 +1191,8 @@ describe("Test Linear Elements", () => {
|
|||||||
20,
|
20,
|
||||||
105,
|
105,
|
||||||
80,
|
80,
|
||||||
"56.68277",
|
"55.45894",
|
||||||
"47.27188",
|
45,
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
|
|
||||||
@@ -1196,7 +1202,7 @@ describe("Test Linear Elements", () => {
|
|||||||
.toMatchInlineSnapshot(`
|
.toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"height": 130,
|
"height": 130,
|
||||||
"width": "368.53316",
|
"width": "366.11716",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
@@ -1208,7 +1214,7 @@ describe("Test Linear Elements", () => {
|
|||||||
),
|
),
|
||||||
).toMatchInlineSnapshot(`
|
).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"x": "273.53316",
|
"x": "271.11716",
|
||||||
"y": 45,
|
"y": 45,
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
@@ -1225,10 +1231,10 @@ describe("Test Linear Elements", () => {
|
|||||||
[
|
[
|
||||||
20,
|
20,
|
||||||
35,
|
35,
|
||||||
"503.53316",
|
"501.11716",
|
||||||
"119.02540",
|
95,
|
||||||
"204.47758",
|
"205.45894",
|
||||||
"77.01270",
|
"52.50000",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -207,10 +207,12 @@ import {
|
|||||||
getLineHeightInPx,
|
getLineHeightInPx,
|
||||||
getApproxMinLineWidth,
|
getApproxMinLineWidth,
|
||||||
getApproxMinLineHeight,
|
getApproxMinLineHeight,
|
||||||
getMinTextElementWidth,
|
getMinTextElementWidth,
|
||||||
ShapeCache,
|
ShapeCache,
|
||||||
getRenderOpacity,
|
getRenderOpacity,
|
||||||
editGroupForSelectedElement,
|
resolveRenderPositionOffset,
|
||||||
|
resolveRenderOpacity,
|
||||||
|
editGroupForSelectedElement,
|
||||||
getElementsInGroup,
|
getElementsInGroup,
|
||||||
getSelectedGroupIdForElement,
|
getSelectedGroupIdForElement,
|
||||||
getSelectedGroupIds,
|
getSelectedGroupIds,
|
||||||
@@ -450,6 +452,7 @@ import { searchItemInFocusAtom } from "./SearchMenu";
|
|||||||
import { isSidebarDockedAtom } from "./Sidebar/Sidebar";
|
import { isSidebarDockedAtom } from "./Sidebar/Sidebar";
|
||||||
import { StaticCanvas, InteractiveCanvas } from "./canvases";
|
import { StaticCanvas, InteractiveCanvas } from "./canvases";
|
||||||
import NewElementCanvas from "./canvases/NewElementCanvas";
|
import NewElementCanvas from "./canvases/NewElementCanvas";
|
||||||
|
import { AnimationController } from "../renderer/animation";
|
||||||
import { isPointHittingLink } from "./hyperlink/helpers";
|
import { isPointHittingLink } from "./hyperlink/helpers";
|
||||||
import { MagicIcon, copyIcon, fullscreenIcon } from "./icons";
|
import { MagicIcon, copyIcon, fullscreenIcon } from "./icons";
|
||||||
import { AppStateObserver, type OnStateChange } from "./AppStateObserver";
|
import { AppStateObserver, type OnStateChange } from "./AppStateObserver";
|
||||||
@@ -740,6 +743,235 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
onRemoveEventListenersEmitter = new Emitter<[]>();
|
onRemoveEventListenersEmitter = new Emitter<[]>();
|
||||||
|
|
||||||
api: ExcalidrawImperativeAPI;
|
api: ExcalidrawImperativeAPI;
|
||||||
|
private renderAnimationVersion = 0;
|
||||||
|
private elementOpacityOverrides = new Map<string, number>();
|
||||||
|
private elementPositionOverrides = new Map<string, { x: number; y: number }>();
|
||||||
|
private elementAnimationStates = new Map<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
opacityFrom: number;
|
||||||
|
opacityTo: number;
|
||||||
|
positionFrom: { x: number; y: number };
|
||||||
|
positionTo: { x: number; y: number };
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut";
|
||||||
|
duration: number;
|
||||||
|
delay: number;
|
||||||
|
elapsed: number;
|
||||||
|
}
|
||||||
|
>();
|
||||||
|
|
||||||
|
private getElementAnimationKey = () => `${this.id}:animate-element`;
|
||||||
|
|
||||||
|
private bumpRenderAnimationVersion = () => {
|
||||||
|
this.renderAnimationVersion++;
|
||||||
|
this.setState({});
|
||||||
|
};
|
||||||
|
|
||||||
|
private getRenderOpacityConfig = () => ({
|
||||||
|
elementOpacityOverrides: this.elementOpacityOverrides,
|
||||||
|
elementPositionOverrides: this.elementPositionOverrides,
|
||||||
|
resolveRenderOpacity: this.props.resolveRenderOpacity,
|
||||||
|
});
|
||||||
|
|
||||||
|
private getResolvedElementOpacity = (element: NonDeletedExcalidrawElement) => {
|
||||||
|
return resolveRenderOpacity(element, this.getRenderOpacityConfig());
|
||||||
|
};
|
||||||
|
|
||||||
|
private getElementVisibleOpacity = (element: NonDeletedExcalidrawElement) => {
|
||||||
|
return clamp(element.opacity, 0, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
private applyAnimationEasing = (
|
||||||
|
progress: number,
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut",
|
||||||
|
) => {
|
||||||
|
switch (easing) {
|
||||||
|
case "linear":
|
||||||
|
return progress;
|
||||||
|
case "easeOut":
|
||||||
|
return easeOut(progress);
|
||||||
|
case "easeInOut":
|
||||||
|
return progress < 0.5
|
||||||
|
? 4 * progress * progress * progress
|
||||||
|
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private getFlyPositionOffset = (
|
||||||
|
element: NonDeletedExcalidrawElement,
|
||||||
|
from: "left" | "right" | "top" | "bottom",
|
||||||
|
) => {
|
||||||
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(
|
||||||
|
element,
|
||||||
|
this.scene.getNonDeletedElementsMap(),
|
||||||
|
);
|
||||||
|
const viewportWidth = this.state.width / this.state.zoom.value;
|
||||||
|
const viewportHeight = this.state.height / this.state.zoom.value;
|
||||||
|
const elementWidth = x2 - x1;
|
||||||
|
const elementHeight = y2 - y1;
|
||||||
|
|
||||||
|
switch (from) {
|
||||||
|
case "left":
|
||||||
|
return { x: -(Math.max(viewportWidth, elementWidth) + 64), y: 0 };
|
||||||
|
case "right":
|
||||||
|
return { x: Math.max(viewportWidth, elementWidth) + 64, y: 0 };
|
||||||
|
case "top":
|
||||||
|
return { x: 0, y: -(Math.max(viewportHeight, elementHeight) + 64) };
|
||||||
|
case "bottom":
|
||||||
|
return { x: 0, y: Math.max(viewportHeight, elementHeight) + 64 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private animateElement = ({
|
||||||
|
id,
|
||||||
|
opacityFrom,
|
||||||
|
opacityTo,
|
||||||
|
positionFrom = { x: 0, y: 0 },
|
||||||
|
positionTo = { x: 0, y: 0 },
|
||||||
|
easing,
|
||||||
|
duration,
|
||||||
|
delay,
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
opacityFrom: number;
|
||||||
|
opacityTo: number;
|
||||||
|
positionFrom?: { x: number; y: number };
|
||||||
|
positionTo?: { x: number; y: number };
|
||||||
|
easing: "linear" | "easeOut" | "easeInOut";
|
||||||
|
duration: number;
|
||||||
|
delay: number;
|
||||||
|
}) => {
|
||||||
|
const normalizedOpacityFrom = clamp(opacityFrom, 0, 100);
|
||||||
|
const normalizedOpacityTo = clamp(opacityTo, 0, 100);
|
||||||
|
const normalizedDuration = Math.max(duration, 0);
|
||||||
|
const normalizedDelay = Math.max(delay, 0);
|
||||||
|
|
||||||
|
this.elementOpacityOverrides.set(id, normalizedOpacityFrom);
|
||||||
|
|
||||||
|
if (positionFrom.x !== 0 || positionFrom.y !== 0) {
|
||||||
|
this.elementPositionOverrides.set(id, positionFrom);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalizedDuration === 0 && normalizedDelay === 0) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
this.elementOpacityOverrides.set(id, normalizedOpacityTo);
|
||||||
|
|
||||||
|
if (positionTo.x !== 0 || positionTo.y !== 0) {
|
||||||
|
this.elementPositionOverrides.set(id, positionTo);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.elementAnimationStates.set(id, {
|
||||||
|
opacityFrom: normalizedOpacityFrom,
|
||||||
|
opacityTo: normalizedOpacityTo,
|
||||||
|
positionFrom,
|
||||||
|
positionTo,
|
||||||
|
easing,
|
||||||
|
duration: normalizedDuration,
|
||||||
|
delay: normalizedDelay,
|
||||||
|
elapsed: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
this.syncElementAnimations();
|
||||||
|
};
|
||||||
|
|
||||||
|
private syncElementAnimations = () => {
|
||||||
|
const animationKey = this.getElementAnimationKey();
|
||||||
|
|
||||||
|
if (this.elementAnimationStates.size === 0) {
|
||||||
|
AnimationController.cancel(animationKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (AnimationController.running(animationKey)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimationController.start(animationKey, ({ deltaTime }) => {
|
||||||
|
let shouldRerender = false;
|
||||||
|
|
||||||
|
for (const [id, animation] of this.elementAnimationStates) {
|
||||||
|
const element = this.scene.getNonDeletedElement(id);
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
shouldRerender =
|
||||||
|
this.elementOpacityOverrides.delete(id) || shouldRerender;
|
||||||
|
shouldRerender =
|
||||||
|
this.elementPositionOverrides.delete(id) || shouldRerender;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
animation.elapsed += deltaTime;
|
||||||
|
|
||||||
|
const progress =
|
||||||
|
animation.elapsed <= animation.delay
|
||||||
|
? 0
|
||||||
|
: animation.duration === 0
|
||||||
|
? 1
|
||||||
|
: Math.min(
|
||||||
|
(animation.elapsed - animation.delay) / animation.duration,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
const easedProgress = this.applyAnimationEasing(
|
||||||
|
progress,
|
||||||
|
animation.easing,
|
||||||
|
);
|
||||||
|
|
||||||
|
const nextOpacity =
|
||||||
|
animation.opacityFrom +
|
||||||
|
(animation.opacityTo - animation.opacityFrom) * easedProgress;
|
||||||
|
const nextPosition = {
|
||||||
|
x:
|
||||||
|
animation.positionFrom.x +
|
||||||
|
(animation.positionTo.x - animation.positionFrom.x) * easedProgress,
|
||||||
|
y:
|
||||||
|
animation.positionFrom.y +
|
||||||
|
(animation.positionTo.y - animation.positionFrom.y) * easedProgress,
|
||||||
|
};
|
||||||
|
|
||||||
|
const clampedOpacity = clamp(nextOpacity, 0, 100);
|
||||||
|
|
||||||
|
if (this.elementOpacityOverrides.get(id) !== clampedOpacity) {
|
||||||
|
this.elementOpacityOverrides.set(id, clampedOpacity);
|
||||||
|
shouldRerender = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentPositionOverride =
|
||||||
|
this.elementPositionOverrides.get(id) ?? ({ x: 0, y: 0 } as const);
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentPositionOverride.x !== nextPosition.x ||
|
||||||
|
currentPositionOverride.y !== nextPosition.y
|
||||||
|
) {
|
||||||
|
if (nextPosition.x === 0 && nextPosition.y === 0) {
|
||||||
|
this.elementPositionOverrides.delete(id);
|
||||||
|
} else {
|
||||||
|
this.elementPositionOverrides.set(id, nextPosition);
|
||||||
|
}
|
||||||
|
shouldRerender = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animation.elapsed >= animation.delay + animation.duration) {
|
||||||
|
this.elementAnimationStates.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldRerender) {
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.elementAnimationStates.size > 0 ? {} : undefined;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
private createExcalidrawAPI(): ExcalidrawImperativeAPI {
|
private createExcalidrawAPI(): ExcalidrawImperativeAPI {
|
||||||
const api: ExcalidrawImperativeAPI = {
|
const api: ExcalidrawImperativeAPI = {
|
||||||
@@ -757,6 +989,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
clear: this.resetHistory,
|
clear: this.resetHistory,
|
||||||
},
|
},
|
||||||
scrollToContent: this.scrollToContent,
|
scrollToContent: this.scrollToContent,
|
||||||
|
animateElements: this.animateElements,
|
||||||
|
cancelElementAnimation: this.cancelElementAnimation,
|
||||||
|
clearElementAnimationOverrides: this.clearElementAnimationOverrides,
|
||||||
getSceneElements: this.getSceneElements,
|
getSceneElements: this.getSceneElements,
|
||||||
getAppState: () => this.state,
|
getAppState: () => this.state,
|
||||||
getFiles: () => this.files,
|
getFiles: () => this.files,
|
||||||
@@ -1734,6 +1969,10 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
const isHovered =
|
const isHovered =
|
||||||
this.state.activeEmbeddable?.element === el &&
|
this.state.activeEmbeddable?.element === el &&
|
||||||
this.state.activeEmbeddable?.state === "hover";
|
this.state.activeEmbeddable?.state === "hover";
|
||||||
|
const renderPositionOffset = resolveRenderPositionOffset(
|
||||||
|
el,
|
||||||
|
this.getRenderOpacityConfig(),
|
||||||
|
);
|
||||||
|
|
||||||
// scale video embeds based on zoom (capped) so that smaller embeds
|
// scale video embeds based on zoom (capped) so that smaller embeds
|
||||||
// on canvas when zoomed are still of legible quality
|
// on canvas when zoomed are still of legible quality
|
||||||
@@ -1755,13 +1994,14 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
})}
|
})}
|
||||||
style={{
|
style={{
|
||||||
transform: isVisible
|
transform: isVisible
|
||||||
? `translate(${x - this.state.offsetLeft}px, ${
|
? `translate(${x + renderPositionOffset.x * this.state.zoom.value - this.state.offsetLeft}px, ${
|
||||||
y - this.state.offsetTop
|
y + renderPositionOffset.y * this.state.zoom.value - this.state.offsetTop
|
||||||
}px) scale(${scale})`
|
}px) scale(${scale})`
|
||||||
: "none",
|
: "none",
|
||||||
display: isVisible ? "block" : "none",
|
display: isVisible ? "block" : "none",
|
||||||
opacity: getRenderOpacity(
|
opacity: getRenderOpacity(
|
||||||
el,
|
el,
|
||||||
|
this.getRenderOpacityConfig(),
|
||||||
getContainingFrame(el, this.scene.getNonDeletedElementsMap()),
|
getContainingFrame(el, this.scene.getNonDeletedElementsMap()),
|
||||||
this.elementsPendingErasure,
|
this.elementsPendingErasure,
|
||||||
null,
|
null,
|
||||||
@@ -2351,6 +2591,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
pendingFlowchartNodes:
|
pendingFlowchartNodes:
|
||||||
this.flowChartCreator.pendingNodes,
|
this.flowChartCreator.pendingNodes,
|
||||||
theme: this.state.theme,
|
theme: this.state.theme,
|
||||||
|
...this.getRenderOpacityConfig(),
|
||||||
|
renderAnimationVersion:
|
||||||
|
this.renderAnimationVersion,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{newElementCanvasElement && (
|
{newElementCanvasElement && (
|
||||||
@@ -2373,6 +2616,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.elementsPendingErasure,
|
this.elementsPendingErasure,
|
||||||
pendingFlowchartNodes: null,
|
pendingFlowchartNodes: null,
|
||||||
theme: this.state.theme,
|
theme: this.state.theme,
|
||||||
|
...this.getRenderOpacityConfig(),
|
||||||
|
renderAnimationVersion:
|
||||||
|
this.renderAnimationVersion,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@@ -3206,6 +3452,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.editorLifecycleEvents.emit("editor:unmount");
|
this.editorLifecycleEvents.emit("editor:unmount");
|
||||||
this.props.onUnmount?.();
|
this.props.onUnmount?.();
|
||||||
this.props.onExcalidrawAPI?.(null);
|
this.props.onExcalidrawAPI?.(null);
|
||||||
|
AnimationController.cancel(this.getElementAnimationKey());
|
||||||
|
|
||||||
(window as any).launchQueue?.setConsumer(() => {});
|
(window as any).launchQueue?.setConsumer(() => {});
|
||||||
|
|
||||||
@@ -4618,6 +4865,98 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public animateElements = ({
|
||||||
|
elements,
|
||||||
|
duration = 250,
|
||||||
|
delay = 0,
|
||||||
|
stagger = 0,
|
||||||
|
phase = "in",
|
||||||
|
easing,
|
||||||
|
...animation
|
||||||
|
}:
|
||||||
|
| {
|
||||||
|
elements: readonly (ExcalidrawElement | ExcalidrawElement["id"])[];
|
||||||
|
type: "fade";
|
||||||
|
duration?: number;
|
||||||
|
delay?: number;
|
||||||
|
stagger?: number;
|
||||||
|
phase?: "in" | "out";
|
||||||
|
easing?: "linear" | "easeOut" | "easeInOut";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
elements: readonly (ExcalidrawElement | ExcalidrawElement["id"])[];
|
||||||
|
type: "fly";
|
||||||
|
from: "left" | "right" | "top" | "bottom";
|
||||||
|
duration?: number;
|
||||||
|
delay?: number;
|
||||||
|
stagger?: number;
|
||||||
|
phase?: "in" | "out";
|
||||||
|
easing?: "linear" | "easeOut" | "easeInOut";
|
||||||
|
}) => {
|
||||||
|
const normalizedDelay = Math.max(delay, 0);
|
||||||
|
const normalizedStagger = Math.max(stagger, 0);
|
||||||
|
|
||||||
|
elements.forEach((elementOrId, index) => {
|
||||||
|
const id = typeof elementOrId === "string" ? elementOrId : elementOrId.id;
|
||||||
|
const element = this.scene.getNonDeletedElement(id);
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animation.type === "fade") {
|
||||||
|
this.animateElement({
|
||||||
|
id,
|
||||||
|
opacityFrom:
|
||||||
|
phase === "in" ? 0 : this.getElementVisibleOpacity(element),
|
||||||
|
opacityTo:
|
||||||
|
phase === "in" ? this.getElementVisibleOpacity(element) : 0,
|
||||||
|
easing: easing ?? "easeInOut",
|
||||||
|
duration,
|
||||||
|
delay: normalizedDelay + index * normalizedStagger,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const flyOffset = this.getFlyPositionOffset(element, animation.from);
|
||||||
|
|
||||||
|
this.animateElement({
|
||||||
|
id,
|
||||||
|
opacityFrom: phase === "in" ? 0 : this.getElementVisibleOpacity(element),
|
||||||
|
opacityTo: phase === "in" ? this.getElementVisibleOpacity(element) : 0,
|
||||||
|
positionFrom: phase === "in" ? flyOffset : { x: 0, y: 0 },
|
||||||
|
positionTo: phase === "in" ? { x: 0, y: 0 } : flyOffset,
|
||||||
|
easing: easing ?? "easeOut",
|
||||||
|
duration,
|
||||||
|
delay: normalizedDelay + index * normalizedStagger,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
public cancelElementAnimation = (id: string) => {
|
||||||
|
if (!this.elementAnimationStates.delete(id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.syncElementAnimations();
|
||||||
|
};
|
||||||
|
|
||||||
|
public clearElementAnimationOverrides = () => {
|
||||||
|
if (
|
||||||
|
this.elementOpacityOverrides.size === 0 &&
|
||||||
|
this.elementPositionOverrides.size === 0 &&
|
||||||
|
this.elementAnimationStates.size === 0
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.elementAnimationStates.clear();
|
||||||
|
this.elementOpacityOverrides.clear();
|
||||||
|
this.elementPositionOverrides.clear();
|
||||||
|
this.syncElementAnimations();
|
||||||
|
this.bumpRenderAnimationVersion();
|
||||||
|
};
|
||||||
|
|
||||||
public applyDeltas = (
|
public applyDeltas = (
|
||||||
deltas: StoreDelta[],
|
deltas: StoreDelta[],
|
||||||
options?: ApplyToOptions,
|
options?: ApplyToOptions,
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
|
|||||||
children,
|
children,
|
||||||
validateEmbeddable,
|
validateEmbeddable,
|
||||||
renderEmbeddable,
|
renderEmbeddable,
|
||||||
|
resolveRenderOpacity,
|
||||||
aiEnabled,
|
aiEnabled,
|
||||||
showDeprecatedFonts,
|
showDeprecatedFonts,
|
||||||
renderScrollbars,
|
renderScrollbars,
|
||||||
@@ -217,6 +218,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
|
|||||||
onDuplicate={onDuplicate}
|
onDuplicate={onDuplicate}
|
||||||
validateEmbeddable={validateEmbeddable}
|
validateEmbeddable={validateEmbeddable}
|
||||||
renderEmbeddable={renderEmbeddable}
|
renderEmbeddable={renderEmbeddable}
|
||||||
|
resolveRenderOpacity={resolveRenderOpacity}
|
||||||
aiEnabled={aiEnabled !== false}
|
aiEnabled={aiEnabled !== false}
|
||||||
showDeprecatedFonts={showDeprecatedFonts}
|
showDeprecatedFonts={showDeprecatedFonts}
|
||||||
renderScrollbars={renderScrollbars}
|
renderScrollbars={renderScrollbars}
|
||||||
|
|||||||
@@ -14,11 +14,16 @@ import {
|
|||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
import {
|
import {
|
||||||
elementOverlapsWithFrame,
|
elementOverlapsWithFrame,
|
||||||
|
getContainingFrame,
|
||||||
getTargetFrame,
|
getTargetFrame,
|
||||||
shouldApplyFrameClip,
|
shouldApplyFrameClip,
|
||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
import { renderElement } from "@excalidraw/element";
|
import {
|
||||||
|
getRenderElementWithPositionOverride,
|
||||||
|
getRenderOpacity,
|
||||||
|
renderElement,
|
||||||
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
import { getElementAbsoluteCoords } from "@excalidraw/element";
|
import { getElementAbsoluteCoords } from "@excalidraw/element";
|
||||||
|
|
||||||
@@ -170,7 +175,10 @@ const renderLinkIcon = (
|
|||||||
context: CanvasRenderingContext2D,
|
context: CanvasRenderingContext2D,
|
||||||
appState: StaticCanvasAppState,
|
appState: StaticCanvasAppState,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
|
renderConfig: StaticCanvasRenderConfig,
|
||||||
) => {
|
) => {
|
||||||
|
element = getRenderElementWithPositionOverride(element, renderConfig);
|
||||||
|
|
||||||
if (element.link && !appState.selectedElementIds[element.id]) {
|
if (element.link && !appState.selectedElementIds[element.id]) {
|
||||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
||||||
const [x, y, width, height] = getLinkHandleFromCoords(
|
const [x, y, width, height] = getLinkHandleFromCoords(
|
||||||
@@ -221,7 +229,13 @@ const renderLinkIcon = (
|
|||||||
|
|
||||||
linkCanvasCacheContext.restore();
|
linkCanvasCacheContext.restore();
|
||||||
}
|
}
|
||||||
context.globalAlpha = element.opacity / 100;
|
context.globalAlpha = getRenderOpacity(
|
||||||
|
element,
|
||||||
|
renderConfig,
|
||||||
|
getContainingFrame(element, elementsMap),
|
||||||
|
renderConfig.elementsPendingErasure,
|
||||||
|
renderConfig.pendingFlowchartNodes,
|
||||||
|
);
|
||||||
context.drawImage(linkCanvas, x - centerX, y - centerY, width, height);
|
context.drawImage(linkCanvas, x - centerX, y - centerY, width, height);
|
||||||
context.restore();
|
context.restore();
|
||||||
}
|
}
|
||||||
@@ -370,7 +384,7 @@ const _renderStaticScene = ({
|
|||||||
context.restore();
|
context.restore();
|
||||||
|
|
||||||
if (!isExporting) {
|
if (!isExporting) {
|
||||||
renderLinkIcon(element, context, appState, elementsMap);
|
renderLinkIcon(element, context, appState, elementsMap, renderConfig);
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -421,7 +435,7 @@ const _renderStaticScene = ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (!isExporting) {
|
if (!isExporting) {
|
||||||
renderLinkIcon(element, context, appState, elementsMap);
|
renderLinkIcon(element, context, appState, elementsMap, renderConfig);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// - when exporting the whole canvas, we DO NOT apply clipping
|
// - when exporting the whole canvas, we DO NOT apply clipping
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import type {
|
|||||||
AppClassProperties,
|
AppClassProperties,
|
||||||
AppState,
|
AppState,
|
||||||
EmbedsValidationStatus,
|
EmbedsValidationStatus,
|
||||||
|
RenderOpacityResolver,
|
||||||
ElementsPendingErasure,
|
ElementsPendingErasure,
|
||||||
InteractiveCanvasAppState,
|
InteractiveCanvasAppState,
|
||||||
StaticCanvasAppState,
|
StaticCanvasAppState,
|
||||||
@@ -37,6 +38,13 @@ export type StaticCanvasRenderConfig = {
|
|||||||
elementsPendingErasure: ElementsPendingErasure;
|
elementsPendingErasure: ElementsPendingErasure;
|
||||||
pendingFlowchartNodes: PendingExcalidrawElements | null;
|
pendingFlowchartNodes: PendingExcalidrawElements | null;
|
||||||
theme: AppState["theme"];
|
theme: AppState["theme"];
|
||||||
|
resolveRenderOpacity?: RenderOpacityResolver;
|
||||||
|
elementOpacityOverrides?: ReadonlyMap<ExcalidrawElement["id"], number>;
|
||||||
|
elementPositionOverrides?: ReadonlyMap<
|
||||||
|
ExcalidrawElement["id"],
|
||||||
|
{ x: number; y: number }
|
||||||
|
>;
|
||||||
|
renderAnimationVersion?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SVGRenderConfig = {
|
export type SVGRenderConfig = {
|
||||||
|
|||||||
@@ -567,6 +567,10 @@ export type OnExportProgress = {
|
|||||||
progress?: number;
|
progress?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RenderOpacityResolver = (
|
||||||
|
element: NonDeletedExcalidrawElement,
|
||||||
|
) => ExcalidrawElement["opacity"] | undefined;
|
||||||
|
|
||||||
export interface ExcalidrawProps {
|
export interface ExcalidrawProps {
|
||||||
onChange?: (
|
onChange?: (
|
||||||
elements: readonly OrderedExcalidrawElement[],
|
elements: readonly OrderedExcalidrawElement[],
|
||||||
@@ -682,6 +686,7 @@ export interface ExcalidrawProps {
|
|||||||
element: NonDeleted<ExcalidrawEmbeddableElement>,
|
element: NonDeleted<ExcalidrawEmbeddableElement>,
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
) => JSX.Element | null;
|
) => JSX.Element | null;
|
||||||
|
resolveRenderOpacity?: RenderOpacityResolver;
|
||||||
aiEnabled?: boolean;
|
aiEnabled?: boolean;
|
||||||
showDeprecatedFonts?: boolean;
|
showDeprecatedFonts?: boolean;
|
||||||
renderScrollbars?: boolean;
|
renderScrollbars?: boolean;
|
||||||
@@ -962,6 +967,11 @@ export interface ExcalidrawImperativeAPI {
|
|||||||
getFiles: () => InstanceType<typeof App>["files"];
|
getFiles: () => InstanceType<typeof App>["files"];
|
||||||
getName: InstanceType<typeof App>["getName"];
|
getName: InstanceType<typeof App>["getName"];
|
||||||
scrollToContent: InstanceType<typeof App>["scrollToContent"];
|
scrollToContent: InstanceType<typeof App>["scrollToContent"];
|
||||||
|
animateElements: InstanceType<typeof App>["animateElements"];
|
||||||
|
cancelElementAnimation: InstanceType<typeof App>["cancelElementAnimation"];
|
||||||
|
clearElementAnimationOverrides: InstanceType<
|
||||||
|
typeof App
|
||||||
|
>["clearElementAnimationOverrides"];
|
||||||
registerAction: (action: Action) => void;
|
registerAction: (action: Action) => void;
|
||||||
refresh: InstanceType<typeof App>["refresh"];
|
refresh: InstanceType<typeof App>["refresh"];
|
||||||
setToast: InstanceType<typeof App>["setToast"];
|
setToast: InstanceType<typeof App>["setToast"];
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ export const getEllipseShape = <Point extends GlobalPoint | LocalPoint>(
|
|||||||
|
|
||||||
export const getCurvePathOps = (shape: Drawable): Op[] => {
|
export const getCurvePathOps = (shape: Drawable): Op[] => {
|
||||||
// NOTE (mtolmacs): Temporary fix for extremely large elements
|
// NOTE (mtolmacs): Temporary fix for extremely large elements
|
||||||
if (!shape || shape.sets.length === 0) {
|
if (!shape) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,29 +316,26 @@ export const getClosedCurveShape = <Point extends GlobalPoint | LocalPoint>(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prefer the fillPath set
|
const ops = getCurvePathOps(roughShape);
|
||||||
const fillPathSet = roughShape.sets.find((s) => s.type === "fillPath");
|
|
||||||
const ops = fillPathSet ? fillPathSet.ops : getCurvePathOps(roughShape);
|
|
||||||
|
|
||||||
const points: Point[] = [];
|
const points: Point[] = [];
|
||||||
let odd = false;
|
let odd = false;
|
||||||
for (const operation of ops) {
|
for (const operation of ops) {
|
||||||
if (operation.op === "move") {
|
if (operation.op === "move") {
|
||||||
if (fillPathSet) {
|
odd = !odd;
|
||||||
// fillPath is always a single run, no odd/even skipping needed
|
if (odd) {
|
||||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||||
} else {
|
|
||||||
odd = !odd;
|
|
||||||
if (odd) {
|
|
||||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (operation.op === "bcurveTo") {
|
} else if (operation.op === "bcurveTo") {
|
||||||
if (fillPathSet || odd) {
|
if (odd) {
|
||||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||||
points.push(pointFrom(operation.data[2], operation.data[3]));
|
points.push(pointFrom(operation.data[2], operation.data[3]));
|
||||||
points.push(pointFrom(operation.data[4], operation.data[5]));
|
points.push(pointFrom(operation.data[4], operation.data[5]));
|
||||||
}
|
}
|
||||||
|
} else if (operation.op === "lineTo") {
|
||||||
|
if (odd) {
|
||||||
|
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user