html--全魔乱舞

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>html, body {
	overflow: hidden;
	touch-action: none;
	content-zooming: none;
	position: absolute;
	margin: 0;
	padding: 0;
	width: 100%;
	height: 100%;
	background: #000;
}

#screen {
	position: absolute;
	width: 100vh;
	height: 100vh;
	max-height: 100vw;
	max-width: 100vw;
	margin: auto;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
	user-select: none;
}
#screen canvas {
	position: relative;
	float: left;
	display: block;
	width: 28.7vmin;
	height: 28.7vmin;
	background:#222;
	margin-left: 3vmin;
	margin-top: 3vmin;
	border-radius: 3vmin;
}</style>
 </HEAD>

 <BODY><div id="screen"></div>
  <script>"use strict";
{
	const Box = class {
		constructor(container, i) {
			this.canvas = document.createElement("canvas");
			container.appendChild(this.canvas);
			this.width = this.canvas.width = this.canvas.offsetWidth;
			this.height = this.canvas.height = this.canvas.offsetHeight;
			this.ctx = this.canvas.getContext("2d");
			this.ctx.lineCap = "round";
			this.points = Array.from(
				{ length: 12 },
				_ => new Box.Point(this.width * 0.5, this.width * 0.5)
			);
			this.constraints = [];
			this.color = `hsl(${10 * i}, 70%, 30%)`;
			// links p1, p2, length, width
			const links = [
				[0, 1, 6, 5],
				[1, 2, 5, 4],
				[3, 7, 10, 6],
				[7, 8, 7, 4],
				[3, 9, 10, 6],
				[9, 10, 7, 4],
				[0, 4, 5, 0],
				[0, 3, 7, 9],
				[0, 5, 6, 5],
				[5, 6, 5, 4],
				[4, 11, 0.1, 11]
			];
			// constaints: p1, p2, p3, angle, range
			const p2 = Math.PI / 2;
			const p3 = Math.PI / 3;
			[
				[0, 4, 11, 0, 0, 0],
				[4, 0, 3, 0, 1, 0.05],
				[3, 0, 4, 0, 1, 0.05],
				[3, 0, 1, p2, p2 * 4, 0],
				[0, 1, 2, -p2, p3, 0.05],
				[3, 0, 5, p2, p2 * 4, 0],
				[0, 5, 6, -p2, p3, 0.05],
				[0, 3, 7, -p3 + 0.5, p2, 0.05],
				[0, 3, 9, -p3 + 0.5, p2, 0.05],
				[3, 7, 8, p2, p3, 0.05],
				[3, 9, 10, p2, p3, 0.05]
			].forEach(p => {
				this.constraints.push(new Box.Constraint(this, links, p, this.width / 30));
			});
		}
		run() {
			this.ctx.setTransform(0, 1, 1, 0, 0, 0);
			this.ctx.clearRect(0, 0, this.width + 1, this.height + 1);
			this.ctx.globalCompositeOperation = "lighter";
			for (const constraint of this.constraints) constraint.solve();
			for (const point of this.points) point.integrate();
		}
	};
	Box.Point = class {
		constructor(x, y) {
			this.x = x;
			this.y = y;
			this.xb = x;
			this.yb = y
			this.w = 0;
		}
		integrate() {
			const vx = (this.x - this.xb) * 0.995;
			const vy = (this.y - this.yb) * 0.995;
			this.xb = this.x;
			this.yb = this.y;
			this.x += vx;
			this.y += vy;
		}
	};
	Box.Constraint = class {
		constructor(box, links, p, s) {
			this.ctx = box.ctx;
			this.p1 = box.points[p[0]];
			this.p2 = box.points[p[1]];
			this.p3 = box.points[p[2]];
			this.m = 0;
			this.angle = p[3];
			this.range = p[4];
			this.force = p[5];
			const link1 = this.link(links, p[0], p[1]);
			const link2 = this.link(links, p[1], p[2]);
			this.len1 = link1[2] * s;
			this.len2 = link2[2] * s;
			this.width = link2[3] * s * 0.5;
			if (this.width * 0.5 > this.p3.w) this.p3.w = this.width * 0.5;
			this.shape = document.createElement("canvas");
			if (this.width) {
				this.shape.width = this.len2 + this.width;
				this.shape.height = this.width;
				const ict = this.shape.getContext("2d");
				ict.lineCap = "round";
				ict.strokeStyle = box.color;
				ict.beginPath();
				ict.lineWidth = this.width * 0.9;
				ict.moveTo(this.width * 0.5, this.width * 0.5);
				ict.lineTo(this.len2 + this.width * 0.5, this.width * 0.5);
				ict.stroke();
			}
		}
		link(links, p0, p1) {
			for (const link of links) {
				if (
					(p0 === link[0] && p1 === link[1]) ||
					(p1 === link[0] && p0 === link[1])
				)
					return link;
			}
		}
		solve() {
			const a1 = Math.atan2(this.p2.y - this.p1.y, this.p2.x - this.p1.x);
			const b1 = Math.atan2(this.p3.y - this.p2.y, this.p3.x - this.p2.x);
			const c = this.angle - (b1 - a1);
			const d = c > Math.PI ? c - 2 * Math.PI : c < -Math.PI ? c + 2 * Math.PI : c;
			const e = Math.abs(d) > this.range ? (-Math.sign(d) * this.range + d) * this.force : 0;
			const cos1 = Math.cos(a1 - e);
			const sin1 = Math.sin(a1 - e);
			const x1 = this.p1.x + (this.p2.x - this.p1.x) * 0.5;
			const y1 = this.p1.y + (this.p2.y - this.p1.y) * 0.5;
			this.p1.x = x1 - cos1 * this.len1 * 0.5;
			this.p1.y = y1 - sin1 * this.len1 * 0.5;
			this.p2.x = x1 + cos1 * this.len1 * 0.5;
			this.p2.y = y1 + sin1 * this.len1 * 0.5;
			this.m += 0.0005 * (Math.random() - 0.5);
			this.m *= 0.99;
			const a2 = this.m + Math.atan2(this.p2.y - this.p3.y, this.p2.x - this.p3.x) + e;
			const cos2 = Math.cos(a2);
			const sin2 = Math.sin(a2);
			const x2 = this.p3.x + (this.p2.x - this.p3.x) * 0.5;
			const y2 = this.p3.y + (this.p2.y - this.p3.y) * 0.5;
			this.p3.x = x2 - cos2 * this.len2 * 0.5;
			this.p3.y = y2 - sin2 * this.len2 * 0.5;
			this.p2.x = x2 + cos2 * this.len2 * 0.5;
			this.p2.y = y2 + sin2 * this.len2 * 0.5;
			if (this.width) {
				this.ctx.setTransform(cos2, sin2, -sin2, cos2, this.p2.x, this.p2.y);
				this.ctx.drawImage(
					this.shape,
					-this.len2 - this.width * 0.5,
					-this.width * 0.5
				);
			}
		}
	};
	const screen = document.querySelector("#screen");
	const boxes = Array.from({ length: 9 }, (v, i) => new Box(screen, i));
	const run = () => {
		requestAnimationFrame(run);
		for (const box of boxes) box.run();
	};
	run();
}</script>
 </BODY>
</HTML>




相关推荐
Mh3 小时前
我是如何设计品牌站组件库的?
前端·css·设计模式
程序员大阳4 小时前
副队长Vue3教程(2)--Vite 搭建 Vue 项目,认识目录结构
前端·javascript·vue.js·vite·创建项目·目录结构·项目结构
濮水大叔4 小时前
Cabloy/Zova 与 Next.js 的 SSR 水合差异
前端·typescript·next.js
SoaringHeart5 小时前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
Flynt6 小时前
Chrome给AI编程助手塞了203篇前端规范,我拆包实测:检索0.77秒是真快,扫描器有点挑食
前端·chrome·ai编程
罗西的思考6 小时前
【轻量化复刻大模型 / KIMI K3】Muon 优化器及 训练系统
人工智能·算法·机器学习
北城笑笑6 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
程序员韩星7 小时前
OpenAI 兼容接口返回 404,先查请求地址,再查模型名
前端·人工智能·http
Patrick_Wilson7 小时前
构建通过了,为什么有些浏览器还会白屏?
前端·javascript·浏览器
ashiho7 小时前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口