粒子跟随动画效果

代码语言:html

所属分类:粒子

代码描述:粒子跟随动画效果

代码标签: 效果

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

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

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

canvas#canvas {
  display: block;
  background: #000;
}
</style>

</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>

<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
      ********************/

    // canvas 
    var ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var flg = true;
    var mouseX = X / 2;
    var mouseY = Y / 2;

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

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

    /********************
         Particle
       ********************/

    var particleNum = 1000;
    var particles = [];
    var maxParticles = 1000;

    if (X < 768) {
      particleNum = 500;
    }

    function Particle(ctx, x, y, r) {
      this.ctx = ctx;
      this.init(x, y, r);
    }

    Particle.prototype.init = function (x, y, r) {
      this.x = x;
      this.y = y;
      this.x1 = this.x;
      this.y1 = this.y;
      this.r = r;
      this.s = Math.random();
      this.a = rand(0, 360);
      this.rad = this.a * Math.PI / 180;
      this.z = Math.random() + 1;
      this.v = {
        x: 0,
        y: 0 };

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

    };

    Particle.prototype.updatePosition = function () {
      if (flg === false) {
        var x = this.x - this.x1;
        var y = this.y - this.y1;
        var d = x * x + y * y;
        var newDist = Math.sqrt(d);
        this.v.x = x / newDist * (1 + this.s);
        this.v.y = y / newDist * (1 + this.s);
        this.x = Math.sin(this.rad) * 2 + this.x;
        this.y = Math.cos(this.rad) * 2 + this.y;
        this.x -= this.v.x;
        this.y -= this.v.y;
      } else {
        var x = this.x - mouseX;
        var y = this.y - mouseY;
        var d = x * x + y * y;
        var newDist = Math.sqrt(d);
        this.v.x = x / newDist * (1 + this.s);
        this.v.y = y / newDist * (1 + this.s);
        this.x = Math.sin(this.rad) * this.z + this.x;
        this.y = Math.cos(this.rad) * this.z + this.y;
        this.x -= this.v.x;
        this.y -= this..........完整代码请登录后点击上方下载按钮下载查看

网友评论0