js实现大风车和泡泡交互动画 效果
代码语言:html
所属分类:动画
代码描述:js实现大风车和泡泡交互动画 效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <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: #ABE7FF; } </style> </head> <body translate="no"> <canvas id="canvas">Canvas not supported.</canvas> <script> /* * File Name / pinwheel.js * Created Date / Aug 14, 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 bubbles = []; var bubbleNum = 150; var shapes = []; var rad = Math.PI * 2 / 4; var style = { black: 'black', white: 'white', lineWidth: 4 }; if (X < 768) { bubbleNum = 100; } /******************** Animation ********************/ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (cb) { setTimeout(cb, 17); }; /******************** Bubble ********************/ function Bubble(ctx, x, y) { this.ctx = ctx; this.init(x, y); } Bubble.prototype.init = function (x, y) { this.x = x; this.y = y; this.r = rand(20, 50); this.ga = Math.random() * Math.random() * Math.random(); this.v = { x: rand(-1, 1) * Math.random() * Math.random(), y: Math.random() }; this.random = Math.random(); this.a = rand(0, 360); this.rad = this.a * Math.PI / 180; this.as = rand(0, 360) * Math.PI / 180; }; Bubble.prototype.draw = function () { var ctx = this.ctx; ctx.save(); ctx.fillStyle = 'white'; ctx.strokeStyle = 'white'; ctx.lineCap = 'round'; ctx.lineWidth = this.r / 4; ctx.globalAlpha = this.ga; ctx.translate(this.x, this.y); ctx.rotate(this.rad); ctx.translate(-this.x, -this.y); ctx.beginPath(); ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false); ctx.fill(); ctx.globalAlpha = this.ga * 1.1; ctx.beginPath(); ctx.arc(this.x, this.y, this.r * 0.7, this.as, this.as + 1, false); ctx.stroke(); ctx.restore(); }; Bubble.prototype.updatePosition = function () { if (this.y > Y - Y / 3) { this.v.x += 0.1; this.v.y -= 0.01; } this.x += this.v.x; this.y += this.v.y; }; Bubble.prototype.updateParams = function (i) { this.a += this.random; this.rad = this.a * Math.PI / 180; }; Bubble.prototype.wrapPosition = function () { if (this.x > X + this.r / 2) { this.init(rand(0, X), rand(0 - Y / 5, 0)); } }; Bubble.prototype.render = function (i) { this.updateParams(i); this.updatePosition(); this.wrapPosition(); this.draw(); .........完整代码请登录后点击上方下载按钮下载查看
网友评论0