[WIP] Initial implem of fade animating elements.

This commit is contained in:
barnabasmolnar
2026-06-02 19:06:32 +02:00
parent c08be69618
commit f472af04a9
9 changed files with 327 additions and 23 deletions
@@ -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,8 @@ export default function ExampleApp({
{}, {},
); );
const [comment, setComment] = useState<Comment | null>(null); const [comment, setComment] = useState<Comment | null>(null);
const [hideAllForFadeDemo, setHideAllForFadeDemo] = useState(showFadeDemo);
const [fadeDemoNextIndex, setFadeDemoNextIndex] = useState(0);
const initialStatePromiseRef = useRef<{ const initialStatePromiseRef = useRef<{
promise: ResolvablePromise<ExcalidrawInitialDataState | null>; promise: ResolvablePromise<ExcalidrawInitialDataState | null>;
@@ -178,7 +182,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 +213,7 @@ export default function ExampleApp({
onPointerDown, onPointerDown,
onScrollChange: rerenderCommentIcons, onScrollChange: rerenderCommentIcons,
validateEmbeddable: true, validateEmbeddable: true,
resolveRenderOpacity: hideAllForFadeDemo ? () => 0 : undefined,
}, },
<> <>
{excalidrawAPI && ( {excalidrawAPI && (
@@ -664,6 +670,58 @@ export default function ExampleApp({
> >
Reset Scene Reset Scene
</button> </button>
{showFadeDemo && (
<>
<button
onClick={() => {
if (!excalidrawAPI) {
return;
}
const elements = excalidrawAPI.getSceneElements();
if (!elements.length) {
return;
}
setHideAllForFadeDemo(true);
const nextIndex =
fadeDemoNextIndex >= elements.length
? 0
: fadeDemoNextIndex;
if (nextIndex === 0) {
excalidrawAPI.clearElementOpacityOverrides();
}
console.info("Fading in element", {
element: elements[nextIndex],
});
excalidrawAPI.fadeElement({
id: elements[nextIndex].id,
from: 0,
to: 100,
duration: 500,
});
setFadeDemoNextIndex(nextIndex + 1);
}}
>
Fade in next element
</button>
<button
onClick={() => {
excalidrawAPI?.clearElementOpacityOverrides();
setHideAllForFadeDemo(true);
setFadeDemoNextIndex(0);
}}
>
Reset fade demo
</button>
</>
)}
<button <button
onClick={() => { onClick={() => {
const libraryItems: LibraryItems = [ const libraryItems: LibraryItems = [
+35 -13
View File
@@ -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,
+32 -1
View File
@@ -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,36 @@ 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 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 +144,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
@@ -793,6 +823,7 @@ export const renderElement = (
context.globalAlpha = getRenderOpacity( context.globalAlpha = getRenderOpacity(
element, element,
renderConfig,
getContainingFrame(element, elementsMap), getContainingFrame(element, elementsMap),
renderConfig.elementsPendingErasure, renderConfig.elementsPendingErasure,
renderConfig.pendingFlowchartNodes, renderConfig.pendingFlowchartNodes,
+171 -4
View File
@@ -207,10 +207,11 @@ import {
getLineHeightInPx, getLineHeightInPx,
getApproxMinLineWidth, getApproxMinLineWidth,
getApproxMinLineHeight, getApproxMinLineHeight,
getMinTextElementWidth, getMinTextElementWidth,
ShapeCache, ShapeCache,
getRenderOpacity, getRenderOpacity,
editGroupForSelectedElement, resolveRenderOpacity,
editGroupForSelectedElement,
getElementsInGroup, getElementsInGroup,
getSelectedGroupIdForElement, getSelectedGroupIdForElement,
getSelectedGroupIds, getSelectedGroupIds,
@@ -450,6 +451,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 +742,90 @@ class App extends React.Component<AppProps, AppState> {
onRemoveEventListenersEmitter = new Emitter<[]>(); onRemoveEventListenersEmitter = new Emitter<[]>();
api: ExcalidrawImperativeAPI; api: ExcalidrawImperativeAPI;
private renderOpacityVersion = 0;
private elementOpacityOverrides = new Map<string, number>();
private elementFadeStates = new Map<
string,
{
from: number;
to: number;
duration: number;
delay: number;
elapsed: number;
}
>();
private getElementFadeAnimationKey = () => `${this.id}:fade-element`;
private bumpRenderOpacityVersion = () => {
this.renderOpacityVersion++;
this.setState({});
};
private getRenderOpacityConfig = () => ({
elementOpacityOverrides: this.elementOpacityOverrides,
resolveRenderOpacity: this.props.resolveRenderOpacity,
});
private getResolvedElementOpacity = (element: NonDeletedExcalidrawElement) => {
return resolveRenderOpacity(element, this.getRenderOpacityConfig());
};
private syncFadeElementAnimations = () => {
const animationKey = this.getElementFadeAnimationKey();
if (this.elementFadeStates.size === 0) {
AnimationController.cancel(animationKey);
return;
}
if (AnimationController.running(animationKey)) {
return;
}
AnimationController.start(animationKey, ({ deltaTime }) => {
let shouldRerender = false;
for (const [id, fade] of this.elementFadeStates) {
const element = this.scene.getNonDeletedElement(id);
if (!element) {
this.elementFadeStates.delete(id);
shouldRerender =
this.elementOpacityOverrides.delete(id) || shouldRerender;
continue;
}
fade.elapsed += deltaTime;
const nextOpacity =
fade.elapsed <= fade.delay
? fade.from
: fade.duration === 0
? fade.to
: fade.from +
(fade.to - fade.from) *
Math.min((fade.elapsed - fade.delay) / fade.duration, 1);
const clampedOpacity = clamp(nextOpacity, 0, 100);
if (this.elementOpacityOverrides.get(id) !== clampedOpacity) {
this.elementOpacityOverrides.set(id, clampedOpacity);
shouldRerender = true;
}
if (fade.elapsed >= fade.delay + fade.duration) {
this.elementFadeStates.delete(id);
}
}
if (shouldRerender) {
this.bumpRenderOpacityVersion();
}
return this.elementFadeStates.size > 0 ? {} : undefined;
});
};
private createExcalidrawAPI(): ExcalidrawImperativeAPI { private createExcalidrawAPI(): ExcalidrawImperativeAPI {
const api: ExcalidrawImperativeAPI = { const api: ExcalidrawImperativeAPI = {
@@ -757,6 +843,9 @@ class App extends React.Component<AppProps, AppState> {
clear: this.resetHistory, clear: this.resetHistory,
}, },
scrollToContent: this.scrollToContent, scrollToContent: this.scrollToContent,
fadeElement: this.fadeElement,
cancelFadeElement: this.cancelFadeElement,
clearElementOpacityOverrides: this.clearElementOpacityOverrides,
getSceneElements: this.getSceneElements, getSceneElements: this.getSceneElements,
getAppState: () => this.state, getAppState: () => this.state,
getFiles: () => this.files, getFiles: () => this.files,
@@ -1762,6 +1851,7 @@ class App extends React.Component<AppProps, AppState> {
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 +2441,8 @@ 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(),
renderOpacityVersion: this.renderOpacityVersion,
}} }}
/> />
{newElementCanvasElement && ( {newElementCanvasElement && (
@@ -2373,6 +2465,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(),
renderOpacityVersion:
this.renderOpacityVersion,
}} }}
/> />
)} )}
@@ -3206,6 +3301,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.getElementFadeAnimationKey());
(window as any).launchQueue?.setConsumer(() => {}); (window as any).launchQueue?.setConsumer(() => {});
@@ -4618,6 +4714,77 @@ class App extends React.Component<AppProps, AppState> {
}, },
); );
public fadeElement = ({
id,
from,
to = 100,
duration = 250,
delay = 0,
}: {
id: string;
from?: number;
to?: number;
duration?: number;
delay?: number;
}) => {
const element = this.scene.getNonDeletedElement(id);
if (!element) {
return;
}
const startOpacity = clamp(
from ?? this.getResolvedElementOpacity(element),
0,
100,
);
const targetOpacity = clamp(to, 0, 100);
const normalizedDuration = Math.max(duration, 0);
const normalizedDelay = Math.max(delay, 0);
this.elementOpacityOverrides.set(id, startOpacity);
if (normalizedDuration === 0 && normalizedDelay === 0) {
this.elementFadeStates.delete(id);
this.elementOpacityOverrides.set(id, targetOpacity);
this.bumpRenderOpacityVersion();
return;
}
this.elementFadeStates.set(id, {
from: startOpacity,
to: targetOpacity,
duration: normalizedDuration,
delay: normalizedDelay,
elapsed: 0,
});
this.bumpRenderOpacityVersion();
this.syncFadeElementAnimations();
};
public cancelFadeElement = (id: string) => {
if (!this.elementFadeStates.delete(id)) {
return;
}
this.syncFadeElementAnimations();
};
public clearElementOpacityOverrides = () => {
if (
this.elementOpacityOverrides.size === 0 &&
this.elementFadeStates.size === 0
) {
return;
}
this.elementFadeStates.clear();
this.elementOpacityOverrides.clear();
this.syncFadeElementAnimations();
this.bumpRenderOpacityVersion();
};
public applyDeltas = ( public applyDeltas = (
deltas: StoreDelta[], deltas: StoreDelta[],
options?: ApplyToOptions, options?: ApplyToOptions,
+2
View File
@@ -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}
+12 -4
View File
@@ -14,11 +14,12 @@ 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 { getRenderOpacity, renderElement } from "@excalidraw/element";
import { getElementAbsoluteCoords } from "@excalidraw/element"; import { getElementAbsoluteCoords } from "@excalidraw/element";
@@ -170,6 +171,7 @@ const renderLinkIcon = (
context: CanvasRenderingContext2D, context: CanvasRenderingContext2D,
appState: StaticCanvasAppState, appState: StaticCanvasAppState,
elementsMap: ElementsMap, elementsMap: ElementsMap,
renderConfig: StaticCanvasRenderConfig,
) => { ) => {
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);
@@ -221,7 +223,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 +378,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 +429,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
+4
View File
@@ -12,6 +12,7 @@ import type {
AppClassProperties, AppClassProperties,
AppState, AppState,
EmbedsValidationStatus, EmbedsValidationStatus,
RenderOpacityResolver,
ElementsPendingErasure, ElementsPendingErasure,
InteractiveCanvasAppState, InteractiveCanvasAppState,
StaticCanvasAppState, StaticCanvasAppState,
@@ -37,6 +38,9 @@ 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>;
renderOpacityVersion?: number;
}; };
export type SVGRenderConfig = { export type SVGRenderConfig = {
+10
View File
@@ -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"];
fadeElement: InstanceType<typeof App>["fadeElement"];
cancelFadeElement: InstanceType<typeof App>["cancelFadeElement"];
clearElementOpacityOverrides: InstanceType<
typeof App
>["clearElementOpacityOverrides"];
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"];