-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolbar): add toolbar item and clear action
- Loading branch information
Showing
8 changed files
with
138 additions
and
55 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
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
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 |
---|---|---|
@@ -1,89 +1,108 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useEffect, useRef, useCallback } from 'react'; | ||
|
||
type Line = { x: number; y: number }[]; | ||
|
||
export const useCanvasDrawing = ( | ||
parentRef?: React.RefObject<HTMLDivElement> | ||
) => { | ||
const canvasRef = useRef<HTMLCanvasElement | null>(null); | ||
const [isDrawing, setIsDrawing] = useState(false); | ||
const [lines, setLines] = useState<Line[]>([]); | ||
const isDrawingRef = useRef(false); | ||
const linesRef = useRef<Line[]>([]); | ||
|
||
useEffect(() => { | ||
const canvas = canvasRef.current; | ||
const clearCanvas = () => { | ||
linesRef.current = []; | ||
redrawLines(); | ||
}; | ||
|
||
const drawLine = useCallback((line: Line) => { | ||
const canvas = canvasRef.current; | ||
if (!canvas) { | ||
return; | ||
} | ||
const ctx = canvas.getContext('2d'); | ||
|
||
if (!ctx || line.length < 2) { | ||
return; | ||
} | ||
|
||
const [start, ...rest] = line; | ||
ctx.beginPath(); | ||
ctx.moveTo(start.x, start.y); | ||
rest.forEach((point) => { | ||
ctx.lineTo(point.x, point.y); | ||
}); | ||
ctx.stroke(); | ||
}, []); | ||
|
||
const redrawLines = useCallback(() => { | ||
const canvas = canvasRef.current; | ||
if (!canvas) { | ||
return; | ||
} | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
if (!canvas || !ctx) { | ||
return; | ||
} | ||
|
||
const redrawLines = () => { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
lines.forEach((line) => { | ||
ctx.beginPath(); | ||
line.forEach((point, index) => { | ||
if (index === 0) { | ||
ctx.moveTo(point.x, point.y); | ||
} else { | ||
ctx.lineTo(point.x, point.y); | ||
ctx.stroke(); | ||
} | ||
}); | ||
ctx.closePath(); | ||
}); | ||
}; | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
linesRef.current.forEach(drawLine); | ||
}, [drawLine]); | ||
|
||
const resizeCanvas = () => { | ||
canvas.width = parentRef?.current?.clientWidth ?? window.innerWidth; | ||
canvas.height = parentRef?.current?.clientHeight ?? window.innerHeight; | ||
redrawLines(); | ||
}; | ||
// Gestion du redimensionnement avec debounce pour optimiser les performances | ||
const resizeCanvas = useCallback(() => { | ||
const canvas = canvasRef.current; | ||
if (!canvas || !parentRef?.current) return; | ||
|
||
window.addEventListener('resize', resizeCanvas); | ||
resizeCanvas(); | ||
canvas.width = parentRef.current.clientWidth; | ||
canvas.height = parentRef.current.clientHeight; | ||
redrawLines(); | ||
}, [redrawLines, parentRef]); | ||
|
||
const startDrawing = (e: MouseEvent) => { | ||
setIsDrawing(true); | ||
useEffect(() => { | ||
const canvas = canvasRef.current; | ||
if (!canvas) return; | ||
|
||
// Ajout des écouteurs d'événements | ||
const startDrawing = (e: MouseEvent) => { | ||
isDrawingRef.current = true; | ||
const rect = canvas.getBoundingClientRect(); | ||
const x = e.clientX - rect.left; | ||
const y = e.clientY - rect.top; | ||
setLines((prevLines) => [...prevLines, [{ x, y }]]); | ||
linesRef.current.push([{ x, y }]); | ||
}; | ||
|
||
const draw = (e: MouseEvent) => { | ||
if (!isDrawing) return; | ||
const currentLine = lines[lines.length - 1]; | ||
if (!isDrawingRef.current) return; | ||
const rect = canvas.getBoundingClientRect(); | ||
const x = e.clientX - rect.left; | ||
const y = e.clientY - rect.top; | ||
const newLine = [...currentLine, { x, y }]; | ||
|
||
setLines((prevLines) => [...prevLines.slice(0, -1), newLine]); | ||
redrawLines(); | ||
const currentLine = linesRef.current[linesRef.current.length - 1]; | ||
currentLine.push({ x, y }); | ||
drawLine(currentLine); | ||
}; | ||
|
||
const stopDrawing = () => { | ||
setIsDrawing(false); | ||
isDrawingRef.current = false; | ||
redrawLines(); // Redessine pour s'assurer que tout est cohérent | ||
}; | ||
|
||
canvas.addEventListener('mousedown', startDrawing); | ||
canvas.addEventListener('mousemove', draw); | ||
canvas.addEventListener('mouseup', stopDrawing); | ||
canvas.addEventListener('mouseleave', stopDrawing); | ||
|
||
window.addEventListener('resize', resizeCanvas); | ||
resizeCanvas(); // Applique initialement la taille du canvas | ||
|
||
// Nettoyage des écouteurs d'événements | ||
return () => { | ||
window.removeEventListener('resize', resizeCanvas); | ||
canvas.removeEventListener('mousedown', startDrawing); | ||
canvas.removeEventListener('mousemove', draw); | ||
canvas.removeEventListener('mouseup', stopDrawing); | ||
canvas.removeEventListener('mouseleave', stopDrawing); | ||
window.removeEventListener('resize', resizeCanvas); | ||
}; | ||
}, [parentRef, lines, isDrawing]); | ||
}, [resizeCanvas, drawLine, redrawLines]); | ||
|
||
return [canvasRef, lines] as const; | ||
return { canvasRef, clearCanvas }; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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