main
parent
da2fe252b6
commit
2781042b7e
|
@ -6,6 +6,7 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
const [isDrawing, setIsDrawing] = useState(false);
|
const [isDrawing, setIsDrawing] = useState(false);
|
||||||
const [currentTool, setCurrentTool] = useState('pencil');
|
const [currentTool, setCurrentTool] = useState('pencil');
|
||||||
const [shapes, setShapes] = useState([]);
|
const [shapes, setShapes] = useState([]);
|
||||||
|
const [lastPosition, setLastPosition] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
|
@ -42,18 +43,14 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.strokeStyle = shape.color;
|
ctx.strokeStyle = shape.color;
|
||||||
ctx.lineWidth = shape.thickness;
|
ctx.lineWidth = shape.thickness;
|
||||||
|
ctx.lineCap = 'round';
|
||||||
switch (shape.type) {
|
|
||||||
case 'rectangle':
|
if (shape.type === 'rectangle') {
|
||||||
ctx.rect(shape.x, shape.y, shape.width, shape.height);
|
ctx.rect(shape.x, shape.y, shape.width, shape.height);
|
||||||
break;
|
} else if (shape.type === 'circle') {
|
||||||
case 'circle':
|
ctx.arc(shape.x, shape.y, shape.radius, 0, Math.PI * 2);
|
||||||
ctx.arc(shape.x, shape.y, shape.radius, 0, Math.PI * 2);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -64,7 +61,9 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
const x = e.clientX - rect.left;
|
const x = e.clientX - rect.left;
|
||||||
const y = e.clientY - rect.top;
|
const y = e.clientY - rect.top;
|
||||||
|
|
||||||
if (currentTool === 'rectangle') {
|
if (currentTool === 'pencil') {
|
||||||
|
setLastPosition({ x, y });
|
||||||
|
} else if (currentTool === 'rectangle') {
|
||||||
setShapes([...shapes, {
|
setShapes([...shapes, {
|
||||||
type: 'rectangle',
|
type: 'rectangle',
|
||||||
x,
|
x,
|
||||||
|
@ -96,33 +95,43 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
const y = e.clientY - rect.top;
|
const y = e.clientY - rect.top;
|
||||||
|
|
||||||
if (currentTool === 'pencil') {
|
if (currentTool === 'pencil') {
|
||||||
const lastX = history[history.length - 1]?.x2 || x;
|
const lastPos = lastPosition;
|
||||||
const lastY = history[history.length - 1]?.y2 || y;
|
|
||||||
|
|
||||||
setHistory([...history, {
|
if (lastPos) {
|
||||||
type: 'draw',
|
// Draw the line immediately
|
||||||
x1: lastX,
|
ctx.beginPath();
|
||||||
y1: lastY,
|
ctx.moveTo(lastPos.x, lastPos.y);
|
||||||
x2: x,
|
ctx.lineTo(x, y);
|
||||||
y2: y,
|
ctx.strokeStyle = color;
|
||||||
color,
|
ctx.lineWidth = thickness;
|
||||||
thickness
|
ctx.lineCap = 'round';
|
||||||
}]);
|
ctx.stroke();
|
||||||
|
|
||||||
|
// Update history
|
||||||
|
setHistory([...history, {
|
||||||
|
type: 'draw',
|
||||||
|
x1: lastPos.x,
|
||||||
|
y1: lastPos.y,
|
||||||
|
x2: x,
|
||||||
|
y2: y,
|
||||||
|
color,
|
||||||
|
thickness
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update last position
|
||||||
|
setLastPosition({ x, y });
|
||||||
} else if (currentTool === 'rectangle' || currentTool === 'circle') {
|
} else if (currentTool === 'rectangle' || currentTool === 'circle') {
|
||||||
const updatedShapes = shapes.map((shape, index) => {
|
const updatedShapes = shapes.map((shape, index) => {
|
||||||
if (index === shapes.length - 1) {
|
if (index === shapes.length - 1) {
|
||||||
|
// Update the shape dimensions
|
||||||
if (currentTool === 'rectangle') {
|
if (currentTool === 'rectangle') {
|
||||||
return {
|
const width = Math.abs(x - shape.x);
|
||||||
...shape,
|
const height = Math.abs(y - shape.y);
|
||||||
width: Math.abs(x - shape.x),
|
return { ...shape, width, height };
|
||||||
height: Math.abs(y - shape.y)
|
} else if (currentTool === 'circle') {
|
||||||
};
|
|
||||||
} else {
|
|
||||||
const radius = Math.sqrt(Math.pow(x - shape.x, 2) + Math.pow(y - shape.y, 2));
|
const radius = Math.sqrt(Math.pow(x - shape.x, 2) + Math.pow(y - shape.y, 2));
|
||||||
return {
|
return { ...shape, radius };
|
||||||
...shape,
|
|
||||||
radius
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return shape;
|
return shape;
|
||||||
|
@ -131,8 +140,31 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Separate function to handle shape drawing
|
||||||
|
useEffect(() => {
|
||||||
|
const canvas = canvasRef.current;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
|
// Clear canvas and redraw all shapes
|
||||||
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
|
// Draw all shapes
|
||||||
|
shapes.forEach((shape) => {
|
||||||
|
drawShape(ctx, shape);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Draw history (for pencil drawing)
|
||||||
|
history.forEach((action) => {
|
||||||
|
if (action.type === 'draw') {
|
||||||
|
drawLine(ctx, action.x1, action.y1, action.x2, action.y2, action.color, action.thickness);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [shapes, history]);
|
||||||
|
|
||||||
const handleMouseUp = () => {
|
const handleMouseUp = () => {
|
||||||
setIsDrawing(false);
|
setIsDrawing(false);
|
||||||
|
// Clear last position when mouse is released
|
||||||
|
setLastPosition(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUndo = () => {
|
const handleUndo = () => {
|
||||||
|
@ -146,13 +178,13 @@ const DrawingCanvas = ({ color, setColor, thickness, setThickness, history, setH
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="drawing-container">
|
<div className="drawing-canvas-container">
|
||||||
<canvas
|
<canvas
|
||||||
ref={canvasRef}
|
ref={canvasRef}
|
||||||
|
className="drawing-canvas"
|
||||||
onMouseDown={handleMouseDown}
|
onMouseDown={handleMouseDown}
|
||||||
onMouseMove={handleMouseMove}
|
onMouseMove={handleMouseMove}
|
||||||
onMouseUp={handleMouseUp}
|
onMouseUp={handleMouseUp}
|
||||||
className="drawing-canvas"
|
|
||||||
/>
|
/>
|
||||||
<div className="toolbar">
|
<div className="toolbar">
|
||||||
<button onClick={() => setCurrentTool('pencil')} className={`tool-button ${currentTool === 'pencil' ? 'active' : ''}`}>
|
<button onClick={() => setCurrentTool('pencil')} className={`tool-button ${currentTool === 'pencil' ? 'active' : ''}`}>
|
||||||
|
|
Loading…
Reference in New Issue