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

JavaScript实现的像素小方块图片背景动画特效代码

所属分类: 网页特效-动画效果    2023-11-26 02:32:03

JavaScript实现的像素小方块图片背景动画特效代码 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

JavaScript实现的像素小方块图片背景动画特效代码(共4个文件)

    • index.html

使用方法

(function() {
  var squares = [];
  var canvas = null;
  var lastFrame = new Date().getTime();
  var delta = 0;
  var mousePos = null;
  var debug = true;
  var click = false;
  var properties = {
    cols: 64,
    rows: 32,
    color: "#ffffff",
    altColor: "#1564a1",
    sLength: 0,
    activeAmt: 0.25,
    easeIn: 0.25
  };

  var init = function() {
    canvas = document.createElement('canvas');
   document.body.appendChild(canvas);



    canvas.width = canvas.offsetWidth;
    canvas.height = (canvas.offsetWidth / properties.cols * properties.rows);
    properties.sLength = canvas.offsetWidth / properties.cols;
    
    // Generate the Grid
    for (var r = 0; r < properties.rows; r++) {
      squares[r] = [];
      for (var c = 0; c < properties.cols; c++) {
        squares[r][c] = null;
      }
    }

    canvas.addEventListener('mousemove', function(evt) {
      mousePos = getMousePos(evt);
    });

    canvas.addEventListener('mouseout', function(evt) {
      mousePos = null;
    });
    
    canvas.addEventListener('mousedown', function(evt){
      mousePos = getMousePos(evt);
      click = true;
    });
  }

  var step = function() {
    delta = (new Date().getTime() - lastFrame) / 1000;
    lastFrame = new Date().getTime();

    // Spawn new Squares
    for (var i = 0; i < Math.ceil(properties.cols * delta); i++) {
      var c = Math.round(Math.random() * (properties.cols - 1));
      var r = Math.round(Math.random() * (properties.rows - 1));
      if (squares[r][c] == null) {
        var duration = Math.round(5 * (Math.random() + 0.5));
        squares[r][c] = new Square(c, r, duration);
      }
    }
    
    // Spawn squares under the mouse
    if (mousePos != null) {
      var col = Math.round((mousePos.x - properties.sLength / 2) / canvas.width * properties.cols);
      var row = Math.round((mousePos.y - properties.sLength / 2) / canvas.height * properties.rows);
      
      if(click){
        squares[row][col] = new Square(col, row, 5, false, "#f00");
        click = false;
      } else if(squares[row][col]) {
        squares[row][col].reset();
      } else {
        squares[row][col] = new Square(col, row, 10, false)
      }
    }

    // Remove old squares
    for (var r = 0; r < properties.rows; r++) {
      for (var c = 0; c < properties.cols; c++) {
        if (squares[r][c] != null && squares[r][c].life <= 0) {
          squares[r][c] = null;
        }
      }
    }

    render();

    requestAnimationFrame(step);
  }

  var render = function() {
    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (var r = 0; r < properties.rows; r++) {
      for (var c = 0; c < properties.cols; c++) {
        if (squares[r][c] != null) {
          squares[r][c].step(ctx);
        }
      }
    }
  }

  function getMousePos(evt) {
    var rect = canvas.getBoundingClientRect();

    return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
    };
  }

  init();
  requestAnimationFrame(step);

  // Helper Function
  var getActiveNum = function() {
    var i = 0;
    for (var r = 0; r < properties.rows; r++) {
      for (var c = 0; c < properties.cols; c++) {
        if (squares[r][c] != null) {
          i++;
        }
      }
    }
    return i;
  }

  var Square = function(c, r, life, animIn, color) {
    
    if(typeof(animIn) == "undefined"){
      animIn = true;
      opacity = 0;
    } else {
      animIn = false;
      opacity = 1;
    }
    
    this.col = c;
    this.row = r;
    this.life = life;
    this.color = color ? color : properties.color;
    this.initialLife = life;
    this.animIn = animIn;
    this.opacity = opacity;

    this.step = function(ctx) {
      this.render(ctx);
      if (this.animIn) { //this.animIn){
        this.opacity = this.opacity +  (1 / properties.easeIn * delta);
        if (this.opacity >= 1) {
          this.opacity = 1;
          this.animIn = false;
        }
      } else {
        this.life = this.life - delta;
        if (this.life < 0) {
          this.life = 0;
        }
        this.opacity = Math.round(1 * (this.life / this.initialLife) * 1000) / 1000;
      }
    };

    this.render = function(ctx) {
      var l = properties.sLength;
      ctx.globalAlpha = this.opacity;
      ctx.fillStyle =  this.color;
      ctx.fillRect(this.col * l, this.row * l, l, l);

      if (debug == true) {
        ctx.globalAlpha = 1;
        ctx.fillStyle = "#000";
        ctx.font = "12px Arial";

       // ctx.fillText(this.col, this.col * l + 2, this.row * l + 12);
       //  ctx.fillText(this.row, this.col * l + l - 14, this.row * l + 12);
        // ctx.fillText(Math.round(this.life), this.col * l + 2, this.row * l + (l / 2) + 10);
       //  ctx.fillText(this.initialLife, this.col * l + 2, this.row * l + l - 4);
       //  ctx.fillText(delta, this.col * l + l - 24, this.row * l + (l / 2) + 10);
       //  ctx.fillText(this.opacity, this.col * l + l - 24, this.row * l + +l - 4);
      }
    };
    
    this.reset = function(){
      this.opacity = 1;
      this.life = this.initialLife;
    };
  }
})();

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

CSS3模拟一年四季交替动画特效

一款季节交替变换动画效果,春夏秋冬各个季节特有的画面,非常有意思哈。
  动画效果
 419  0

纯CSS3实现的大象走路动画特效

一款纯CSS3灰色大象行走动画,模拟人物行走姿态,身体上下缓缓起伏,带阴影效果。
  动画效果
 882  0

js鼠标悬停网格卡片触发堆叠动画特效代码

一款悬停卡片触发的堆叠动画特效,它使用了CSS平移和旋转变换使卡片等距堆叠效果,鼠标悬停于卡片上即可触发效果。
  动画效果
 4401  0

jquery基于CSS3打开动画全屏站点搜索框效果

一款响应式动画全屏搜索插件,非常棒的特效!
  动画效果
 6306  0

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

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