原生js实现摩天轮旋转动画效果
代码语言:html
所属分类:动画
代码描述:原生js实现摩天轮旋转动画效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> * { margin: 0; padding: 0; font-size: 1rem; } canvas#canvas { display: block; background: #000; background: #1a2a6c; /* fallback for old browsers */ background: -webkit-linear-gradient(to top, #fdbb2d, #b21f1f, #1a2a6c); /* Chrome 10-25, Safari 5.1-6 */ background: linear-gradient(to top, #fdbb2d, #b21f1f, #1a2a6c); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ } ul#navigation { display: none; } @media screen and (min-width: 48rem) { ul#navigation { display: block; 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; } } </style> </head> <body translate="no"> <canvas id="canvas">Canvas not supported.</canvas> <ul id="navigation"> <li>Scroll up, down and click.</li> </ul> <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 ********************/ var ctx = canvas.getContext('2d'); var X = canvas.width = window.innerWidth; var Y = canvas.height = window.innerHeight; var mouseX = null; var mouseY = null; var ferrisWheels = []; var ferrisWheelNum = 1; var splitNum = 16; var splitAn = 360 / splitNum; var wheelRadius = 200; var baloonMax = 100; if (X < 768) { wheelRadius = 100; baloonMax = 50; } /******************** Animation ********************/ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (cb) { setTimeout(cb, 17); }; /******************** FerrisWheel ********************/ function FerrisWheel(ctx, x, y) { this.ctx = ctx; this.init(x, y); } FerrisWheel.prototype.init = function (x, y) { this.x = x; this.y = y; this.r = wheelRadius; this.lw = 8; this.a = 45; this.rad = this.a * Math.PI / 180; this.v = { x: 0, y: 0 }; this.c = { r: rand(1, 1), g: rand(1, 1), b: rand(1, 1) }; }; FerrisWheel.prototype.draw = function () { var ctx = this.ctx; // base ctx.save(); ctx.lineWidth = this.lw; ctx.strokeStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')'; ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(this.x + this.r, Y); ctx.stroke(); ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(this.x - this.r, Y); ctx.stroke(); ctx.restore(); // circle ctx.save(); ctx.lineWidth = this.lw; ctx.strokeStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')'; for (var i = 1; i < 3; i++) { ctx.beginPath(); ctx.arc(this.x, this.y, this.r * i, 0, Math.PI * 2, false); ctx.stroke(); } ctx.restore(); // pillar ctx.save(); ctx.lineWidth = this.lw; ctx.strokeStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')'; ctx.translate(this.x, this.y); ctx.rotate(this.rad); ctx.translate(-this.x, -this.y); for (var i = 0; i < splitNum; i++) { ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(Math.cos(splitAn * Math.PI / 180 * i) * this.r * 3 + this.x, Math.sin(splitAn * Math.PI / 180 * i) * this.r * 3 + this.y); ctx.stroke(); } ctx.restore(); // gondola ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.rad); ctx.translate(-this.x, -this.y); for (var i = 0; i < splitNum; i++) { ctx.fillStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')'; ctx.beginPath(); ctx.arc(Math.cos(splitAn * Math.PI / 180 * i) * this.r * 3 + this.x, Math.sin(splitAn * Math.PI / 180 * i) * this.r * 3 + this.y, this.r / 3, 0, Math.PI * 2, false); ctx.fill(); ctx.fillStyle = 'rgb(' + this.c.r * 50 + ', ' + this.c.g * 50 + ', ' + this.c.b * 50 + ')'; ctx.beginPath(); ctx.arc(Math.cos(splitAn * Math.PI / 180 * i) * this.r * 3 + this.x, M.........完整代码请登录后点击上方下载按钮下载查看
网友评论0