Last active
April 1, 2024 09:57
-
-
Save Karan-Palan/df1393cd793aa2e3af82cbda3ff6e790 to your computer and use it in GitHub Desktop.
SVG resize on collision detection basic example for Musicblocks
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
import React, { useState, useEffect } from 'react'; | |
const Brick: React.FC<{ x: number; y: number; width: number; height: number }> = ({ x, y, width, height }) => { | |
return ( | |
<rect x={x} y={y} width={width} height={height} fill="blue" /> | |
); | |
}; | |
const SVGResize: React.FC = () => { | |
const [brickSize, setBrickSize] = useState({ width: 100, height: 100 }); | |
const [isColliding, setIsColliding] = useState(false); | |
useEffect(() => { | |
// Simulating collision detection | |
const collisionDetected = checkCollision(); | |
setIsColliding(collisionDetected); | |
}, []); | |
useEffect(() => { | |
// Resize brick dynamically based on collision detection | |
if (isColliding) { | |
setBrickSize({ width: 80, height: 80 }); | |
} | |
}, [isColliding]); | |
const checkCollision = (): boolean => { | |
// Simulated collision detection logic | |
// Replace this with the actual collision detection logic or import the file/function | |
return Math.random() > 0.5; // Returns true or false randomly | |
}; | |
return ( | |
<div> | |
<svg width="200" height="200"> | |
<Brick x={50} y={50} width={brickSize.width} height={brickSize.height} /> | |
</svg> | |
</div> | |
); | |
}; | |
export default SVGResize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment