原生js实现海底海草鼠标交互效果

代码语言:html

所属分类:动画

代码描述:原生js实现海底海草鼠标交互效果

代码标签: 海底 海草 鼠标 交互 效果

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

<!DOCTYPE html>
<html lang="en">
<head>

<style>
* {
  font-family: 'arial black', Helvetica, verdana, monospace, sans-serif;
  letter-spacing: 0.2rem;
  margin: 0;
  padding: 0;
  color: #FFF;
  overflow: hidden;
}

body {
  position: relative;
}

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

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

<script>
/*
* File Name / gardenEel.js
* Created Date / Aug 12, 2020
* Aurhor / Toshiya Marukubo
* Twitter / https://twitter.com/toshiyamarukubo
*/

(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 = null;
    var mouseY = null;
    var shapeNum = 100;
    var shapes = [];
    var bubbleNum = 50;
    var bubbles = [];
    var rad = Math.PI * 2 / 12;
    var style = {
      black: 'black',
      white: 'white',
      lineWidth: 4 };


    if (X < 768) {
      shapeNum = 50;
      bubbleNum = 25;
    }

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

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

    /********************
         Draw Ground
       ********************/

    function drawGround() {
      ctx.save();
      ctx.fillStyle = 'rgb(247, 237, 223)';
      ctx.beginPath();
      ctx.moveTo(0, Y);
      ctx.lineTo(0, Y - Y / 5);
      ctx.lineTo(X, Y - Y / 5);
      ctx.lineTo(X, Y);
      ctx.closePath();
      ctx.fill();
      ctx.restore();
    }

    /********************
        Bubble
      ********************/

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

    Bubble.prototype.init = function (x, y, i) {
      this.x = x;
      this.y = y;
      this.r = rand(10, 50);
      this.ga = Math.random() * Math.random();
      this.v = {
        x: 0,
        y: Math.random() };

    };

    Bubble.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.fillStyle = style.white;
      ctx.globalAlpha = this.ga;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
      ctx.fill();
      ctx.restore();
    };

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

    Bubble.prototype.wrapPosition = function () {
      if (this.y < 0 - this.r) this.y = Y + this.r;
    };

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

    for (var i = 0; i < bubbleNum; i++) {
      var b = new Bubble(ctx, rand(0, X), rand(0, Y));
      bubbles.push(b);
    }

    /********************
        Shape
      ********************/

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

    Shape.prototype.init = function (x, y, i) {
      this.x = x;
      this.y = y;
      this.i = i;
      this.cx = this.x;
      this.cy = this.y;
      this.l = (Y - Y / 5 - this.y) / 3;
      if (this.l < 0) th.........完整代码请登录后点击上方下载按钮下载查看

网友评论0