原生js实现光波走光动画效果

代码语言:html

所属分类:粒子

代码描述:原生js实现光波走光动画效果

代码标签: 光波 走光 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
* {
  margin: 0;
  padding: 0;
}

canvas#canvas {
  display: block;
  background: #092745;
}

ul#navigation {
  display: none;
}

@media screen and (min-width: 48rem) {

  ul#navigation {
    position: absolute;
    height: 100%;
    top: 0;
    right: 0;
    -webkit-writing-mode: vertical-rl;
        -ms-writing-mode: tb-rl;
            writing-mode: vertical-rl;
    text-align: center;
    margin-right: 1.6rem;
    font-size: 0.8rem;
  }
  ul#navigation > li {
    display: inline-block;
  }

}
</style>

</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>
<ul id="navigation">
<li>Scroll right to speed up.</li>
</ul>

<script>
(function () {
  'use strict';
  window.addEventListener('load', function () {
    var canvas = document.getElementById('canvas');

    if (!canvas || !canvas.getContext) {
      return false;
    }

    /********************
        Random Number
      ********************/

    function rand(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    }

    /********************
        Var
      ********************/

    var ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var mouseX = X / 2;
    var mouseY = Y / 2;
    var ellipses = [];
    var ellipseNum = X / 10 + 1;

    /********************
                                   Animation
                                 ********************/

    window.requestAnimationFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function (cb) {
      setTimeout(cb, 17);
    };

    /********************
         Ellipse
       ********************/

    function Ellipse(ctx, x, y, i) {
      this.ctx = ctx;
      this.init(x, y, i);
    }

    Ellipse.prototype.init = function (x, y, i) {
      this.x = x;
      this.y = y;
      this.i = i;
      this.r = Y / 40;
      this.v = {
        x: 0,
        y: 0 };

      this.c = {
        r: rand(0, 255),
        g: rand(0, 255),
        b: rand(200, 255),
        a: 1 };

      this.a = i * 2;
      this.rad = this.a * Math.PI / 180;
    };

    Ellipse.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.globalCompositeOperation = 'lighter';
      ctx.beginPath().........完整代码请登录后点击上方下载按钮下载查看

网友评论0