当前位置:首页 > JavaScript

js实现小球

2026-04-06 11:50:48JavaScript

实现小球动画的 JavaScript 方法

使用 Canvas 绘制小球

创建一个 Canvas 元素并设置其宽高,通过 JavaScript 获取上下文进行绘制:

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const ball = {
  x: canvas.width / 2,
  y: canvas.height / 2,
  radius: 20,
  dx: 2,
  dy: -2,
  color: 'red'
};

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

实现小球运动逻辑

更新小球位置并处理边界碰撞检测:

function updateBall() {
  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;
  }
}

动画循环

js实现小球

使用 requestAnimationFrame 实现平滑动画:

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawBall();
  updateBall();
  requestAnimationFrame(animate);
}
animate();

使用 CSS 实现小球效果

HTML 结构

<div class="ball"></div>

CSS 样式

js实现小球

.ball {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: blue;
  position: absolute;
}

JavaScript 控制动画

const ball = document.querySelector('.ball');
let x = 0, y = 0;
let dx = 2, dy = 2;

function moveBall() {
  x += dx;
  y += dy;

  // 边界检测
  if (x + 50 > window.innerWidth || x < 0) dx = -dx;
  if (y + 50 > window.innerHeight || y < 0) dy = -dy;

  ball.style.transform = `translate(${x}px, ${y}px)`;
  requestAnimationFrame(moveBall);
}
moveBall();

物理效果增强

添加重力加速度

let gravity = 0.2;
let friction = 0.99;

function applyPhysics() {
  ball.dy += gravity;
  ball.dx *= friction;
  ball.dy *= friction;

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

  // 地面碰撞
  if (ball.y + ball.radius > canvas.height) {
    ball.y = canvas.height - ball.radius;
    ball.dy = -ball.dy * 0.8; // 弹性系数
  }
}

交互控制示例

鼠标点击生成小球

canvas.addEventListener('click', (e) => {
  const newBall = {
    x: e.clientX,
    y: e.clientY,
    radius: Math.random() * 20 + 10,
    dx: (Math.random() - 0.5) * 4,
    dy: (Math.random() - 0.5) * 4,
    color: `hsl(${Math.random() * 360}, 100%, 50%)`
  };
  balls.push(newBall);
});

完整示例整合 将上述代码片段组合即可创建交互式小球动画系统,支持多球物理模拟和用户交互。

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

相关文章

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

原生js实现轮播图

原生js实现轮播图

基本结构搭建 HTML部分需要包含轮播图容器、图片列表及导航按钮。结构示例如下: <div class="slider-container"> <div class="slid…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现乘法

js实现乘法

实现乘法运算的方法 在JavaScript中实现乘法运算可以通过多种方式完成,以下列举几种常见方法: 基础运算符 直接使用乘法运算符*是最简单的方式: let result = 3 * 5; //…

js实现放大缩小

js实现放大缩小

使用 CSS transform 实现缩放 通过修改元素的 transform 属性实现平滑缩放效果。CSS 的 scale() 函数可以轻松调整元素大小。 const element = docu…

vue.js 实现grid

vue.js 实现grid

Vue.js 实现 Grid 布局 Vue.js 可以通过多种方式实现 Grid 布局,以下是几种常见的方法: 使用 CSS Grid 布局 CSS Grid 是一种强大的布局系统,可以直接在 Vu…