前端特效04:使用js实现的奇幻水母效果

今天,小编将与大家分享web前端特效荟萃系列第四期,喜欢把玩儿炫酷效果的小伙伴快快看过来^_^ ,希望大家喜欢呦~






第四期,给大家分享一个使用js实现的奇幻水母效果。相关代码如下:


<!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 pointer = 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>
在线演示:Here~ 


今天的分享就是这些啦,这里是igeekbar,希望可以给各位geek小伙伴们带来帮助呦~~~如果你制作出了或收藏了哪些酷酷的前端特效,赶快留言分享给我们呦~~~


Geek豪哥   iGeekBar社区大管家

iGeekBar创始人之一,热爱科技与设计,正在探索研究数据科学的女猿,最爱勾搭极客帅哥美女的逗比疯姑娘~~
     扫一扫立刻加入iGeekBar会员QQ群(545980198)
    和更多iG客会员交流分享吧~