canvas文字雨滴效果

											canvas文字雨滴效果
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>文字瀑布</title>
</head>
<body>
	<div id="test"></div>
	<br />
	<!-- <input onclick="operateAnimation(this)" id="operate" type="button" value="暂停"> -->
	<input onclick="operateAnimation(this)" type="button" value="暂停" id="operate">
	<br />
	<canvas id="canvas" style="background: #000;"></canvas>
</body>
<script type="text/javascript">
	var canvas = document.getElementById("canvas")
	var content = canvas.getContext("2d")
	var W = document.innerWidth||document.documentElement.cilenWidth||document.body.clientWidth;
	
	var H = document.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(){
		
		content.fillStyle = "rgba(0,0,0,0.05)";
		
		content.fillRect(0,0,W,H)
		
		content.fillStyle= "#ff00ff"
		
		for (var i=0;i<colunms;i++) {
			
			var index = Math.floor(Math.random()*str.length)
			
			var x= i*fontSize
			
			var y= drops[i]*fontSize
			
			content.fillText(index,x,y)
			
				if(y >= canvas.height && Math.random() > 0.99){
				drops[i] = 0;
			 }
			drops[i]++
		}
	};
	
	draw()
	
	var intervalId = setInterval(draw,30)
	
	function operateAnimation(objBtn){
		
		var operate = document.getElementById("operate")
		
		if(objBtn.value=="开始"){
			
			objBtn.value="暂停"
			
			intervalId = setInterval(draw,30)
			
		}else{
			
			objBtn.value="开始"
			
			clearInterval(intervalId)
			
		}
		return false
	}
	
</script>
</html>

以上是实现canvas雨滴效果的代码,如有问题请联系小编!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现骇客帝国中的文字效果,可以借助HTML5 canvas和一些JavaScript代码来实现。下面是一个示例代码: ```html <!DOCTYPE html> <html> <head> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 获取canvas元素 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 设置canvas宽高 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 定义文字样式 const fontSize = 16; ctx.font = `${fontSize}px Courier`; ctx.fillStyle = '#0F0'; // 定义文本内容 const text = 'Welcome to the Matrix'; // 定义文本的起始位置 let x = (canvas.width - ctx.measureText(text).width) / 2; let y = canvas.height / 2; // 定义绘制文本的间隔 const interval = 100; // 定义控制参数 let currentCharIndex = 0; let lastTime = performance.now(); function draw() { // 计算时间间隔 const currentTime = performance.now(); const deltaTime = currentTime - lastTime; // 控制文字逐个显示的速度 if (deltaTime > interval) { lastTime = currentTime; // 清除布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制文字 for (let i = 0; i < currentCharIndex; i++) { const char = text.charAt(i); ctx.fillText(char, x, y + (i * fontSize)); } // 更新当前字符索引 currentCharIndex++; // 检测是否显示完所有字符 if (currentCharIndex > text.length) { currentCharIndex = 0; } } // 递归调用draw函数 requestAnimationFrame(draw); } // 启动动 draw(); </script> </body> </html> ``` 这段代码会在页面上创建一个全屏的canvas元素,并通过逐个显示字符的方式实现骇客帝国中的文字效果。你可以根据需要修改文本内容、样式和动参数来实现自己想要的效果。希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值