jquery实现canvas水母跟随鼠标动画效果代码
代码语言:html
所属分类:动画
代码描述:jquery实现canvas水母跟随鼠标动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!doctype html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script> <style> body { background: #333; } canvas { background: black; }</style> </head> <body> <script> var App = {}; jQuery(document).ready(function() { // Setup canvas and app App.setup(); // Launch animation loop App.frame = function() { App.update(); window.requestAnimationFrame(App.frame); }; App.frame(); jQuery('canvas#ourCanvas').on('click', function(event) { App.hasUserClicked = !App.hasUserClicked; }); jQuery('canvas#ourCanvas').on('mousemove', function(event) { App.target.x = event.pageX; App.target.y = event.pageY; }); }); App.setup = function() { // Setup canvas and get canvas context var canvas = document.createElement('canvas'); canvas.height = window.innerHeight; canvas.width = window.innerWidth; canvas.id = 'ourCanvas'; document.body.appendChild(canvas); this.ctx = canvas.getContext('2d'); this.width = canvas.width; this.height = canvas.height; // Define a few useful elements this.stepCount = 0; this.hasUserClicked = false; this.xC = canvas.width / 2; this.yC = canvas.height / 2; this.target = { x: this.xC, y: this.yC, radius: 20 }; this.armsPop = 20; this.particlesPerArm = 15; // Create initial targets and arms this.arms = []; for (var i = 0; i < this.armsPop; i++) { this.arms.push([]); } // Fill initial arms this.initialBirth(); // Some forces this.gravity = -1; this.springStiffness = 0.5; this.viscosity = 0.1; this.isElastic = false; }; App.initialBirth = function() { for (var armIndex = 0; armIndex < this.arms.length; armIndex++) { var arm = this.arms[armIndex]; var particlesNb = 20 + Math.ceil(20 * Math.random()); for (var i = 0; i < particlesNb; i++) { var x = this.width * Math.random(); var y = this.height * Math.random(); var particle = { x: x, y: y, xLast: x, yLast: y, .........完整代码请登录后点击上方下载按钮下载查看
网友评论0