当前位置:首页 > 文章咨询 > 正文内容

完整代码(网页版)

admin8个月前 (02-17)文章咨询140

要实现一个"球球刷爱心"的互动小程序,我们可以用HTML5 Canvas来创建一个网页版的demo,包含小球跟随鼠标、点击生成爱心、收集爱心计数等核心功能,以下是完整的实现代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">球球刷爱心</title>
  <style>
    * { margin: 0; padding: 0; }
    body { overflow: hidden; background: #f8f9fa; }
    canvas { display: block; }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <script>
    // 获取Canvas和上下文
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    let canvasWidth = window.innerWidth;
    let canvasHeight = window.innerHeight;
    canvas.width = canvasWidth;
    canvas.height = canvasHeight;
    // 小球类
    class Ball {
      constructor() {
        this.x = canvasWidth / 2;
        this.y = canvasHeight / 2;
        this.radius = 25;
        this.color = '#ff5252';
        this.speed = 5; // 跟随鼠标的平滑速度
      }
      update(targetX, targetY) {
        // 平滑跟随鼠标
        this.x += (targetX - this.x) / this.speed;
        this.y += (targetY - this.y) / this.speed;
      }
      draw() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
        ctx.fillStyle = this.color;
        ctx.fill();
        ctx.shadowBlur = 10;
        ctx.shadowColor = this.color;
        ctx.closePath();
      }
    }
    // 爱心类
    class Heart {
      constructor(x, y) {
        this.x = x;
        this.y = y;
        this.size = Math.random() * 15 + 10; // 随机大小
        this.color = `hsl(${Math.random() * 20 + 340}, 100%, 70%)`; // 粉色系
        this.speedY = Math.random() * 1.5 + 0.5; // 上升速度
        this.rotateSpeed = Math.random() * 0.06 - 0.03; // 旋转速度
        this.rotation = 0;
        this.life = 120; // 生命周期(帧)
      }
      update() {
        this.y -= this.speedY;
        this.rotation += this.rotateSpeed;
        this.life--;
      }
      draw() {
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.rotation);
        ctx.scale(this.size / 20, this.size / 20);
        // 绘制爱心路径
        ctx.beginPath();
        ctx.moveTo(0, 10);
        ctx.bezierCurveTo(15, -15, 30, 5, 0, 30);
        ctx.bezierCurveTo(-30, 5, -15, -15, 0, 10);
        ctx.fillStyle = this.color;
        ctx.fill();
        ctx.shadowBlur = 5;
        ctx.shadowColor = this.color;
        ctx.closePath();
        ctx.restore();
      }
    }
    // 全局变量
    let ball = new Ball();
    let hearts = [];
    let score = 0;
    let mouseX = canvasWidth / 2;
    let mouseY = canvasHeight / 2;
    // 事件监听
    canvas.addEventListener('mousemove', (e) => {
      mouseX = e.clientX;
      mouseY = e.clientY;
    });
    canvas.addEventListener('click', (e) => {
      // 每次点击生成多个爱心
      for (let i = 0; i < 3; i++) {
        hearts.push(new Heart(
          e.clientX + Math.random() * 30 - 15,
          e.clientY + Math.random() * 30 - 15
        ));
      }
    });
    window.addEventListener('resize', () => {
      canvasWidth = window.innerWidth;
      canvasHeight = window.innerHeight;
      canvas.width = canvasWidth;
      canvas.height = canvasHeight;
    });
    // 碰撞检测
    function checkCollision(ball, heart) {
      const dx = ball.x - heart.x;
      const dy = ball.y - heart.y;
      const distance = Math.sqrt(dx * dx + dy * dy);
      return distance < ball.radius + heart.size / 2;
    }
    // 渲染循环
    function animate() {
      requestAnimationFrame(animate);
      // 半透明背景(保留轨迹效果)
      ctx.fillStyle = 'rgba(255,255,255,0.15)';
      ctx.fillRect(0, 0, canvasWidth, canvasHeight);
      // 更新小球
      ball.update(mouseX, mouseY);
      ball.draw();
      // 更新爱心
      for (let i = hearts.length - 1; i >= 0; i--) {
        const heart = hearts[i];
        heart.update();
        heart.draw();
        // 碰撞检测
        if (checkCollision(ball, heart)) {
          score++;
          hearts.splice(i, 1);
        }
        // 生命周期结束移除
        if (heart.life <= 0) {
          hearts.splice(i, 1);
        }
      }
      // 绘制分数
      ctx.fillStyle = '#333';
      ctx.font = 'bold 28px Arial';
      ctx.fillText(`爱心数: ${score}`, 30, 50);
      ctx.shadowBlur = 0;
    }
    // 启动动画
    animate();
  </script>
</body>
</html>

功能说明

  1. 小球跟随:鼠标移动时,小球会平滑跟随鼠标位置
  2. 生成爱心:点击屏幕时,会在点击位置生成多个随机大小、颜色的爱心
  3. 爱心动画:爱心会向上漂浮并旋转,一段时间后自动消失
  4. 收集爱心:小球碰到爱心时,爱心会被收集,分数增加
  5. 响应式:窗口大小变化时自动调整画布尺寸

运行方式

  1. 将上述代码保存为HTML文件(如heart_ball.html)
  2. 用浏览器打开该文件即可体验

扩展优化方向

  1. 微信小程序版:将Canvas替换为微信小程序的canvas组件,调整事件监听(如touchmove代替mousemove)
  2. 增加音效:收集爱心时播放音效
  3. 爱心类型:添加不同类型的爱心(如加分更多的特殊爱心)
  4. 背景美化:添加渐变背景或动态背景
  5. 排行榜:记录最高分数并展示

这个程序通过Canvas实现了流畅的互动效果,适合作为小游戏或互动展示使用,你可以根据需求调整颜色、大小、速度等参数,让效果更符合预期。

完整代码(网页版)

标签: 网页版

相关文章

How to Say Browse Douyin in English

How to Say Browse Douyin in English

In today's digital age, Douyin has become an extremely popular short-video sharing platform. Many pe...

苹果7刷抖音的电量续航时长实测

苹果7刷抖音的电量续航时长实测

在当今社交媒体盛行的时代,抖音成为了许多人闲暇时光的娱乐首选,而对于使用苹果7的用户来说,可能会关心自己的手机在刷抖音时能坚持多久才会没电,我们就来实际测试一下苹果7刷抖音的电量消耗情况。 我们...

抖音在刷94年的那些事儿

抖音在刷94年的那些事儿

在当今社交媒体的时代,抖音无疑是一颗璀璨的明星,吸引着无数用户的目光,而其中关于抖音在刷94年相关内容的现象,也引起了不少人的关注。 当我们打开抖音,会发现有许多与94年有关的视频在不断涌现,这...

抖音评论为何会被刷掉

抖音评论为何会被刷掉

在抖音这个热门的短视频社交平台上,许多用户都有过这样的经历:自己精心撰写的评论在发布后不久就莫名其妙地不见了,仿佛被刷掉了一样,抖音评论为什么会刷掉呢? 可能是因为评论内容违反了抖音的社区规则,...

抖音刷赞被抓事件引发的思考

抖音刷赞被抓事件引发的思考

在当今社交媒体盛行的时代,抖音作为一款极具影响力的短视频平台,受到了广大用户的喜爱和追捧,与之相关的一些不正当行为也逐渐浮出水面,其中抖音刷赞被抓的事件引起了社会的广泛关注。 抖音刷赞,就是通过...

抖音粉丝增长的有效方法

抖音粉丝增长的有效方法

在当今的社交媒体时代,抖音无疑是最热门的平台之一,拥有大量的用户和创作者,许多人都希望在抖音上获得更多的粉丝,以扩大自己的影响力和知名度,抖音粉丝怎么刷呢?这里有一些合法且有效的方法可以帮助你增加抖音...