@@ -5,20 +5,23 @@ import { motion, useAnimation } from 'motion/react'
55const ScratchToReveal = ( { width, height, minScratchPercentage = 50 , onComplete, children, className } ) => {
66 const canvasRef = useRef ( null )
77 const [ isScratching , setIsScratching ] = useState ( false )
8- const [ isComplete , setIsComplete ] = useState ( false ) // New state to track completion
8+ const [ isComplete , setIsComplete ] = useState ( false )
99
1010 const controls = useAnimation ( )
1111
1212 useEffect ( ( ) => {
1313 const canvas = canvasRef . current
14- const ctx = canvas ?. getContext ( '2d' )
15- if ( canvas && ctx ) {
16- const gradient = ctx . createLinearGradient ( 0 , 0 , canvas . width , canvas . height )
17- gradient . addColorStop ( 0 , '#A97CF8' )
18- gradient . addColorStop ( 0.5 , '#F38CB8' )
19- gradient . addColorStop ( 1 , '#FDCC92' )
20- ctx . fillStyle = gradient
21- ctx . fillRect ( 0 , 0 , canvas . width , canvas . height )
14+ if ( canvas ) {
15+ // 关键优化:添加willReadFrequently: true
16+ const ctx = canvas . getContext ( '2d' , { willReadFrequently : true } )
17+ if ( ctx ) {
18+ const gradient = ctx . createLinearGradient ( 0 , 0 , canvas . width , canvas . height )
19+ gradient . addColorStop ( 0 , '#A97CF8' )
20+ gradient . addColorStop ( 0.5 , '#F38CB8' )
21+ gradient . addColorStop ( 1 , '#FDCC92' )
22+ ctx . fillStyle = gradient
23+ ctx . fillRect ( 0 , 0 , canvas . width , canvas . height )
24+ }
2225 }
2326 } , [ ] )
2427
0 commit comments