import React, { useEffect, useState } from "react";
import "./App.css";
const App = () => {
const [prizeValue, setPrizeValue] = useState("$10"); // Default value
useEffect(() => {
const canvasElement = document.getElementById("scratch");
const canvasContext = canvasElement.getContext("2d");
let isDragging = false;
const initializeCanvas = () => {
const gradient = canvasContext.createLinearGradient(0, 0, 135, 135);
gradient.addColorStop(0, "#d63031");
gradient.addColorStop(1, "#fdcb6e");
canvasContext.fillStyle = gradient;
canvasContext.fillRect(0, 0, 200, 200);
// Generate a random prize value from the available options
const prizeOptions = [
"$1", "$5", "$10", "$20", "$25", "$30", "$35", "$40", "$45", "$50"
];
const randomPrize = prizeOptions[Math.floor(Math.random() * prizeOptions.length)];
setPrizeValue(randomPrize);
};
const scratch = (x, y) => {
canvasContext.globalCompositeOperation = "destination-out";
canvasContext.beginPath();
canvasContext.arc(x, y, 12, 0, 2 * Math.PI);
canvasContext.fill();
};
const getMouseCoordinates = (event) => {
const rect = canvasElement.getBoundingClientRect();
const x = (event.pageX || event.touches[0].pageX) - rect.left;
const y = (event.pageY || event.touches[0].pageY) - rect.top;
return { x, y };
};
const handleMouseDown = (event) => {
isDragging = true;
const { x, y } = getMouseCoordinates(event);
scratch(x, y);
};
const handleMouseMove = (event) => {
if (isDragging) {
event.preventDefault();
const { x, y } = getMouseCoordinates(event);
scratch(x, y);
}
};
const handleMouseUp = () => {
isDragging = false;
};
const handleMouseLeave = () => {
isDragging = false;
};
const isTouchDevice = 'ontouchstart' in window;
canvasElement.addEventListener(isTouchDevice ? "touchstart" : "mousedown", handleMouseDown);
canvasElement.addEventListener(isTouchDevice ? "touchmove" : "mousemove", handleMouseMove);
canvasElement.addEventListener(isTouchDevice ? "touchend" : "mouseup", handleMouseUp);
canvasElement.addEventListener("mouseleave", handleMouseLeave);
initializeCanvas();
// Cleanup event listeners on unmount
return () => {
canvasElement.removeEventListener(isTouchDevice ? "touchstart" : "mousedown", handleMouseDown);
canvasElement.removeEventListener(isTouchDevice ? "touchmove" : "mousemove", handleMouseMove);
canvasElement.removeEventListener(isTouchDevice ? "touchend" : "mouseup", handleMouseUp);
canvasElement.removeEventListener("mouseleave", handleMouseLeave);
};
}, []);
return (
<div className="container">
<div className="base">
<h4>You Won</h4>
<h3>{prizeValue}</h3>
</div>
<canvas
id="scratch"
width="200"
height="200"
style={{
cursor: 'url("https://media.geeksforgeeks.org/wp-content/uploads/20231030101751/bx-eraser-icon.png"), auto'
}}
></canvas>
</div>
);
};
export default App;