-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from low-earth-orbit/rectangle
Add rectangle feature
- Loading branch information
Showing
7 changed files
with
343 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { MutableRefObject, useRef } from "react"; | ||
import { Stage, Layer, Line, Rect } from "react-konva"; | ||
import { LineType, StageSizeType } from "./Canvas"; | ||
|
||
type FreeDrawLayerProps = { | ||
lines: LineType[]; | ||
setLines: (newLines: LineType[]) => void; | ||
tool: string; | ||
color: string; | ||
strokeWidth: number; | ||
stageSize: StageSizeType; | ||
isFreeDrawing: MutableRefObject<boolean>; | ||
}; | ||
|
||
export default function FreeDrawLayer({ lines }: FreeDrawLayerProps) { | ||
return ( | ||
<Layer> | ||
{lines.map((line, i) => ( | ||
<Line | ||
key={i} | ||
points={line.points} | ||
stroke={line.stroke} | ||
strokeWidth={line.strokeWidth} | ||
tension={0.5} | ||
lineCap="round" | ||
lineJoin="round" | ||
globalCompositeOperation={ | ||
line.tool === "eraser" ? "destination-out" : "source-over" | ||
} | ||
/> | ||
))} | ||
</Layer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { MutableRefObject, useState } from "react"; | ||
import { Stage, Layer } from "react-konva"; | ||
import { ShapeType, StageSizeType } from "./Canvas"; | ||
import Rectangle from "./shapes/Rectangle"; | ||
|
||
type ShapesLayerProps = { | ||
shapes: ShapeType[]; | ||
setShapes: (newShapes: ShapeType[]) => void; | ||
tool: string; | ||
color: string; | ||
strokeWidth: number; | ||
stageSize: StageSizeType; | ||
isFreeDrawing: MutableRefObject<boolean>; | ||
selectedShapeId: string; | ||
setSelectedShapeId: (id: string) => void; | ||
}; | ||
|
||
export default function ShapesLayer({ | ||
tool, | ||
shapes, | ||
setShapes, | ||
color, | ||
strokeWidth, | ||
stageSize, | ||
isFreeDrawing, | ||
selectedShapeId, | ||
setSelectedShapeId, | ||
}: ShapesLayerProps) { | ||
return ( | ||
<Layer> | ||
{shapes.map((shape, i) => { | ||
const shapeProps = { | ||
id: shape.id, | ||
x: shape.x, | ||
y: shape.y, | ||
width: shape.width, | ||
height: shape.height, | ||
strokeWidth: shape.strokeWidth, | ||
stroke: shape.stroke, | ||
}; | ||
|
||
return ( | ||
<Rectangle | ||
key={i} | ||
shapeProps={shapeProps} | ||
isSelected={shape.id === selectedShapeId} | ||
onSelect={() => { | ||
setSelectedShapeId(shape.id); | ||
}} | ||
onChange={(newAttrs: ShapeType) => { | ||
const newShapes = shapes.slice(); // deep copy | ||
newShapes[i] = newAttrs; | ||
setShapes(newShapes); | ||
}} | ||
/> | ||
); | ||
})} | ||
</Layer> | ||
); | ||
} |
Oops, something went wrong.