import React, { useState } from 'react' import { Image, View } from 'react-native' import { RnScratchCard, ScratchCardProps, UserInput } from './RnScratchCard' import { ScratchGrid } from './ScratchGrid' export const ScratchCard: React.FC = (props: ScratchCardProps) => { const image = Image.resolveAssetSource(props.source) const [grid, setGrid] = useState() return ( { const { width, height } = event.nativeEvent.layout if (grid?.size.width !== width || grid?.size.height !== height) { const size = { width, height } setGrid(new ScratchGrid(size, props.brushWidth)) } }}> ) function handleOnScratch(event: UserInput) { if (props.onScratch && grid) { const { nativeEvent } = event const x = Math.min(grid.size.width - 1, Math.max(0, nativeEvent.x)) const y = Math.min(grid.size.height - 1, Math.max(0, nativeEvent.y)) grid.update({ x, y }) props.onScratch(grid.percentCompleted) } } }