mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
feat: improved freedraw (#3512)
Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
parent
198800136e
commit
49c6bdd520
66 changed files with 786 additions and 247 deletions
|
@ -9,7 +9,8 @@ import {
|
|||
canHaveArrowheads,
|
||||
getTargetElements,
|
||||
hasBackground,
|
||||
hasStroke,
|
||||
hasStrokeStyle,
|
||||
hasStrokeWidth,
|
||||
hasText,
|
||||
} from "../scene";
|
||||
import { SHAPES } from "../shapes";
|
||||
|
@ -53,10 +54,17 @@ export const SelectedShapeActions = ({
|
|||
{showChangeBackgroundIcons && renderAction("changeBackgroundColor")}
|
||||
{showFillIcons && renderAction("changeFillStyle")}
|
||||
|
||||
{(hasStroke(elementType) ||
|
||||
targetElements.some((element) => hasStroke(element.type))) && (
|
||||
{(hasStrokeWidth(elementType) ||
|
||||
targetElements.some((element) => hasStrokeWidth(element.type))) &&
|
||||
renderAction("changeStrokeWidth")}
|
||||
|
||||
{(elementType === "freedraw" ||
|
||||
targetElements.some((element) => element.type === "freedraw")) &&
|
||||
renderAction("changeStrokeShape")}
|
||||
|
||||
{(hasStrokeStyle(elementType) ||
|
||||
targetElements.some((element) => hasStrokeStyle(element.type))) && (
|
||||
<>
|
||||
{renderAction("changeStrokeWidth")}
|
||||
{renderAction("changeStrokeStyle")}
|
||||
{renderAction("changeSloppiness")}
|
||||
</>
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { Point, simplify } from "points-on-curve";
|
||||
import React, { useContext } from "react";
|
||||
import { RoughCanvas } from "roughjs/bin/canvas";
|
||||
import rough from "roughjs/bin/rough";
|
||||
|
@ -70,7 +69,7 @@ import {
|
|||
import { loadFromBlob } from "../data";
|
||||
import { isValidLibrary } from "../data/json";
|
||||
import Library from "../data/library";
|
||||
import { restore } from "../data/restore";
|
||||
import { restore, restoreElements } from "../data/restore";
|
||||
import {
|
||||
dragNewElement,
|
||||
dragSelectedElements,
|
||||
|
@ -111,7 +110,7 @@ import {
|
|||
} from "../element/binding";
|
||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||
import { mutateElement } from "../element/mutateElement";
|
||||
import { deepCopyElement } from "../element/newElement";
|
||||
import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
|
||||
import { MaybeTransformHandleType } from "../element/transformHandles";
|
||||
import {
|
||||
isBindingElement,
|
||||
|
@ -122,6 +121,7 @@ import {
|
|||
import {
|
||||
ExcalidrawBindableElement,
|
||||
ExcalidrawElement,
|
||||
ExcalidrawFreeDrawElement,
|
||||
ExcalidrawGenericElement,
|
||||
ExcalidrawLinearElement,
|
||||
ExcalidrawTextElement,
|
||||
|
@ -1266,7 +1266,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
});
|
||||
} else if (data.elements) {
|
||||
this.addElementsFromPasteOrLibrary({
|
||||
elements: data.elements,
|
||||
elements: restoreElements(data.elements),
|
||||
position: "cursor",
|
||||
});
|
||||
} else if (data.text) {
|
||||
|
@ -2341,7 +2341,6 @@ class App extends React.Component<AppProps, AppState> {
|
|||
return;
|
||||
} else if (
|
||||
this.state.elementType === "arrow" ||
|
||||
this.state.elementType === "draw" ||
|
||||
this.state.elementType === "line"
|
||||
) {
|
||||
this.handleLinearElementOnPointerDown(
|
||||
|
@ -2349,6 +2348,12 @@ class App extends React.Component<AppProps, AppState> {
|
|||
this.state.elementType,
|
||||
pointerDownState,
|
||||
);
|
||||
} else if (this.state.elementType === "freedraw") {
|
||||
this.handleFreeDrawElementOnPointerDown(
|
||||
event,
|
||||
this.state.elementType,
|
||||
pointerDownState,
|
||||
);
|
||||
} else {
|
||||
this.createGenericElementOnPointerDown(
|
||||
this.state.elementType,
|
||||
|
@ -2845,6 +2850,65 @@ class App extends React.Component<AppProps, AppState> {
|
|||
}
|
||||
};
|
||||
|
||||
private handleFreeDrawElementOnPointerDown = (
|
||||
event: React.PointerEvent<HTMLCanvasElement>,
|
||||
elementType: ExcalidrawFreeDrawElement["type"],
|
||||
pointerDownState: PointerDownState,
|
||||
) => {
|
||||
// Begin a mark capture. This does not have to update state yet.
|
||||
const [gridX, gridY] = getGridPoint(
|
||||
pointerDownState.origin.x,
|
||||
pointerDownState.origin.y,
|
||||
null,
|
||||
);
|
||||
|
||||
const element = newFreeDrawElement({
|
||||
type: elementType,
|
||||
x: gridX,
|
||||
y: gridY,
|
||||
strokeColor: this.state.currentItemStrokeColor,
|
||||
backgroundColor: this.state.currentItemBackgroundColor,
|
||||
fillStyle: this.state.currentItemFillStyle,
|
||||
strokeWidth: this.state.currentItemStrokeWidth,
|
||||
strokeStyle: this.state.currentItemStrokeStyle,
|
||||
roughness: this.state.currentItemRoughness,
|
||||
opacity: this.state.currentItemOpacity,
|
||||
strokeSharpness: this.state.currentItemLinearStrokeSharpness,
|
||||
simulatePressure: event.pressure === 0.5,
|
||||
});
|
||||
|
||||
this.setState((prevState) => ({
|
||||
selectedElementIds: {
|
||||
...prevState.selectedElementIds,
|
||||
[element.id]: false,
|
||||
},
|
||||
}));
|
||||
|
||||
const pressures = element.simulatePressure
|
||||
? element.pressures
|
||||
: [...element.pressures, event.pressure];
|
||||
|
||||
mutateElement(element, {
|
||||
points: [[0, 0]],
|
||||
pressures,
|
||||
});
|
||||
|
||||
const boundElement = getHoveredElementForBinding(
|
||||
pointerDownState.origin,
|
||||
this.scene,
|
||||
);
|
||||
this.scene.replaceAllElements([
|
||||
...this.scene.getElementsIncludingDeleted(),
|
||||
element,
|
||||
]);
|
||||
this.setState({
|
||||
draggingElement: element,
|
||||
editingElement: element,
|
||||
startBoundElement: boundElement,
|
||||
suggestedBindings: [],
|
||||
});
|
||||
};
|
||||
|
||||
private handleLinearElementOnPointerDown = (
|
||||
event: React.PointerEvent<HTMLCanvasElement>,
|
||||
elementType: ExcalidrawLinearElement["type"],
|
||||
|
@ -2899,7 +2963,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
const [gridX, gridY] = getGridPoint(
|
||||
pointerDownState.origin.x,
|
||||
pointerDownState.origin.y,
|
||||
elementType === "draw" ? null : this.state.gridSize,
|
||||
this.state.gridSize,
|
||||
);
|
||||
|
||||
/* If arrow is pre-arrowheads, it will have undefined for both start and end arrowheads.
|
||||
|
@ -3107,6 +3171,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
const hasHitASelectedElement = pointerDownState.hit.allHitElements.some(
|
||||
(element) => this.isASelectedElement(element),
|
||||
);
|
||||
|
||||
if (
|
||||
hasHitASelectedElement ||
|
||||
pointerDownState.hit.hasHitCommonBoundingBoxOfSelectedElements
|
||||
|
@ -3207,18 +3272,24 @@ class App extends React.Component<AppProps, AppState> {
|
|||
return;
|
||||
}
|
||||
|
||||
if (isLinearElement(draggingElement)) {
|
||||
if (draggingElement.type === "freedraw") {
|
||||
const points = draggingElement.points;
|
||||
const dx = pointerCoords.x - draggingElement.x;
|
||||
const dy = pointerCoords.y - draggingElement.y;
|
||||
|
||||
const pressures = draggingElement.simulatePressure
|
||||
? draggingElement.pressures
|
||||
: [...draggingElement.pressures, event.pressure];
|
||||
|
||||
mutateElement(draggingElement, {
|
||||
points: [...points, [dx, dy]],
|
||||
pressures,
|
||||
});
|
||||
} else if (isLinearElement(draggingElement)) {
|
||||
pointerDownState.drag.hasOccurred = true;
|
||||
const points = draggingElement.points;
|
||||
let dx: number;
|
||||
let dy: number;
|
||||
if (draggingElement.type === "draw") {
|
||||
dx = pointerCoords.x - draggingElement.x;
|
||||
dy = pointerCoords.y - draggingElement.y;
|
||||
} else {
|
||||
dx = gridX - draggingElement.x;
|
||||
dy = gridY - draggingElement.y;
|
||||
}
|
||||
let dx = gridX - draggingElement.x;
|
||||
let dy = gridY - draggingElement.y;
|
||||
|
||||
if (getRotateWithDiscreteAngleKey(event) && points.length === 2) {
|
||||
({ width: dx, height: dy } = getPerfectElementSize(
|
||||
|
@ -3231,19 +3302,11 @@ class App extends React.Component<AppProps, AppState> {
|
|||
if (points.length === 1) {
|
||||
mutateElement(draggingElement, { points: [...points, [dx, dy]] });
|
||||
} else if (points.length > 1) {
|
||||
if (draggingElement.type === "draw") {
|
||||
mutateElement(draggingElement, {
|
||||
points: simplify(
|
||||
[...(points as Point[]), [dx, dy]],
|
||||
0.7 / this.state.zoom.value,
|
||||
),
|
||||
});
|
||||
} else {
|
||||
mutateElement(draggingElement, {
|
||||
points: [...points.slice(0, -1), [dx, dy]],
|
||||
});
|
||||
}
|
||||
mutateElement(draggingElement, {
|
||||
points: [...points.slice(0, -1), [dx, dy]],
|
||||
});
|
||||
}
|
||||
|
||||
if (isBindingElement(draggingElement)) {
|
||||
// When creating a linear element by dragging
|
||||
this.maybeSuggestBindingForLinearElementAtCursor(
|
||||
|
@ -3383,8 +3446,33 @@ class App extends React.Component<AppProps, AppState> {
|
|||
pointerDownState.eventListeners.onKeyUp!,
|
||||
);
|
||||
|
||||
if (draggingElement?.type === "draw") {
|
||||
if (draggingElement?.type === "freedraw") {
|
||||
const pointerCoords = viewportCoordsToSceneCoords(
|
||||
childEvent,
|
||||
this.state,
|
||||
);
|
||||
|
||||
const points = draggingElement.points;
|
||||
let dx = pointerCoords.x - draggingElement.x;
|
||||
let dy = pointerCoords.y - draggingElement.y;
|
||||
|
||||
// Allows dots to avoid being flagged as infinitely small
|
||||
if (dx === points[0][0] && dy === points[0][1]) {
|
||||
dy += 0.0001;
|
||||
dx += 0.0001;
|
||||
}
|
||||
|
||||
const pressures = draggingElement.simulatePressure
|
||||
? []
|
||||
: [...draggingElement.pressures, childEvent.pressure];
|
||||
|
||||
mutateElement(draggingElement, {
|
||||
points: [...points, [dx, dy]],
|
||||
pressures,
|
||||
});
|
||||
|
||||
this.actionManager.executeAction(actionFinalize);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -3428,7 +3516,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
);
|
||||
}
|
||||
this.setState({ suggestedBindings: [], startBoundElement: null });
|
||||
if (!elementLocked && elementType !== "draw") {
|
||||
if (!elementLocked) {
|
||||
resetCursor(this.canvas);
|
||||
this.setState((prevState) => ({
|
||||
draggingElement: null,
|
||||
|
@ -3575,7 +3663,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!elementLocked && elementType !== "draw" && draggingElement) {
|
||||
if (!elementLocked && elementType !== "freedraw" && draggingElement) {
|
||||
this.setState((prevState) => ({
|
||||
selectedElementIds: {
|
||||
...prevState.selectedElementIds,
|
||||
|
@ -3599,7 +3687,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||
);
|
||||
}
|
||||
|
||||
if (!elementLocked && elementType !== "draw") {
|
||||
if (!elementLocked && elementType !== "freedraw") {
|
||||
resetCursor(this.canvas);
|
||||
this.setState({
|
||||
draggingElement: null,
|
||||
|
|
|
@ -153,7 +153,7 @@ export const HelpDialog = ({ onClose }: { onClose?: () => void }) => {
|
|||
<Shortcut label={t("toolBar.arrow")} shortcuts={["A", "5"]} />
|
||||
<Shortcut label={t("toolBar.line")} shortcuts={["P", "6"]} />
|
||||
<Shortcut
|
||||
label={t("toolBar.draw")}
|
||||
label={t("toolBar.freedraw")}
|
||||
shortcuts={["Shift+P", "7"]}
|
||||
/>
|
||||
<Shortcut label={t("toolBar.text")} shortcuts={["T", "8"]} />
|
||||
|
|
|
@ -23,7 +23,7 @@ const getHints = ({ appState, elements }: Hint) => {
|
|||
return t("hints.linearElementMulti");
|
||||
}
|
||||
|
||||
if (elementType === "draw") {
|
||||
if (elementType === "freedraw") {
|
||||
return t("hints.freeDraw");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue