【前端】弹球特效(重力模拟)
创始人
2025-01-08 18:05:53
0

在这里插入图片描述
在这里插入图片描述

弹球特效

文章目录

    • 弹球特效

请添加图片描述

import React, { useRef, useEffect } from 'react';  const BouncingBallSimulation = () => {   const canvasRef = useRef(null);    useEffect(() => {     const canvas = canvasRef.current;     const ctx = canvas.getContext('2d');     let animationFrameId;      // Set canvas size     canvas.width = 900;     canvas.height = 1600;      // Circle properties     const centerX = canvas.width / 2;     const centerY = canvas.height / 2;     const radius = Math.min(centerX, centerY) * 0.7;      // Ball properties     const ballRadius = 15;     let x = centerX;     let y = centerY - radius + ballRadius;     let dx = 2;     let dy = 0;      // Gravity and friction     const gravity = 0.2;     const friction = 0.99;      // Matrix rain properties     const fontSize = 14;     const columns = canvas.width / fontSize;     const drops = [];     for (let i = 0; i < columns; i++) {       drops[i] = 1;     }      const drawMatrixRain = () => {       ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';       ctx.fillRect(0, 0, canvas.width, canvas.height);        ctx.fillStyle = '#0F0';       ctx.font = `${fontSize}px monospace`;        for (let i = 0; i < drops.length; i++) {         const text = String.fromCharCode(0x30A0 + Math.random() * 96);         ctx.fillText(text, i * fontSize, drops[i] * fontSize);          if (drops[i] * fontSize > canvas.height && Math.random() > 0.975) {           drops[i] = 0;         }         drops[i]++;       }     };      const draw = () => {       ctx.clearRect(0, 0, canvas.width, canvas.height);        // Draw matrix rain       drawMatrixRain();        // Draw circle       ctx.beginPath();       ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);       ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)';       ctx.stroke();        // Draw ball       ctx.beginPath();       ctx.arc(x, y, ballRadius, 0, Math.PI * 2);       ctx.fillStyle = 'white';       ctx.fill();        // Apply gravity       dy += gravity;        // Ball movement and collision detection       const nextX = x + dx;       const nextY = y + dy;       const distanceFromCenter = Math.sqrt((nextX - centerX) ** 2 + (nextY - centerY) ** 2);        if (distanceFromCenter + ballRadius > radius) {         // Calculate the normal vector         const nx = (nextX - centerX) / distanceFromCenter;         const ny = (nextY - centerY) / distanceFromCenter;          // Calculate the dot product         const dotProduct = dx * nx + dy * ny;          // Update velocity         dx = (dx - 2 * dotProduct * nx) * friction;         dy = (dy - 2 * dotProduct * ny) * friction;       } else {         x = nextX;         y = nextY;       }        animationFrameId = requestAnimationFrame(draw);     };      draw();      return () => {       cancelAnimationFrame(animationFrameId);     };   }, []);    return (     
canvasRef} className="border border-gray-300" />
); }; export default BouncingBallSimulation;

在这里插入图片描述

相关内容

热门资讯

wpk真的有外挂!wpk发牌逻... wpk真的有外挂!wpk发牌逻辑(透视)外挂透视挂辅助安装(固有存在有挂)准备好在ia的高塔上攀登,...
aapoker辅助工具存在!a... aapoker辅助工具存在!aapoker德州线上扑克,(aapoker俱乐部后台)从前存在有挂(详...
微扑克ai机器人(微扑克)微扑... 微扑克ai机器人(微扑克)微扑克系统的发牌速度有多快(透视)竟然有挂(详细辅助详细教程)1、很好的工...
六分钟实锤!德扑ai智能(德州... 六分钟实锤!德扑ai智能(德州扑克)真是真的有挂(详细辅助攻略教程)1、下载好德扑ai智能辅助软件之...
微扑克辅助机器人(微扑克)微扑... 微扑克辅助机器人(微扑克)微扑克有挂(透视)确实真的有挂(详细辅助我来教教你)该软件可以轻松地帮助玩...
aapoker发牌机制!aap... aapoker发牌机制!aapoker插件,(aapoker规律)素来存在有挂(详细辅助曝光教程),...
wpk俱乐部!wpk透视辅助合... wpk俱乐部!wpk透视辅助合作(透视)外挂透视辅助代打(都是真的是有挂);1. ai辅助创建新账号...
第一分钟实锤!德扑之星辅助器购... 第一分钟实锤!德扑之星辅助器购买(德州ai)一贯真的是有挂(详细辅助德州教程)1、首先打开德扑之星辅...
微扑克ai机器人(微扑克)微扑... 微扑克ai机器人(微扑克)微扑克app发牌规律(透视)一贯真的有挂(详细辅助靠谱教程);微扑克ai机...
aapoker透明挂!aapo... aapoker透明挂!aapoker线上有外挂吗,(aapoker苹果版)都是是真的有挂(详细辅助分...