{
    分享网正式开通,我们为大家提供免费资源,欢迎大家踊跃投稿!

HTML5 Canvas高楼大厦城市建筑剪影动画特效

这是一款简约扁平风格的HTML5 Canvas高楼大厦城市建筑剪影动画特效,视差动画效果显得很有层次感。


js代码

<script>
;(function() {
  
  'use strict'
  
  var c = document.getElementById('c');
  var ctx = c.getContext('2d');
  var w = c.width = window.innerWidth;
  var h = c.height = window.innerHeight;
  var cx = w / 2;
  var cy = h / 2;
  var Box = function(x, y, vx, color) {
    this.color = color;
    this.vx = vx;
    this.x = x;
    this.y = y;
    this.w = 10 + Math.random() * 50;
    this.h = 5 + Math.random() * 300;
  };
  Box.prototype = {
    constructor: Box,
    update: function() {
      this.x += this.vx;
      if(this.x < -this.w / 2) {
        this.x = w + this.w / 2;
      }
    },
    render: function(ctx) {
      ctx.save();
      ctx.fillStyle = this.color;
      ctx.translate(this.x, this.y);
      ctx.fillRect(-this.w/2, -this.h, this.w, this.h);
      ctx.restore();
    }
  };
  
  var ctr = 50;
  var boxes = [];
  var boxes2 = [];
  var boxes3 = [];
  var box; 
  var speed = -5;
  
  for(var i = 0; i < ctr; i++) {
    box = new Box(Math.random() * w, h, speed * 0.5, '#e5e5e5');
    boxes.push(box);
  }
  for(var i = 0; i < ctr; i++) {
    box = new Box(Math.random() * w, h, speed * 0.8, '#cccccc');
    boxes2.push(box);
  }  
  for(var i = 0; i < ctr; i++) {
    box = new Box(Math.random() * w, h, speed, '#999999');
    boxes3.push(box);
  }    
  
  requestAnimationFrame(function loop() {
    requestAnimationFrame(loop);
    ctx.clearRect(0, 0, w, h);
    ctx.globalAlpha = 0.9;
    for(var i = 0, len = boxes.length; i < len; i++) {
      box = boxes[i];
      box.update();
      box.render(ctx);
    }
    for(var i = 0, len = boxes2.length; i < len; i++) {
      box = boxes2[i];
      box.update();
      box.render(ctx);
    }
    for(var i = 0, len = boxes3.length; i < len; i++) {
      box = boxes3[i];
      box.update();
      box.render(ctx);
    }    
  });
  
})();
</script>


在线预览
资源均来自第三方,谨慎下载,前往第三方网站下载


米微资源分享网 , 版权所有丨本站资源仅限于学习研究,严禁从事商业或者非法活动!丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:HTML5 Canvas高楼大厦城市建筑剪影动画特效
喜欢 ()分享 (0)