前端特效:使用js实现奇幻水母效果
2018-01-25 16:29
525 查看
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cloth</title> <style> html { overflow: hidden; touch-action: none; content-zooming: none; } body { position: absolute; margin: 0; padding: 0; width: 100%; height: 100%; background: #000; } canvas { width: 100%; height: 100%; background: #000; position: absolute; } </style> </head> <body> <div id="container"> </div> <script type="text/javascript" src="http://cdn.gbtags.com/jquery/1.11.1/jquery.min.js"></script> <script> ! function(Math) { "use strict"; var Arm = function(x, y, num, len) { this.length = len; this.num = num; this.x = new Float32Array(num); this.y = new Float32Array(num); this.sx = ((resolutionX / 2) - x) / 600; this.sy = ((resolutionY / 2) - y) / 600; for (var i = 0; i < num; i++) { this.x[i] = x; this.y[i] = y; y += len; } }; Arm.prototype.ik = function(p0, p1) { var xDis = this.x[p0] - this.x[p1]; var yDis = this.y[p0] - this.y[p1]; var dist = Math.sqrt(xDis * xDis + yDis * yDis); this.x[p0] = this.x[p1] + xDis / dist * this.length; this.y[p0] = this.y[p1] + yDis / dist * this.length; }; Arm.prototype.anim = function() { var i = this.num - 1; this.x[i] += (pointer.x - this.x[i]) * 0.5; this.y[i] += (pointer.y - this.y[i]) * 0.5; while (--i) this.ik(i, i + 1); i = 0; while (++i < this.num) { this.ik(i, i - 1); this.x[i] -= this.sx; this.y[i] += this.sy; } ctx.beginPath(); i = 0; while (++i < this.num) ctx.lineTo(this.x[i], this.y[i]); ctx.strokeStyle = "#654"; ctx.stroke(); }; var run = function() { requestAnimationFrame(run); ctx.clearRect(0, 0, resolutionX, resolutionY); ctx.globalCompositeOperation = 'lighter'; for (var i = 0, len = arms.length; i < len; i++) { arms[i].anim(); } }; var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); document.body.appendChild(canvas); var resolutionX = canvas.width = canvas.offsetWidth / 1; var resolutionY = canvas.height = canvas.offsetHeight / 1; var pointer = { x: resolutionX / 2, y: resolutionY / 2, move: function(e) { var touchMode = e.targetTouches, pointer; if (touchMode) { e.preventDefault(); pointer = touchMode[0]; } else poi a390 nter = e; this.x = pointer.clientX * resolutionX / canvas.offsetWidth; this.y = pointer.clientY * resolutionY / canvas.offsetHeight; } }; window.addEventListener('mousemove', pointer.move.bind(pointer), false); canvas.addEventListener('touchmove', pointer.move.bind(pointer), false); var arms = []; var r = Math.min(resolutionX, resolutionY) / 2.5; for (var i = 0; i < 360; i += 1) { arms.push( new Arm( 1 + resolutionX * 0.5 + Math.cos(i * Math.PI / 180) * r, 1 + resolutionY * 0.5 + Math.sin(i * Math.PI / 180) * r, 50, r / 20 ) ); } run(); }(Math); </script> </body> </html>
效果展示及转载来源:http://igeekbar.com/igclass/code/5f987f74-ac57-4cf6-846e-3adf682a24d5.htm
相关文章推荐
- 使用js实现的奇幻水母效果
- ajax实例:如何使用json+ajax的方法实现类似前端特效tab切换效果
- 使用MVC中的HtmlHelper对象的ActionLink方法,实现一个Javascript:void(0)效果,去调用一个JS方法
- 使用js实现有光源扫描图片效果
- 使用js实现文字波浪效果
- js实现运动logo图片效果及运动元素对象sportBox使用方法
- JS:目录跟随定位效果的实现及锚点中使用name和id定位的区别。
- 使用JS实现类似屏幕保护效果
- 使用 JS + CSS 实现的公式编辑效果
- 使用Jquery分页插件jquery.pagination.js 实现无刷新分页效果
- 使用js实现OICQ菜单效果
- 两种方法使用js读写cookie实现一个底部广告浮层效果
- JS特效:用鼠标事件实现图片的渐显效果
- JS实现的流动边框效果的表格特效z
- js实现运动logo图片效果及运动元素对象sportBox使用方法
- 使用js实现滑动页面效果,很漂亮
- .NET中使用js实现百度搜索下拉提示效果[不是局部刷新,呜呜。。]
- 使用jquery实现图文切换效果另加特效
- JS 特效 用js实现radio选择钮效果
- .NET中使用js实现百度搜索下拉提示效果[不是局部刷新,呜呜。。]