使用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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值