当前位置:首页 > JavaScript

js实现小球

2026-03-14 18:02:44JavaScript

实现小球动画的JavaScript方法

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

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

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

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创建动画循环。在每帧更新小球位置,并检测是否碰到边界以反转速度方向。

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)];
});

多小球实现方案

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

js实现小球

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实现拖拽

实现拖拽的基本步骤 使用JavaScript实现拖拽功能需要监听鼠标事件,包括mousedown、mousemove和mouseup。以下是实现的基本逻辑: const draggableEleme…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…