原生js实现随机canvas魔幻变换效果

代码语言:html

所属分类:动画

代码描述:原生js实现随机canvas魔幻变换效果

代码标签: 随机 canvas 魔幻 变换 效果

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

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

<style>
* {
  margin: 0;
  padding: 0;
  color: #FFF;
}

canvas#canvas {
  display: block;
  background: #000;
}
  
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;
  padding: 0.8rem 0;
}

p.right {
  font-size: 0.8rem;
  position: absolute;
  left: 0;
  bottom: 0;
  padding: 1.6rem;
}

p.count {
  display: none;
}

@media screen and (min-width: 48rem) {
  
  p.count {
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    padding: 1.6rem;
  }
  p.right{
    left: initial;
    right: 0;
  }

}
</style>

</head>
<body translate="no">
<canvas id="canvas" width="347" height="298">Canvas not supported.</canvas>
<ul id="navigation">
<li>- Up to rotate left</li>
<li>Down to rotate right -</li>
</ul>
<p class="count"><input type="range" id="range" min="1" max="100" step="1" value="30"></p>
<p class="right">Scroll right to speed up -</p>

<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 range = document.getElementById('range');
    var shapeNum = range.value;
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var mouseX = X / 2;
    var mouseY = Y / 2;
    var rotateSpeed = 5;
    var rMax = Y;
    var increaseR = 2;
    var text = 'STAR';
    var selectShapeNum = rand(1, 6);

    if (X < 768) {
      rMax = Y / 2;
    }

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

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

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

    var shapes = [];

    function Shape(ctx, x, y, i, n, p) {
      this.ctx = ctx;
      this.init(x, y, i, n, p);
    }
    Shape.prototype.init = function (x, y, i, n, p) {
      this.x = x;
      this.y = y;
      this.r = i;
      this.n = n;
      this.p = 5;
      this.flg = true;
      this.a = rand(0, 360);
      this.rad = this.a * Math.PI / 180;
      this.c = {
        r: rand(0, 255),
        g: rand(0, 255),
        b: rand(0, 255) };

    };
    Shape.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.lineWidth = 0.2;
      ctx.globalCompositeOperation = 'lighter';
      ctx.strokeStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')';
      ctx.beginPath();
      ctx.translate(this.x, this.y);
      ctx.rotate(this.rad);
      ctx.translate(-this.x, -this.y);
      switch (this.n) {
        case 1:
          this.rect();
          break;
        case 2:
          this.star();
          break;
        case 3:
          this.triangle();
          break;
        case 4:
          this.line();
          break;
        case 5:
          this.circle();
          break;
        case 6:
          this.whirlpool();
          break;}

      ctx.restore();
    };
    Shape.prototype.resize = function () {
      this.x = X / 2;
      this.y = Y / 2;
    };
    Shape.prototype.rect = function () {
      text = 'SQUARE';
      ctx.rect(this.x - this.r / 2, this.y - this.r / 2, this.r, this.r);
      ctx.stroke();
    };
    Shape.prototype.star = function () {
      text = 'STAR';
      var angle = Math.PI / 5 * 4;
      for (var i = 0; i < 5; i++) {
        ctx.lineTo(Math.cos(i * angle) * this.r + this.x, Math.sin(i * angle) * this.r + this.y);
      }
      ctx.closePath();
      ctx.stroke();
    };
    Shape.prototype.triangle = function () {
      text = 'TRIANGLE';
      var angle = Math.PI / 3 * 4;
      for (var i = 0; i < 3; i++) {
        ctx.lineTo(Math.cos(i * angle) * this.r + this.x, Math.sin(i * angle) * this.r + this.y);
      }
      ctx.closePath();
      ctx.stroke();
    };
    Shape.prototype.line = function () {
      text = 'LINE';
      ctx.moveTo(this.x, this.y);
      ctx.lineTo(Math.cos(this.rad) * this.r + t.........完整代码请登录后点击上方下载按钮下载查看

网友评论0