html实现雨滴

实现代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <style>
  body {
	overflow: hidden;
	background: black;
}
  </style>
 </HEAD>

 <BODY>  
<canvas id="canvas-club"></canvas>
 <script>
  var c = document.getElementById("canvas-club");
var ctx = c.getContext("2d");
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;
var clearColor = 'rgba(0, 0, 0, .1)';
var max = 30;
var drops = [];

function random(min, max) {
    return Math.random() * (max - min) + min;
}

function O() {}

O.prototype = {
	init: function() {
		this.x = random(0, w);
		this.y = 0;
		this.color = 'hsl(180, 100%, 50%)';
		this.w = 2;
		this.h = 1;
		this.vy = random(4, 5);
		this.vw = 3;
		this.vh = 1;
		this.size = 2;
		this.hit = random(h * .8, h * .9);
		this.a = 1;
		this.va = .96;
	},
	draw: function() {
		if (this.y > this.hit) {
			ctx.beginPath();
			ctx.moveTo(this.x, this.y - this.h / 2);

			ctx.bezierCurveTo(
				this.x + this.w / 2, this.y - this.h / 2,
				this.x + this.w / 2, this.y + this.h / 2,
				this.x, this.y + this.h / 2);

			ctx.bezierCurveTo(
				this.x - this.w / 2, this.y + this.h / 2,
				this.x - this.w / 2, this.y - this.h / 2,
				this.x, this.y - this.h / 2);

			ctx.strokeStyle = 'hsla(180, 100%, 50%, '+this.a+')';
			ctx.stroke();
			ctx.closePath();
			
		} else {
			ctx.fillStyle = this.color;
			ctx.fillRect(this.x, this.y, this.size, this.size * 5);
		}
		this.update();
	},
	update: function() {
		if(this.y < this.hit){
			this.y += this.vy;
		} else {
			if(this.a > .03){
				this.w += this.vw;
				this.h += this.vh;
				if(this.w > 100){
					this.a *= this.va;
					this.vw *= .98;
					this.vh *= .98;
				}
			} else {
				this.init();
			}
		}
		
	}
}

function resize(){
	w = c.width = window.innerWidth;
	h = c.height = window.innerHeight;
}

function setup(){
	for(var i = 0; i < max; i++){
		(function(j){
			setTimeout(function(){
				var o = new O();
				o.init();
				drops.push(o);
			}, j * 200)
		}(i));
	}
}


function anim() {
	ctx.fillStyle = clearColor;
	ctx.fillRect(0,0,w,h);
	for(var i in drops){
		drops[i].draw();
	}
	requestAnimationFrame(anim);
}


window.addEventListener("resize", resize);

setup();
anim();
  </script>
 </BODY>
</HTML>

实现结果:

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你实现这个效果。首先,你需要一个画布来显示雨滴的下落效果。你可以使用HTML5的Canvas元素来创建画布。接下来,你可以使用JavaScript来处理按下空格键后雨滴的垂直下落。 以下是一个简单的实现示例: HTML代码: ```html <!DOCTYPE html> <html> <head> <title>Raindrops</title> <style> #canvas { border: 1px solid black; } </style> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script src="script.js"></script> </body> </html> ``` JavaScript代码(script.js): ```javascript window.onload = function() { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var raindrops = []; function Raindrop(x, y, speed) { this.x = x; this.y = y; this.speed = speed; this.update = function() { this.y += this.speed; if (this.y > canvas.height) { this.y = -10; // 重置雨滴位置 } }; this.draw = function() { ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(this.x, this.y + 10); ctx.strokeStyle = "blue"; ctx.stroke(); }; } function createRaindrop() { var x = Math.random() * canvas.width; var y = Math.random() * -200; // 雨滴的初始位置在画布上方 var speed = Math.random() * 5 + 2; // 雨滴的下落速度 var raindrop = new Raindrop(x, y, speed); raindrops.push(raindrop); } function update() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < raindrops.length; i++) { var raindrop = raindrops[i]; raindrop.update(); raindrop.draw(); } requestAnimationFrame(update); } document.addEventListener("keypress", function(event) { if (event.code === "Space") { createRaindrop(); } }); update(); }; ``` 这段代码创建了一个400x400像素的画布,按下空格键会在画布上方创建一个垂直下落的蓝色雨滴。每个雨滴都会以不同的速度下落,当雨滴达到画布底部时,它会重新回到画布顶部。 你可以在浏览器中运行这段代码,按下空格键体验雨滴垂直下落的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值