文字瀑布流

<!DOCTYPE html>
<html lang="zh">

	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>文字瀑布流</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0
			}
		</style>
	</head>

	<body>
		<div id="test"></div>
		<br>
		<input onclick="operateAnimation(this)" id="operate" type="button" value="暂停" />
		<br>
		<canvas id="canvas" style="background:#000"></canvas>
		<script type="text/javascript">
			var canvas = document.getElementById("canvas");
			var context = canvas.getContext("2d");
			var W = window.innerWidth ||
				document.documentElement.clientWidth ||
				document.body.clientWidth;
			var H = window.innerHeight ||
				document.documentElement.clientHeight ||
				document.body.clientHeight;
				
			canvas.width = W;
			canvas.height = H;
			var fontSize = 16;
			//列  
			var colunms = Math.floor(W / fontSize);
			var drops = [];
			for(var i = 0; i < colunms; i++) {
				drops.push(0);
			}
			console.log(drops);
			var str = "显示的运动字体";
			function draw() {
				context.fillStyle = "rgba(0,0,0,0.05)";
				context.fillRect(0, 0, W, H);
				context.font = "700 " + fontSize + "px  微软雅黑";
				context.fillStyle = "#00cc33";
				for(var i = 0; i < colunms; i++) {
					var index = Math.floor(Math.random() * str.length);
					var x = i * fontSize; //x坐标  
					var y = drops[i] * fontSize;
					context.fillText(index, x, y);
					if(y >= canvas.height && Math.random() > 0.99) {
						drops[i] = 0;
					}
					drops[i]++;
				}
			};
			draw();
			var intervalId = setInterval(draw, 50);

			// 开始/暂停  
			function operateAnimation(objBtn) {
				var operate = document.getElementById("operate");

				if(objBtn.value == "开始") {
					objBtn.value = "暂停";
					intervalId = setInterval(draw, 50);
				} else {
					objBtn.value = "开始";
					clearInterval(intervalId);
				}
				return false;
			}
		</script>
	</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值