【前端】弹球特效(重力模拟)
创始人
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;

在这里插入图片描述

相关内容

热门资讯

终于清楚辅助!七七娱乐辅助AI... 终于清楚辅助!七七娱乐辅助AI插件,七七娱乐多功能一直真的有辅助插件1、完成七七娱乐有辅助插件,帮助...
一秒答解外挂!天天卡五星外挂A... 一秒答解外挂!天天卡五星外挂AI攻略,天天卡五星多功能本来真的是有外挂攻略1、操作简单,无需天天卡五...
玩家必备教程辅助器!心悦辽宁麻... 玩家必备教程辅助器!心悦辽宁麻将辅助器AIapp,心悦辽宁麻将多功能一直存在有辅助器app所有人都在...
重大来袭辅助器!残局斗地主辅助... 重大来袭辅助器!残局斗地主辅助器AI插件,残局斗地主多功能确实是有辅助器插件1、起透看视 残局斗地主...
盘点十款辅助!小熊猫麻将辅助A... 盘点十款辅助!小熊猫麻将辅助AI脚本,小熊猫麻将多功能果然是真的有辅助脚本小熊猫麻将脚本下载中分为三...
必看攻略辅助!创乐缘辅助AI技... 必看攻略辅助!创乐缘辅助AI技巧,创乐缘多功能原来真的有辅助技巧1、起透看视 创乐缘辅助软件价格2、...
重大通报透视挂!熊猫互娱透视挂... 重大通报透视挂!熊猫互娱透视挂AI插件,熊猫互娱多功能竟然有透视挂插件1、让任何用户在无需熊猫互娱安...
程序员教你辅助器!联众娱乐辅助... 程序员教你辅助器!联众娱乐辅助器AI技巧,联众娱乐多功能一贯有辅助器技巧1、联众娱乐免费辅助多个强度...
科普分享作弊器!推筒子作弊器A... 科普分享作弊器!推筒子作弊器AI插件,推筒子多功能原来是有作弊器插件所有人都在同一条线上,像星星一样...
一分钟揭秘开挂!牛大亨开挂AI... 一分钟揭秘开挂!牛大亨开挂AI攻略,牛大亨多功能真是真的有开挂攻略1、牛大亨透视辅助软件激活码多个强...