用javascript实现数字雨

数字雨这个东西,无数人写过了,我这个是上课无聊写的版本,纯属娱乐,没有经过性能优化的版本。

和别人不太一样的是我这个版本用了ascii码,数字会变化,就是性能不佳,懒得优化。

和之前的太阳系模型、小人互殴系统一样都是拿来伪装成动态壁纸的。

<!DOCTYPE html>
<html>
<head>
	<title>number rain</title>
</head>
<body style='background-color:black;margin:0;padding:0;width:100%;height:100%;overflow:hidden;'>
<div id='div'></div>
</body>
<script type="text/javascript">
	function id(x){
		return document.getElementById(x)
	}

	function c(K){
		var t = document.createElement('div');
		t.innerHTML = String.fromCharCode(Math.round(Math.random()*128)-1);
		t.style = 'position:absolute;color:green;font-weight:bold;';
		t.style.left = 20*K + 'px';
		t.speed = 4+(Math.random()-0.5)*0.5;
		t.top = 0;
		id('div').appendChild(t);
		return t;
	}

	window.onload = function(){
		var w = window.innerWidth;
		var h = window.innerHeight;

		var nx = Math.round(w/20);
		var all = [];
		for(var j=0;j<50;j++){
			for(var i=0;i<nx;i++){
				all.push(c(i));
			}
		}

		function render(){
			for(var i=0;i<all.length;i++){
				all[i].top += all[i].speed;
				if(all[i].top>h+20){
					all[i].top = 20;
					all[i].speed = 4 + (Math.random()-0.5)*5;
				}
				all[i].style.top = all[i].top + 'px';
				if(Math.random()<0.1){
					all[i].innerHTML = String.fromCharCode(Math.round(Math.random()*128)-1);
				}
				all[i].style.color = 'rgb(0,'+Math.round((all[i].top*2)/h*255)+',0)';
			}
			requestAnimationFrame(render);
		}
		requestAnimationFrame(render);
	}

</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值