differentiate between constant/variable stroke type

This commit is contained in:
dwelle
2025-06-27 14:18:48 +02:00
parent 3c07ff358a
commit 0199c82e98
3 changed files with 64 additions and 22 deletions

View File

@ -3,10 +3,10 @@ import { useState, useEffect } from "react";
export const FreedrawDebugSliders = () => { export const FreedrawDebugSliders = () => {
const [streamline, setStreamline] = useState<number>( const [streamline, setStreamline] = useState<number>(
DRAWING_CONFIGS.default.streamline, DRAWING_CONFIGS.default.variable.streamline,
); );
const [simplify, setSimplify] = useState<number>( const [simplify, setSimplify] = useState<number>(
DRAWING_CONFIGS.default.simplify, DRAWING_CONFIGS.default.variable.simplify,
); );
useEffect(() => { useEffect(() => {
@ -14,7 +14,7 @@ export const FreedrawDebugSliders = () => {
window.h = {} as any; window.h = {} as any;
} }
if (!window.h.debugFreedraw) { if (!window.h.debugFreedraw) {
window.h.debugFreedraw = DRAWING_CONFIGS.default; window.h.debugFreedraw = DRAWING_CONFIGS.default.variable;
} }
setStreamline(window.h.debugFreedraw.streamline); setStreamline(window.h.debugFreedraw.streamline);
@ -39,7 +39,7 @@ export const FreedrawDebugSliders = () => {
<div <div
style={{ style={{
position: "absolute", position: "absolute",
bottom: "10px", bottom: "70px",
left: "50%", left: "50%",
transform: "translateX(-50%)", transform: "translateX(-50%)",
zIndex: 9999, zIndex: 9999,

View File

@ -8,34 +8,70 @@ import type { StrokeOptions } from "perfect-freehand";
import type { ExcalidrawFreeDrawElement, PointerType } from "./types"; import type { ExcalidrawFreeDrawElement, PointerType } from "./types";
type FreedrawStrokeType = "constant" | "variable";
export const getElementStrokeType = (
element: ExcalidrawFreeDrawElement,
): FreedrawStrokeType => {
return element.drawingConfigs?.fixedStrokeWidth ? "constant" : "variable";
};
export const DRAWING_CONFIGS: Record< export const DRAWING_CONFIGS: Record<
PointerType | "default", PointerType | "default",
{ streamline: number; simplify: number } Record<FreedrawStrokeType, { streamline: number; simplify: number }>
> = { > = {
default: { default: {
streamline: 0.35, constant: {
simplify: 0.1, streamline: 0.35,
simplify: 0.1,
},
variable: {
streamline: 0.35,
simplify: 0.1,
},
}, },
mouse: { mouse: {
streamline: 0.6, constant: {
simplify: 0.1, streamline: 0.6,
simplify: 0.1,
},
variable: {
streamline: 0.6,
simplify: 0.1,
},
}, },
pen: { pen: {
// for optimal performance, we use a lower streamline and simplify constant: {
streamline: 0.2, // for optimal performance, we use a lower streamline and simplify
simplify: 0.1, streamline: 0.2,
simplify: 0.1,
},
variable: {
// for optimal performance, we use a lower streamline and simplify
streamline: 0.2,
simplify: 0.1,
},
}, },
touch: { touch: {
streamline: 0.65, constant: {
simplify: 0.1, streamline: 0.65,
simplify: 0.1,
},
variable: {
streamline: 0.65,
simplify: 0.1,
},
}, },
} as const; } as const;
export const getFreedrawConfig = (eventType: string | null | undefined) => { export const getFreedrawConfig = (
return ( eventType: string | null | undefined,
strokeType: FreedrawStrokeType,
): { streamline: number; simplify: number } => {
const inputConfig =
DRAWING_CONFIGS[(eventType as PointerType | null) || "default"] || DRAWING_CONFIGS[(eventType as PointerType | null) || "default"] ||
DRAWING_CONFIGS.default DRAWING_CONFIGS.default;
); return inputConfig[strokeType];
}; };
/** /**
@ -105,9 +141,11 @@ export const getFreedrawStroke = (element: ExcalidrawFreeDrawElement) => {
} }
const streamline = const streamline =
element.drawingConfigs?.streamline ?? DRAWING_CONFIGS.default.streamline; element.drawingConfigs?.streamline ??
getFreedrawConfig("default", getElementStrokeType(element)).streamline;
const simplify = const simplify =
element.drawingConfigs?.simplify ?? DRAWING_CONFIGS.default.simplify; element.drawingConfigs?.simplify ??
getFreedrawConfig("default", getElementStrokeType(element)).simplify;
const laser = new LaserPointer({ const laser = new LaserPointer({
size: element.strokeWidth, size: element.strokeWidth,

View File

@ -7476,7 +7476,10 @@ class App extends React.Component<AppProps, AppState> {
const simulatePressure = event.pressure === 0.5 || event.pressure === 0; const simulatePressure = event.pressure === 0.5 || event.pressure === 0;
const freedrawConfig = getFreedrawConfig(event.pointerType); const freedrawConfig = getFreedrawConfig(
event.pointerType,
this.state.currentItemFixedStrokeWidth ? "constant" : "variable",
);
const element = newFreeDrawElement({ const element = newFreeDrawElement({
type: elementType, type: elementType,
@ -11159,7 +11162,8 @@ export const createTestHook = () => {
window.h = window.h || ({} as Window["h"]); window.h = window.h || ({} as Window["h"]);
// Initialize debug freedraw parameters // Initialize debug freedraw parameters
window.h.debugFreedraw = window.h.debugFreedraw || DRAWING_CONFIGS.default; window.h.debugFreedraw =
window.h.debugFreedraw || DRAWING_CONFIGS.default.variable;
Object.defineProperties(window.h, { Object.defineProperties(window.h, {
elements: { elements: {