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

完整代码(网页版)

admin2个月前 (02-17)文章咨询36

要实现一个"球球刷爱心"的互动小程序,我们可以用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实现了流畅的互动效果,适合作为小游戏或互动展示使用,你可以根据需求调整颜色、大小、速度等参数,让效果更符合预期。

完整代码(网页版)

标签: 网页版

相关文章

如何在抖音刷出独角兽特效

如何在抖音刷出独角兽特效

在抖音这个充满创意和乐趣的短视频平台上,各种特效为用户带来了无限的惊喜和欢乐,独角兽特效备受大家喜爱,它能让用户仿佛置身于梦幻的童话世界,拥有一只可爱的独角兽陪伴,怎样才能在抖音刷出独角兽特效呢?...

抖音刷礼物提成究竟是多少?

抖音刷礼物提成究竟是多少?

在当今的社交媒体时代,抖音无疑是最为热门的平台之一,许多用户在抖音上通过各种方式展示自己的才华,吸引了大量的粉丝关注,而粉丝们为了表达对主播的喜爱和支持,常常会通过刷礼物的方式来进行打赏,大家可能会好...

抖音快刷网,解析其背后的真相

抖音快刷网,解析其背后的真相

在当今社交媒体蓬勃发展的时代,抖音无疑是其中的佼佼者,吸引了数亿用户的关注和参与,而与之相关的一些服务也应运而生,其中就包括抖音快刷网。 抖音快刷网声称可以为用户提供快速增加抖音粉丝、点赞、评论...

工作室刷抖音是否真实可靠?

工作室刷抖音是否真实可靠?

在当今社交媒体盛行的时代,抖音作为一款极具影响力的短视频平台,吸引了无数用户和商家的关注,关于工作室刷抖音的现象也备受争议,很多人都在好奇工作室刷抖音是真的吗? 抖音平台的火爆使得其成为了众多个...

苹果刷国际版抖音的方法

苹果刷国际版抖音的方法

在苹果设备上,许多用户都想知道如何刷国际版抖音,以下是一些可行的方法。 你需要拥有一个非中国大陆地区的苹果账号,可以通过在苹果官网进行注册,在注册过程中选择其他国家或地区,按照相应的步骤填写信息...

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

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

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