当前位置:首页 > JavaScript

js实现小球

2026-03-14 18:02:44JavaScript

实现小球动画的JavaScript方法

创建HTML5 Canvas元素作为画布,设置宽度和高度以适应需求。通过JavaScript获取Canvas的上下文,用于绘制图形。

<canvas id="ballCanvas" width="400" height="400"></canvas>

初始化小球对象,包含位置、速度、半径和颜色等属性。使用Math.random()为小球赋予随机初始位置和速度。

js实现小球

const canvas = document.getElementById('ballCanvas');
const ctx = canvas.getContext('2d');

let ball = {
  x: canvas.width / 2,
  y: canvas.height / 2,
  radius: 20,
  dx: 2,
  dy: -2,
  color: 'red'
};

绘制小球的函数

定义绘制函数,使用arc()方法绘制圆形。设置填充样式并调用fill()方法完成绘制。

function drawBall() {
  ctx.beginPath();
  ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
  ctx.fillStyle = ball.color;
  ctx.fill();
  ctx.closePath();
}

动画循环与边界检测

使用requestAnimationFrame创建动画循环。在每帧更新小球位置,并检测是否碰到边界以反转速度方向。

js实现小球

function update() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawBall();

  ball.x += ball.dx;
  ball.y += ball.dy;

  if(ball.x + ball.dx > canvas.width - ball.radius || ball.x + ball.dx < ball.radius) {
    ball.dx = -ball.dx;
  }
  if(ball.y + ball.dy > canvas.height - ball.radius || ball.y + ball.dy < ball.radius) {
    ball.dy = -ball.dy;
  }

  requestAnimationFrame(update);
}

update();

添加鼠标交互功能

通过事件监听实现鼠标控制。mousemove事件更新小球位置,click事件可改变小球颜色。

canvas.addEventListener('mousemove', (e) => {
  const rect = canvas.getBoundingClientRect();
  ball.x = e.clientX - rect.left;
  ball.y = e.clientY - rect.top;
});

canvas.addEventListener('click', () => {
  const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
  ball.color = colors[Math.floor(Math.random() * colors.length)];
});

多小球实现方案

创建小球数组存储多个小球实例。修改更新和绘制逻辑以处理多个对象。

let balls = Array(10).fill().map(() => ({
  x: Math.random() * canvas.width,
  y: Math.random() * canvas.height,
  radius: Math.random() * 20 + 10,
  dx: (Math.random() - 0.5) * 4,
  dy: (Math.random() - 0.5) * 4,
  color: `rgb(${Math.random()*255}, ${Math.random()*255}, ${Math.random()*255})`
}));

function update() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  balls.forEach(ball => {
    drawBall(ball);

    ball.x += ball.dx;
    ball.y += ball.dy;

    if(ball.x + ball.dx > canvas.width - ball.radius || ball.x + ball.dx < ball.radius) {
      ball.dx = -ball.dx;
    }
    if(ball.y + ball.dy > canvas.height - ball.radius || ball.y + ball.dy < ball.radius) {
      ball.dy = -ball.dy;
    }
  });

  requestAnimationFrame(update);
}

标签: 小球js
分享给朋友:

相关文章

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const random…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js实现删除

js实现删除

使用 splice 方法删除数组元素 splice 方法可以删除数组中的元素,并返回被删除的元素。它接受两个参数:起始索引和要删除的元素数量。 const array = [1, 2, 3, 4…

js验证码实现

js验证码实现

验证码的基本原理 验证码(CAPTCHA)用于区分人类用户和自动化程序。常见类型包括图形验证码、滑动验证码、短信验证码等。JavaScript 可用于前端验证码的生成和验证逻辑。 图形验证码实现 使…