科幻旋转机械螺母动画效果

代码语言: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 mouseX = X / 2;
    var mouseY = Y / 2;
    var color = 'rgb(71, 255, 255)';

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

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

    /********************
         Text
       ********************/

    var text = '#9';
    var textSize = '56px';
    function drawText() {
      ctx.save();
      ctx.fillStyle = color;
      ctx.font = textSize + " impact";
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.fillText(text, X / 2, Y / 2);
      ctx.restore();
    }

    /********************
        Line
      ********************/

    var linesNum = 5;
    var lines = [];

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

    Line.prototype.init = function (x, y) {
      this.x = x;
      this.y = y;
      this.c = color;
      this.l = rand(10, 50);
      this.lw = 1;
      this.v = {
        x: rand(-5, 5) * Math.random(),
        y: rand(-5, 5) * Math.random() };

    };

    Line.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.lineWidth = this.lw;
      ctx.strokeStyle = this.c;
      ctx.beginPath();
      ctx.moveTo(0, this.y);
      ctx.lineTo(X, this.y);
      ctx.stroke();
      ctx.lineWidth = this.lw;
      ctx.beginPath();
      ctx.moveTo(this.x, 0);
      ctx.lineTo(this.x, Y);
      ctx.stroke();
      ctx.restore();
    };

    Line.prototype.updatePosition = function () {
      this.x += this.v.x;
      this.y += this.v.y;
    };

    Line.prototype.wrapPosition = function () {
      if (this.x < 0) this.x = X;
      if (this.x > X) this.x = 0;
      if (this.y < 0) this.y = Y;
      if (this.y > Y) this.y = 0;
    };

    Line.prototype.updateParams = function () {
      this.l -= 0.1;
      if (this.l < 0) {
        this.v.x = rand(-5, 5) * Math.random();
        this.v.y = rand(-5, 5) * Math.random();
        this.l = rand(10, 50);
      }
    };

    Line.prototype.render = function () {
      this.updatePosition();
      this.wrapPosition();
      this.updateParams();
      this.draw();
    };

    for (var i = 0; i < linesNum; i++) {
      var line = new Line(ctx, rand(0, X), rand(0, Y));
      lines.push(line);
    }

    /********************
        Circle
      ********************/

    var circleNum = 3;
    var circles = [];
    var radius = 80;
    var diff = radius / 100;
    var lw = 5;
    var blur = 10;

    if (X < 768) {
      radius = 50;
      textSize = '32px';
      lw = 3;
    }

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

    Circle.prototype.init = function (x, y, r, w) {
      this.x = x;
      this.y = y;
      this.r = r;
      this.w = w;
      this.c = color;
    };

    Circle.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.lineWidth = this.w;
      ctx.strokeStyle = this.c;
      ctx.shadowColor = color;
      ctx.shadowBlur = blur;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
      ctx.stroke();
      ctx.restore();
    };

    Circle.prototype.resize = function () {
      this.x = X / 2;
      this.y = Y / 2;
    };

    Circle.prototype.render = function () {
      this.draw();
    };

    for (var i = 0; i < 2; i++) {
      var circle = new Circle(ctx, mouseX, mouseY, radius + i * 10, lw);
      circles.push(circle);
    }
    for (var i = 0; i < 2; i++) {
      var circle = new Circle(ctx, mouseX, mouseY, radius * 2 + i * 10, lw);
      circles.push(circle);
    }
    for (var i = 0; i < 1; i++) {
      var circle = new Circle(ctx, mouseX, mouseY, radius * 1.55, lw * 5);
      circles.push(circle);
    }
   .........完整代码请登录后点击上方下载按钮下载查看

网友评论0