当前位置: 首页 > news >正文

纯JavaScript实现表白代码

纯JavaScript实现表白代码

文章目录

    • 纯JavaScript实现表白代码
      • 代码
      • 效果图

在国庆节当天,大家都在表达着对祖国的热爱,当然这也是个对自己爱的人表白的一个好机会,本文旨在使用JavaScript实现表白的代码。

代码

首先是实现的具体代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>htm5浪漫表白</title>
</head>

<body>

<script type="text/javascript">
BLUR = false;

PULSATION = true;
PULSATION_PERIOD = 600;
PARTICLE_RADIUS = 4;

/* disable blur before using blink */

BLINK = false;

GLOBAL_PULSATION = false;

QUALITY = 2; /* 0 - 5 */

/* set false if you prefer rectangles */
ARC = true;

/* trembling + blur = fun */
TREMBLING = 0; /* 0 - infinity */

FANCY_FONT = "Arial";

BACKGROUND = "#000";

BLENDING = true;

/* if empty the text will be a random number */
var TEXT;
num = 0;
TEXTArray = ["陌陌", "深爱", "某某", "直到", "永远"];

QUALITY_TO_FONT_SIZE = [10, 12, 40, 50, 100, 350];
QUALITY_TO_SCALE = [20, 6, 4, 2, 0.9, 0.5];
QUALITY_TO_TEXT_POS = [10, 20, 60, 100, 370, 280];


window.onload = function () {
	document.body.style.backgroundColor = BACKGROUND;

	var canvas = document.getElementById("canvas");
	var ctx = canvas.getContext("2d");

	var W = canvas.width;
	var H = canvas.height;

	var tcanvas = document.createElement("canvas");
	var tctx = tcanvas.getContext("2d");
	tcanvas.width = W;
	tcanvas.height = H;

	total_area = W * H;
	total_particles = 928;
	single_particle_area = total_area / total_particles;
	area_length = Math.sqrt(single_particle_area);

	var particles = [];
	for (var i = 1; i <= total_particles; i++) {
		particles.push(new particle(i));
	}

	function particle(i) {
		this.r = Math.round(Math.random() * 255 | 0);
		this.g = Math.round(Math.random() * 255 | 0);
		this.b = Math.round(Math.random() * 255 | 0);
		this.alpha = 1;

		this.x = (i * area_length) % W;
		this.y = (i * area_length) / W * area_length;

		/* randomize delta to make particles sparkling */
		this.deltaOffset = Math.random() * PULSATION_PERIOD | 0;

		this.radius = 0.1 + Math.random() * 2;
	}

	var positions = [];

	function new_positions() {

		TEXT = TEXTArray[num];

		if (num < TEXTArray.length - 1) {
			num++;
		} else {
			num = 0;
		}
		//alert(TEXT);

		tctx.fillStyle = "white";
		tctx.fillRect(0, 0, W, H)
		//tctx.fill();

		tctx.font = "bold " + QUALITY_TO_FONT_SIZE[QUALITY] + "px " + FANCY_FONT;

		//tctx.textAlign='center';//文本水平对齐方式
		//tctx.textBaseline='middle';

		//tctx.strokeStyle = "black";
		tctx.fillStyle = "#f00";
		//tctx.strokeText(TEXT,30, 50);
		tctx.fillText(TEXT, 20, 60);

		image_data = tctx.getImageData(0, 0, W, H);
		pixels = image_data.data;
		positions = [];
		for (var i = 0; i < pixels.length; i = i + 2) {
			if (pixels[i] != 255) {
				position = {
					x: (i / 2 % W | 0) * QUALITY_TO_SCALE[QUALITY] | 0,
					y: (i / 2 / W | 0) * QUALITY_TO_SCALE[QUALITY] | 0
				}
				positions.push(position);
			}
		}

		get_destinations();
	}

	function draw() {

		var now = Date.now();

		ctx.globalCompositeOperation = "source-over";

		if (BLUR) ctx.globalAlpha = 0.1;
		else if (!BLUR && !BLINK) ctx.globalAlpha = 1.0;

		ctx.fillStyle = BACKGROUND;
		ctx.fillRect(0, 0, W, H)

		if (BLENDING) ctx.globalCompositeOperation = "lighter";

		for (var i = 0; i < particles.length; i++) {

			p = particles[i];

			/* in lower qualities there is not enough full pixels for all of  them - dirty hack*/

			if (isNaN(p.x)) continue

			ctx.beginPath();
			ctx.fillStyle = "rgb(" + p.r + ", " + p.g + ", " + p.b + ")";
			ctx.fillStyle = "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.alpha + ")";

			if (BLINK) ctx.globalAlpha = Math.sin(Math.PI * mod * 1.0);

			if (PULSATION) { /* this would be 0 -> 1 */
				var mod = ((GLOBAL_PULSATION ? 0 : p.deltaOffset) + now) % PULSATION_PERIOD / PULSATION_PERIOD;

				/* lets make the value bouncing with sinus */
				mod = Math.sin(mod * Math.PI);
			} else var mod = 1;

			var offset = TREMBLING ? TREMBLING * (-1 + Math.random() * 2) : 0;

			var radius = PARTICLE_RADIUS * p.radius;

			if (!ARC) {
				ctx.fillRect(offset + p.x - mod * radius / 2 | 0, offset + p.y - mod * radius / 2 | 0, radius * mod,
					radius * mod);
			} else {
				ctx.arc(offset + p.x | 0, offset + p.y | 0, radius * mod, Math.PI * 2, false);
				ctx.fill();
			}

			p.x += (p.dx - p.x) / 10;
			p.y += (p.dy - p.y) / 10;
		}
	}

	function get_destinations() {
		for (var i = 0; i < particles.length; i++) {
			pa = particles[i];
			particles[i].alpha = 1;
			var distance = [];
			nearest_position = 0;
			if (positions.length) {
				for (var n = 0; n < positions.length; n++) {
					po = positions[n];
					distance[n] = Math.sqrt((pa.x - po.x) * (pa.x - po.x) + (pa.y - po.y) * (pa.y - po.y));
					if (n > 0) {
						if (distance[n] <= distance[nearest_position]) {
							nearest_position = n;
						}
					}
				}
				particles[i].dx = positions[nearest_position].x;
				particles[i].dy = positions[nearest_position].y;
				particles[i].distance = distance[nearest_position];

				var po1 = positions[nearest_position];
				for (var n = 0; n < positions.length; n++) {
					var po2 = positions[n];
					distance = Math.sqrt((po1.x - po2.x) * (po1.x - po2.x) + (po1.y - po2.y) * (po1.y - po2.y));
					if (distance <= 5) {
						positions.splice(n, 1);
					}
				}
			} else {
				//particles[i].alpha = 0;
			}
		}
	}

	function init() {
		new_positions();
		setInterval(draw, 30);
		setInterval(new_positions, 2000);
	}

	init();
};
</script>

<style type="text/css">
body {
	background: #000;
	text-align: center;
	font-family: "simhei"
}
canvas {
	margin: auto;
	position: absolute;
	left: 0;
	right:0;
	top: 0;
	bottom: 0;
}
</style>


<canvas id="canvas" width="1000" height="800"></canvas>

<div style="text-align:center;">
<p><a href="" title="" target="_blank"></a></p>
</div>

</body>
</html>

效果图

下面展示代码的部分效果图,完整的效果可以复制上述的代码之后运行即就可以了。
在这里插入图片描述
最后,谢谢大家的支持了。

相关文章:

  • SpringCloudGateway 学习笔记 - 搭建项目
  • MySQL学习笔记:一条SQL语句的执行过程
  • Springboot框架建立(1)
  • mongodb数据模型设计
  • Java学习笔记:SQLite数据库
  • 通过mockjs生成随机响应数据
  • VGG16-好莱坞明星识别
  • 《运营商劫持, 中间人攻击, 黑客入侵怎么办?》- HTTPS 技术反制
  • Vue项目的记录(七)
  • 【云原生 • Kubernetes】集群资源监控概述、监控平台的搭建
  • SpringCloud Stream基本使用
  • 没有CANdela,无法编辑cdd数据库文件,也能轻松完成诊断测试,立省大二十个w
  • 【Linux】基本指令 (下篇)
  • 博途PLC的模糊PID(Matlab “fuzzy“工具箱使用介绍)
  • 【Vue 开发实战】实战篇 # 45:如何构建可交互的组件文档让代码高亮的显示在页面
  • 【知识碎片】第三方登录弹窗效果
  • Bytom交易说明(账户管理模式)
  • Centos6.8 使用rpm安装mysql5.7
  • Create React App 使用
  • magento2项目上线注意事项
  • Node 版本管理
  • Redis在Web项目中的应用与实践
  • spring + angular 实现导出excel
  • Webpack 4 学习01(基础配置)
  • 阿里云Kubernetes容器服务上体验Knative
  • 测试如何在敏捷团队中工作?
  • 前端js -- this指向总结。
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 手写双向链表LinkedList的几个常用功能
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 小程序button引导用户授权
  • 原生Ajax
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 扩展资源服务器解决oauth2 性能瓶颈
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (九十四)函数和二维数组
  • (一)appium-desktop定位元素原理
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET 中创建支持集合初始化器的类型
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .NET实现之(自动更新)
  • .NET中的十进制浮点类型,徐汇区网站设计
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka
  • @RequestMapping用法详解
  • @Service注解让spring找到你的Service bean
  • []AT 指令 收发短信和GPRS上网 SIM508/548