-
Notifications
You must be signed in to change notification settings - Fork 3
/
Canvas.jsx
215 lines (207 loc) · 4.92 KB
/
Canvas.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as React from "react";
import { Box } from "@mui/material";
import Grid from "./Grid";
import PropTypes from "prop-types";
import ResizeHandle from "./ResizeHandle";
import SelectionRectangle from "./SelectionRectangle";
import useSelectionRectangle from "./useSelectionRectangle";
/**
* A canvas component that can be used for as part of a drawing application. It includes features such as resize, grid, and selection rectangle.
*/
const Canvas = React.forwardRef(
(
{
backgroundColor = "#ffffff",
backgroundImage = "",
children,
gridColor = "rgba(0, 0, 0, 0.1)",
gridSize = 25,
height = 500,
minHeight = 100,
minWidth = 100,
onKeyDown,
onMouseDown,
onResize,
onSelectionRectangle,
showBorder = true,
showGrid = true,
tabIndex,
width = 500
},
ref
) => {
// selection rectangle logic
const [isSelecting, rectangle, startSelection] = useSelectionRectangle(
onSelectionRectangle,
{
maxHeight: Math.max(height, minHeight),
maxWidth: Math.max(width, minWidth)
}
);
/**
* Callback for mouse down on the canvas. Handles user callback and selection rectangle.
* @param {React.MouseEvent} e Mouse event
*/
const handleMouseDown = e => {
onMouseDown && onMouseDown(e);
startSelection(e);
};
/**
* Callback for canvas being resized. Applies limits and calls user callback.
* @param {number} width Proposed new width
* @param {number} height Proposed new height
*/
const handleResize = (width, height) => {
width = Math.max(width, minWidth);
height = Math.max(height, minHeight);
onResize(width, height);
};
return (
<Box
sx={{
backgroundColor,
backgroundImage: `url(${backgroundImage})`,
backgroundRepeat: "no-repeat",
backgroundSize: `${width}px ${height}px`,
border: showBorder ? `1px solid ${gridColor}` : "none",
height: `${height}px`,
minHeight,
minWidth,
outline: "none",
position: "relative",
width: `${width}px`
}}
onMouseDown={handleMouseDown}
onKeyDown={onKeyDown}
tabIndex={tabIndex}
ref={ref}
id="canvas"
>
{showGrid && <Grid size={gridSize} color={gridColor} />}
{children}
{isSelecting && <SelectionRectangle {...rectangle} />}
{onResize && (
<ResizeHandle
onResize={handleResize}
canvasSize={{
height,
width
}}
/>
)}
</Box>
);
}
);
Canvas.displayName = "Canvas";
export default Canvas;
Canvas.propTypes = {
/**
* Background color of the canvas
*/
backgroundColor: PropTypes.string,
/**
* Background image of the canvas
*/
backgroundImage: PropTypes.string,
/**
* Color of the grid
*/
gridColor: PropTypes.string,
/**
* Grid size in pixels
*/
gridSize: PropTypes.number,
/**
* Height of the canvas
*/
height: PropTypes.number,
/**
* Minimum height of the canvas
*/
minHeight: PropTypes.number,
/**
* Minimum width of the canvas
*/
minWidth: PropTypes.number,
/**
* Callback function to be called on key down
*
* **Signature**
*
* ```
* function(event: React.KeyboardEvent<HTMLDivElement>) => void
* ```
*
* event: The event source of the callback.
*/
onKeyDown: PropTypes.func,
/**
* Callback function to be called on mouse down
*
* **Signature**
*
* ```
* function(event: React.MouseEvent<HTMLDivElement>) => void
* ```
*
* event: The event source of the callback.
*/
onMouseDown: PropTypes.func,
/**
* Callback function to be called when the canvas is resized
*
* **Signature**
*
* ```
* function(newWidth: number, newHeight:number) => void
* ```
*
* newWidth: The new width of the canvas
*
* newHeight: The new height of the canvas
*/
onResize: PropTypes.func,
/**
* Callback function to be called when the user is selecting via rectangle
*
* **Signature**
*
* ```
* function({top: number, left: number, width:number, height:number}) => void
* ```
*
* top: Top position of the selection rectangle
*
* left: Left position of the selection rectangle
*
* width: Width of the selection rectangle
*
* height: Height of the selection rectangle
*/
onSelectionRectangle: PropTypes.func,
/**
* Reference to the canvas
*/
ref: PropTypes.func,
/**
* Show a border around the canvas
*/
showBorder: PropTypes.bool,
/**
* Show a grid on the canvas
*/
showGrid: PropTypes.bool,
/**
* Style object to be applied to the canvas
*/
sx: PropTypes.object,
/**
* Tab index of the canvas
*/
tabIndex: PropTypes.number,
/**
* Width of the canvas
*/
width: PropTypes.number
};