-
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.
- Loading branch information
Showing
8 changed files
with
408 additions
and
11 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,119 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import Paper from "paper"; | ||
import { SimulatorPath } from "../../utils/types/path"; | ||
|
||
interface PathRendererProps { | ||
paths: SimulatorPath[]; | ||
simulatorId?: string; | ||
shouldReset?: boolean; | ||
onResetComplete?: () => void; | ||
} | ||
|
||
export const PathRenderer: React.FC<PathRendererProps> = ({ | ||
paths, | ||
simulatorId = "default", | ||
shouldReset = false, | ||
onResetComplete, | ||
}) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const scopeRef = useRef<paper.PaperScope>(); | ||
|
||
// Initialize Paper.js scope | ||
useEffect(() => { | ||
if (!canvasRef.current) return; | ||
|
||
// Create a new Paper.js scope for this canvas | ||
const scope = new Paper.PaperScope(); | ||
scopeRef.current = scope; | ||
|
||
// Get container dimensions | ||
const container = canvasRef.current.parentElement; | ||
if (!container) return; | ||
|
||
const rect = container.getBoundingClientRect(); | ||
const pixelRatio = 1; | ||
|
||
canvasRef.current.width = rect.width * pixelRatio; | ||
canvasRef.current.height = rect.height * pixelRatio; | ||
|
||
// Setup with explicit scope | ||
scope.setup(canvasRef.current); | ||
scope.view.viewSize = new scope.Size(rect.width, rect.height); | ||
scope.view.scale(pixelRatio, pixelRatio); | ||
|
||
const handleResize = () => { | ||
if (!container || !canvasRef.current || !scope.view) return; | ||
scope.activate(); // Activate this scope before operations | ||
const newRect = container.getBoundingClientRect(); | ||
canvasRef.current.width = newRect.width * pixelRatio; | ||
canvasRef.current.height = newRect.height * pixelRatio; | ||
scope.view.viewSize = new scope.Size(newRect.width, newRect.height); | ||
}; | ||
|
||
window.addEventListener("resize", handleResize); | ||
|
||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
scope.activate(); | ||
scope.project?.clear(); | ||
}; | ||
}, [simulatorId]); | ||
|
||
// Handle reset | ||
useEffect(() => { | ||
const scope = scopeRef.current; | ||
if (!scope || !scope.project) return; | ||
|
||
if (shouldReset) { | ||
scope.activate(); | ||
scope.project.activeLayer.removeChildren(); | ||
scope.view.update(); | ||
onResetComplete?.(); | ||
} | ||
}, [shouldReset, onResetComplete]); | ||
|
||
// Update paths | ||
useEffect(() => { | ||
const scope = scopeRef.current; | ||
if (!scope || !scope.project) return; | ||
|
||
scope.activate(); | ||
|
||
// Clear previous frame | ||
scope.project.activeLayer.removeChildren(); | ||
|
||
// Draw all paths | ||
paths.forEach((simulatorPath) => { | ||
const pathCopy = simulatorPath.path.clone(); | ||
scope.project.activeLayer.addChild(pathCopy); | ||
|
||
console.log(`[${simulatorId}] Path added:`, { | ||
segments: pathCopy.segments.length, | ||
visible: pathCopy.visible, | ||
strokeColor: pathCopy.strokeColor, | ||
position: pathCopy.position, | ||
}); | ||
}); | ||
|
||
scope.view.update(); | ||
console.log( | ||
`[${simulatorId}] Layer children:`, | ||
scope.project.activeLayer.children.length | ||
); | ||
}, [paths, simulatorId]); | ||
|
||
return ( | ||
<canvas | ||
ref={canvasRef} | ||
className={`paper-canvas-${simulatorId}`} | ||
style={{ | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
width: "100%", | ||
height: "100%", | ||
pointerEvents: "none", | ||
}} | ||
/> | ||
); | ||
}; |
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,66 @@ | ||
import { Point } from "paper"; | ||
import { Scenario } from "../../types/scenario"; | ||
import { SerializableSimulatorPath } from "../../utils/types/path"; | ||
|
||
const createCurvedDiagonalPath = (): SerializableSimulatorPath => { | ||
return { | ||
id: "curved-diagonal", | ||
points: [ | ||
{ | ||
x: 100, | ||
y: 100, | ||
handleOut: { x: 100, y: 0 }, | ||
}, | ||
{ | ||
x: 300, | ||
y: 300, | ||
handleIn: { x: -100, y: 0 }, | ||
handleOut: { x: 100, y: 0 }, | ||
}, | ||
{ | ||
x: 500, | ||
y: 500, | ||
handleIn: { x: -100, y: 0 }, | ||
}, | ||
], | ||
closed: false, | ||
position: { x: 300, y: 300 }, | ||
label: "Curved Path", | ||
mass: 50000, | ||
strokeColor: "#ff0000", | ||
strokeWidth: 5, | ||
opacity: 1, | ||
}; | ||
}; | ||
|
||
export const pathTest: Scenario = { | ||
id: "path-test", | ||
name: "Path Test", | ||
description: | ||
"A test scenario with a curved diagonal path that exerts gravity", | ||
data: { | ||
settings: { | ||
SHOW_VELOCITY_ARROWS: true, | ||
SHOW_FORCE_ARROWS: true, | ||
NEW_PARTICLE_MASS: 0.01, | ||
NEW_PARTICLE_ELASTICITY: 0.8, | ||
POINTER_MASS: 0, | ||
FRICTION: 1, | ||
PARTICLES_EXERT_GRAVITY: false, | ||
SOLID_BOUNDARIES: true, | ||
}, | ||
gravityPoints: [], | ||
particles: [ | ||
{ | ||
id: "test-particle", | ||
position: { x: 50, y: 50 }, | ||
velocity: { x: 3, y: 0 }, | ||
mass: 0.1, | ||
elasticity: 0.8, | ||
color: "#ffffff", | ||
size: 5, | ||
}, | ||
], | ||
paths: [createCurvedDiagonalPath()], | ||
}, | ||
}; |
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
Oops, something went wrong.