Skip to content

Commit

Permalink
Merge pull request #68 from low-earth-orbit/eraser-fix
Browse files Browse the repository at this point in the history
Various small fixes
  • Loading branch information
low-earth-orbit authored Oct 6, 2024
2 parents f3481ce + 942f670 commit 5c9593a
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"prettier.prettierPath": "./node_modules/prettier",
"cSpell.words": ["Konva", "reduxjs"]
"cSpell.words": ["Konva", "reduxjs", "trackpad"]
}
47 changes: 40 additions & 7 deletions components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export interface StageSizeType {
export interface CanvasObjectType {
id: string;
type: ObjectType;
tool?: ToolType;
shapeName?: ShapeName;
stroke?: string; // stroke color
strokeWidth?: number;
Expand All @@ -63,7 +62,7 @@ export interface CanvasObjectType {
lineHeight?: number;
}

export type ObjectType = "ink" | "shape" | "text";
export type ObjectType = "ink" | "eraserStroke" | "shape" | "text";

export type ToolType =
| "select"
Expand Down Expand Up @@ -141,6 +140,10 @@ export default function Canvas() {
return `url(${basePath}rectangle.svg) 12 12, pointer`;
case "addOval":
return `url(${basePath}oval.svg) 12 12, pointer`;
case "addTriangle":
return `url(${basePath}triangle.svg) 12 12, pointer`;
case "addStar":
return `url(${basePath}star.svg) 12 12, pointer`;
case "eraser":
return `url(${basePath}erase.svg) 12 12, default`;
default:
Expand Down Expand Up @@ -173,13 +176,37 @@ export default function Canvas() {
localStorage.setItem("canvasState", JSON.stringify(canvasObjects));
}, [canvasObjects]);

// Disable default trackpad zoom behavior globally
useEffect(() => {
const disableZoomAndScroll = (e: WheelEvent) => {
// Prevent zoom in/out triggered by ctrlKey + scroll or pinch zoom
// allow scroll
if (e.ctrlKey || e.deltaY < -1 || e.deltaY > 1) {
e.preventDefault();
}
};

window.addEventListener("wheel", disableZoomAndScroll, { passive: false });

// Cleanup on component unmount
return () => {
window.removeEventListener("wheel", disableZoomAndScroll);
};
}, []);

const handleDelete = useCallback(() => {
if (selectedObjectId === "") {
if (canvasObjects.length > 0) {
setConfirmationModalOpen(true);
}
} else {
dispatch(deleteCanvasObject(selectedObjectId));
// Update cursor style
const stage = stageRef.current;
if (stage) {
const container = stage.container();
container.style.cursor = "default";
}
}
}, [dispatch, selectedObjectId, canvasObjects]);

Expand Down Expand Up @@ -380,8 +407,7 @@ export default function Canvas() {
const pos = e.target.getStage().getRelativePointerPosition();
const newLine: CanvasObjectType = {
id: uuid(),
tool: selectedTool, // eraser or pen
type: "ink",
type: selectedTool === "eraser" ? "eraserStroke" : "ink",
points: [pos.x, pos.y],
stroke: strokeColor,
strokeWidth:
Expand All @@ -395,15 +421,17 @@ export default function Canvas() {
// deselect object when clicked on empty area or clicked on an ink object (created by pen or eraser)
if (
e.target === e.target.getStage() ||
e.target.attrs.name.includes("ink")
e.target.attrs.name.includes("ink") ||
e.target.attrs.name.includes("eraserStroke")
) {
dispatch(selectCanvasObject(""));
}

// side panel
if (
e.target === e.target.getStage() ||
e.target.attrs.name?.includes("ink")
e.target.attrs.name?.includes("ink") ||
e.target.attrs.name?.includes("eraserStroke")
) {
setShapePanelVisible(false);
setTextPanelVisible(false);
Expand All @@ -416,7 +444,12 @@ export default function Canvas() {

const handleMouseMove = (e: any) => {
// Creating new text/object is in progress
if (isInProgress && newObject && newObject.type !== "ink") {
if (
isInProgress &&
newObject &&
newObject.type !== "ink" &&
newObject.type !== "eraserStroke"
) {
const stage = e.target.getStage();
const point = stage.getRelativePointerPosition();

Expand Down
14 changes: 10 additions & 4 deletions components/ink/InkLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ type Props = {

export default function InkLayer({ objects, newObject }: Props) {
const lines = [
...objects.filter((obj: CanvasObjectType) => obj.type === "ink"),
...(newObject && newObject.type === "ink" ? [newObject] : []),
...objects.filter(
(obj: CanvasObjectType) =>
obj.type === "ink" || obj.type === "eraserStroke",
),
...(newObject &&
(newObject.type === "ink" || newObject.type === "eraserStroke")
? [newObject]
: []),
];

return (
<Layer>
{lines.map((line, i) => (
<Line
name={`ink-${line.tool === "eraser" ? "eraser" : "pen"}`}
name={line.type}
key={i}
points={line.points}
stroke={line.stroke}
Expand All @@ -25,7 +31,7 @@ export default function InkLayer({ objects, newObject }: Props) {
lineCap="round"
lineJoin="round"
globalCompositeOperation={
line.tool === "eraser" ? "destination-out" : "source-over"
line.type === "eraserStroke" ? "destination-out" : "source-over"
}
/>
))}
Expand Down
3 changes: 3 additions & 0 deletions components/text/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export default function TextField({

// create textarea and add it to the document
const textarea = document.createElement("textarea");
textarea.wrap = "soft";

document.body.appendChild(textarea);

// adjust the styles to match
Expand Down Expand Up @@ -127,6 +129,7 @@ export default function TextField({
textarea.style.color = node.fill() as string;
textarea.style.transformOrigin = "left top";
textarea.style.scale = zoomLevel.toString();
textarea.style.textWrap = "wrap";

// set rotation
const rotation = node.rotation();
Expand Down
8 changes: 4 additions & 4 deletions components/toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
LineWeightRounded,
CropSquareRounded,
CircleOutlined,
ChangeHistoryOutlined,
StarBorderOutlined,
ChangeHistoryRounded,
StarBorderPurple500Rounded,
TextFields,
UndoRounded,
RedoRounded,
Expand Down Expand Up @@ -356,7 +356,7 @@ function Toolbar({
dispatch(selectCanvasObject("")); // clear selected object, if there is
}}
>
<ChangeHistoryOutlined />
<ChangeHistoryRounded />
</IconButton>
</Tooltip>

Expand All @@ -369,7 +369,7 @@ function Toolbar({
dispatch(selectCanvasObject("")); // clear selected object, if there is
}}
>
<StarBorderOutlined />
<StarBorderPurple500Rounded />
</IconButton>
</Tooltip>
</Popover>
Expand Down
2 changes: 1 addition & 1 deletion public/mousePointer/dark/erase.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/mousePointer/dark/rectangle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/mousePointer/dark/star.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/mousePointer/dark/triangle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/mousePointer/light/erase.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/mousePointer/light/star.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/mousePointer/light/triangle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c9593a

Please sign in to comment.