限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

js基于canvas碰碰球动画特效代码

所属分类: 网页特效-动画效果    2024-03-21 01:25:57

js基于canvas碰碰球动画特效代码 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js基于canvas碰碰球动画特效代码(共1个文件)

    • index.html

使用方法

      var ctx = document.getElementById("myCanvas").getContext("2d");
      var lastframe = 0;

      var Box = {
        x: 1,
        y: 1,
        width: myCanvas.width - 2,
        height: myCanvas.height - 2,
      };

      class Ball {
        undefined;
        constructor(x, y, r, speed) {
          undefined;
          this.x = x;
          this.y = y;
          this.r = r;
          this.xdir = 1;
          this.ydir = 1;
          this.speed = speed;
        }
        update(dt) {
          undefined;
          this.x += dt * this.speed * this.xdir;
          this.y += dt * this.speed * this.ydir;
          if (this.y + this.r > Box.height + Box.y) {
            undefined;
            this.ydir = -1;
          }
          if (this.x + this.r > Box.width + Box.x) {
            undefined;
            this.xdir = -1;
          }
          if (this.y - this.r < Box.y) {
            undefined;
            this.ydir = 1;
          }
          if (this.x - this.r < Box.x) {
            undefined;
            this.xdir = 1;
          }
        }
      }
      // 球体大小运动速度
      const ball1 = new Ball(90, 120, 80, 700);
      const ball2 = new Ball(60, 180, 120, 360);

      function collide(b1, b2) {
        undefined;
        dist = Math.sqrt(
          (b1.y - b2.y) * (b1.y - b2.y) + (b1.x - b2.x) * (b1.x - b2.x)
        );
        if (dist < b1.r + b2.r) return 1;
        return 0;
      }

      function main(tframe) {
        undefined;
        // 请求动画帧
        window.requestAnimationFrame(main);
        //document.write(tframe)
        //更新和渲染游戏
        update(tframe);
        render();
      }

      function update(tframe) {
        undefined;
        var dt = (tframe - lastframe) / 1000;
        lastframe = tframe;

        ball1.update(dt);
        ball2.update(dt);
        if (collide(ball1, ball2)) {
          temp = ball1.xdir;
          ball1.xdir = ball2.xdir;
          ball2.xdir = temp;
          temp = ball1.ydir;
          ball1.ydir = ball2.ydir;
          ball2.ydir = temp;
        }
      }

      function draw_ball(b) {
        undefined;
        ctx.fillStyle = "#ffffff";
        ctx.beginPath();
        ctx.arc(b.x, b.y, b.r, 0, 2 * Math.PI, 1);
        ctx.fill();
      }

      function drawFrame() {
        undefined;
        // 绘制背景和边框
        ctx.fillStyle = "gray";
        ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);

        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 2;
        ctx.strokeRect(Box.x, Box.y, Box.width, Box.height);
      }

      function render() {
        undefined;
        drawFrame();
        draw_ball(ball1);
        draw_ball(ball2);
      }

      main(0);

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 动画效果

jquery游动的鱼群canvas动画特效代码

一个款鱼群canvas动画效果,使用了create.js库将html5画布元素转换为可配置参数游泳速度,减速,弧度,转动频率,鱼的数量,鱼大小,游泳周期,颜色等交互式的鱼群动画。
  动画效果
 7259  0

jquery+canvas搜狗指数动画背景热点关键词

热点关键词由远到近的动画运动作为背景,带搜索框,很实用!
  动画效果
 9409  0

jquery非常丰富漂亮的动态文字特效

一款动感的文字动画特效,会让你的网站即刻生动起来,带多种效果!
  动画效果
 4559  0

jquery打字机文本文字动画插件

一款迷你文本动画插件,可自定义文本文字,可设定:速度、颜色、间隔时间等。
  动画效果
 4318  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论