tsx'use client'; import { useEffect, useRef, useState } from 'react'; import confetti from 'canvas-confetti'; interface Premio { texto: string; mensagemWhats: string; isPix: boolean; peso: number; } const LISTA_PREMIOS: Premio[] = [ { texto: 'Transferência Grátis', mensagemWhats: 'Transferência Grátis', isPix: false, peso: 30 }, { texto: 'R$ 200 PIX', mensagemWhats: 'um PIX de R$ 200,00', isPix: true, peso: 25 }, { texto: 'R$ 250 PIX', mensagemWhats: 'um PIX de R$ 250,00', isPix: true, peso: 15 }, { texto: 'R$ 300 PIX', mensagemWhats: 'um PIX de R$ 300,00', isPix: true, peso: 10 }, { texto: 'R$ 400 PIX', mensagemWhats: 'um PIX de R$ 400,00', isPix: true, peso: 8 }, { texto: 'R$ 500 Desconto', mensagemWhats: 'um Desconto de R$ 500,00', isPix: false, peso: 6 }, { texto: 'R$ 500 PIX', mensagemWhats: 'um PIX de R$ 500,00', isPix: true, peso: 4 }, { texto: 'R$ 600 Desconto', mensagemWhats: 'um Desconto de R$ 600,00', isPix: false, peso: 2 }, ]; export default function BevicarRaspadinha() { const canvasRef = useRef(null); const isDrawing = useRef(false); const [revealed, setRevealed] = useState(false); const [premioSorteado, setPremioSorteado] = useState(null); const [jaJogou, setJaJogou] = useState(false); // Função para sortear baseado em peso const sortearPremioComPeso = (): Premio => { const pesoTotal = LISTA_PREMIOS.reduce((acc, p) => acc + p.peso, 0); let numeroAleatorio = Math.random() * pesoTotal; for (const premio of LISTA_PREMIOS) { if (numeroAleatorio < premio.peso) return premio; numeroAleatorio -= premio.peso; } return LISTA_PREMIOS[0]; }; // Verifica trava de segurança local useEffect(() => { const raspadinhaUtilizada = localStorage.getItem('bevicar_raspadinha_usada'); if (raspadinhaUtilizada) { setJaJogou(true); setRevealed(true); // Recupera o prêmio que já havia sido sorteado anteriormente para manter a consistência const premioSalvo = localStorage.getItem('bevicar_premio_ganho'); if (premioSalvo) { setPremioSorteado(JSON.parse(premioSalvo)); } } else { const novoPremio = sortearPremioComPeso(); setPremioSorteado(novoPremio); localStorage.setItem('bevicar_premio_ganho', JSON.stringify(novoPremio)); } }, []); useEffect(() => { // Se já jogou anteriormente, não monta o canvas da raspadinha if (jaJogou || !premioSorteado) return; const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); if (!ctx) return; const resizeCanvas = () => { canvas.width = canvas.parentElement?.clientWidth || 350; canvas.height = canvas.parentElement?.clientHeight || 208; ctx.fillStyle = '#9ca3af'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.font = '900 24px sans-serif'; ctx.fillStyle = '#000000'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('RASPE AQUI', canvas.width / 2, canvas.height / 2); }; resizeCanvas(); const checkScrapedPercentage = () => { const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); const pixels = imageData.data; let transparentPixels = 0; for (let i = 3; i < pixels.length; i += 4) { if (pixels[i] === 0) transparentPixels++; } const percentage = (transparentPixels / (pixels.length / 4)) * 100; // Ao raspar mais de 45%, ativa vitória if (percentage > 45) { setRevealed(true); localStorage.setItem('bevicar_raspadinha_usada', 'true'); // Efeito de Confetes Explosivos confetti({ particleCount: 150, spread: 80, origin: { y: 0.6 } }); } }; const scratch = (x: number, y: number) => { ctx.globalCompositeOperation = 'destination-out'; ctx.beginPath(); ctx.arc(x, y, 25, 0, Math.PI * 2); ctx.fill(); checkScrapedPercentage(); }; const handleMouseDown = () => { isDrawing.current = true; }; const handleMouseUp = () => { isDrawing.current = false; }; const handleMouseMove = (e: MouseEvent) => { if (!isDrawing.current || revealed) return; const rect = canvas.getBoundingClientRect(); scratch(e.clientX - rect.left, e.clientY - rect.top); }; const handleTouchStart = () => { isDrawing.current = true; }; const handleTouchEnd = () => { isDrawing.current = false; }; const handleTouchMove = (e: TouchEvent) => { if (!isDrawing.current || revealed || e.touches.length === 0) return; const rect = canvas.getBoundingClientRect(); scratch(e.touches[0].clientX - rect.left, e.touches[0].clientY - rect.top); }; canvas.addEventListener('mousedown', handleMouseDown); window.addEventListener('mouseup', handleMouseUp); canvas.addEventListener('mousemove', handleMouseMove); canvas.addEventListener('touchstart', handleTouchStart); window.addEventListener('touchend', handleTouchEnd); canvas.addEventListener('touchmove', handleTouchMove, { passive: true }); return () => { canvas.removeEventListener('mousedown', handleMouseDown); window.removeEventListener('mouseup', handleMouseUp); canvas.removeEventListener('mousemove', handleMouseMove); canvas.removeEventListener('touchstart', handleTouchStart); window.removeEventListener('touchend', handleTouchEnd); }; }, [revealed, premioSorteado, jaJogou]); const handleWhatsAppClick = () => { if (!revealed) return; if (!premioSorteado) return; const phone = '5541997155778'; const text = encodeURIComponent( `Olá! Eu validei minha raspadinha da Bevicar Automóveis e garanti ${premioSorteado.mensagemWhats}! Estou ciente do regulamento de 30 dias vinculado à compra do carro. Como faço para resgatar na minha ficha?` ); window.open(`https://wa.me{phone}?text=${text}`, '_blank'); }; return (
{/* Cabeçalho */}

Raspe e Ganhe

Bevicar Automóveis 🚗

{/* Conteúdo */}
{/* Regras e Lista */}

{jaJogou ? 'Seu Prêmio Ativo:' : 'Garanta seu prêmio:'}

*Limite de 1 raspadinha por veículo adquirido. Validade de 30 dias.

💸 PIX na hora

🚘 Transf. Grátis

🔥 Descontos altos

🎁 Vantagens únicas

{/* Área da Raspadinha */}
{/* Prêmio Exibido */} {premioSorteado && (

{jaJogou ? '🏆 SEU PRÊMIO 🏆' : '🎉 PARABÉNS 🎉'}

Você faturou

{premioSorteado.texto}

Registrado no navegador por 30 dias

)} {/* Camada Raspável (Só renderiza se NUNCA tiver jogado) */} {!revealed && !jaJogou && ( )}
{/* Botão WhatsApp */} {/* Rodapé */}

(41) 99715-5778

Av. das Araucárias, 683

Fazenda Rio Grande - PR

); }