更多>
最新下载
24小时阅读排行榜
- 1 HTML表格文本怎么排版_HTML表格文本如何对齐与美化显示
- 2 python的递归函数如何理解?
- 3 怎样在python中定义非闭包
- 4 Python 文件加密与解密的实现思路
- 5 HTML自适应布局怎么设计_HTML自适应页面布局的实现思路
- 6 Python中内置类型性能有哪些?
- 7 Linux 安全:如何使用 Kerberos 实现集中认证 (KDC + 客户端)
- 8 牛市是什么_为什么牛市里情绪和流动性会推动价格快速上涨
- 9 postgresql数据库具有什么核心特性_postgresql核心特性深度解析
- 10 币圈的“孕线”形态怎么看?如何判断是看涨还是看跌孕线?
- 11 python中OpenCV调节亮度
- 12 如何利用布林带的收口和开口来预判K线形态的突破方向?
- 13 什么是“条件单”交易?它和限价单有什么区别,如何用它来追突破?
- 14 Python 文件删除后的恢复机制研究
- 15 什么是通证(Token)和硬币(Coin)?它们之间有什么关键区别?
更多>
最新教程
-
- Node.js 教程
- 9042 2025-08-28
-
- CSS3 教程
- 1171867 2025-08-27
-
- Rust 教程
- 14420 2025-08-27
-
- Vue 教程
- 16720 2025-08-22
-
- PostgreSQL 教程
- 13305 2025-08-21
-
- Git 教程
- 5873 2025-08-21
HTML5+Canvas的黑洞特效
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>不要失去你的彩虹</title>
<style>
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id=c></canvas>
<script>
let w = c.width = window.innerWidth;
let h = c.height = window.innerHeight;
const ctx = c.getContext('2d');
const opts = {
hexLength: 30,
lenFn: ({ len, t }) =>
len + Math.sin(t),
radFn: ({ rad, len, t, excitement }) =>
rad + (excitement + opts.propFn({ len, t }))*2 / 4,
propFn: ({ len, t }) =>
len / opts.hexLength / 10 - t,
excitementFn: ({ len, t }) =>
Math.sin(opts.propFn({ len, t }))**2,
colorFn: ({ rad, excitement, t }) =>
`hsl(${rad / Math.TAU * 360 + t}, ${excitement * 100}%, ${20 + excitement * 50}%)`,
timeStep: .01,
randomJig: 8,
repaintColor: 'rgba(0,0,0,.1)'
};
let tick = 0;
Math.TAU = 6.28318530717958647692;
const vertices = [];
class Vertex {
constructor({ x, y }) {
this.len = Math.sqrt(x*x + y*y);
this.rad = Math.acos(x / this.len) * (y > 0 ? 1 : -1) + .13;
this.prevPoint = { x, y };
}
step() {
const excitement = opts.excitementFn({ len: this.len, t: tick });
const param = {
len: this.len,
rad: this.rad,
t: tick,
excitement
};
const nextLen = opts.lenFn(param);
const nextRad = opts.radFn(param);
const color = opts.colorFn(param);
ctx.strokeStyle = color;
ctx.lineWidth = excitement + .2;
ctx.beginPath();
ctx.moveTo(this.prevPoint.x, this.prevPoint.y);
this.prevPoint.x = nextLen * Math.cos(nextRad) +
Math.random() * (1-excitement)**2 * opts.randomJig * 2 - opts.randomJig;
this.prevPoint.y = nextLen * Math.sin(nextRad) +
Math.random() * (1-excitement)**2 * opts.randomJig * 2 - opts.randomJig;
ctx.lineTo(this.prevPoint.x, this.prevPoint.y);
ctx.stroke();
}
static gen() {
vertices.length = 0;
const hexCos = Math.cos(Math.TAU / 12) * opts.hexLength;
const hexSin = Math.sin(Math.TAU / 12) * opts.hexLength;
let alternanceX = false;
for(let x = 0; x < w; x += hexCos) {
let alternance = alternanceX = !alternanceX;
for(let y = 0; y < h; y += hexSin + opts.hexLength) {
alternance = !alternance;
vertices.push(new Vertex({
x: x - w / 2,
y: y + alternance * hexSin - h / 2
}))
}
}
}
}
Vertex.gen();
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, w, h);
const anim = () => {
window.requestAnimationFrame(anim);
tick += opts.timeStep;
ctx.fillStyle = opts.repaintColor;
ctx.fillRect(0, 0, w, h);
ctx.translate(w/2, h/2);
vertices.forEach((vertex) => vertex.step());
ctx.translate(-w/2, -h/2);
}
anim();
window.addEventListener('resize', () => {
w = c.width = window.innerWidth;
h = c.height = window.innerHeight;
Vertex.gen();
tick = 0;
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, w, h);
})
</script>
</body>
<html>
<head>
<meta charset="utf-8">
<title>不要失去你的彩虹</title>
<style>
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id=c></canvas>
<script>
let w = c.width = window.innerWidth;
let h = c.height = window.innerHeight;
const ctx = c.getContext('2d');
const opts = {
hexLength: 30,
lenFn: ({ len, t }) =>
len + Math.sin(t),
radFn: ({ rad, len, t, excitement }) =>
rad + (excitement + opts.propFn({ len, t }))*2 / 4,
propFn: ({ len, t }) =>
len / opts.hexLength / 10 - t,
excitementFn: ({ len, t }) =>
Math.sin(opts.propFn({ len, t }))**2,
colorFn: ({ rad, excitement, t }) =>
`hsl(${rad / Math.TAU * 360 + t}, ${excitement * 100}%, ${20 + excitement * 50}%)`,
timeStep: .01,
randomJig: 8,
repaintColor: 'rgba(0,0,0,.1)'
};
let tick = 0;
Math.TAU = 6.28318530717958647692;
const vertices = [];
class Vertex {
constructor({ x, y }) {
this.len = Math.sqrt(x*x + y*y);
this.rad = Math.acos(x / this.len) * (y > 0 ? 1 : -1) + .13;
this.prevPoint = { x, y };
}
step() {
const excitement = opts.excitementFn({ len: this.len, t: tick });
const param = {
len: this.len,
rad: this.rad,
t: tick,
excitement
};
const nextLen = opts.lenFn(param);
const nextRad = opts.radFn(param);
const color = opts.colorFn(param);
ctx.strokeStyle = color;
ctx.lineWidth = excitement + .2;
ctx.beginPath();
ctx.moveTo(this.prevPoint.x, this.prevPoint.y);
this.prevPoint.x = nextLen * Math.cos(nextRad) +
Math.random() * (1-excitement)**2 * opts.randomJig * 2 - opts.randomJig;
this.prevPoint.y = nextLen * Math.sin(nextRad) +
Math.random() * (1-excitement)**2 * opts.randomJig * 2 - opts.randomJig;
ctx.lineTo(this.prevPoint.x, this.prevPoint.y);
ctx.stroke();
}
static gen() {
vertices.length = 0;
const hexCos = Math.cos(Math.TAU / 12) * opts.hexLength;
const hexSin = Math.sin(Math.TAU / 12) * opts.hexLength;
let alternanceX = false;
for(let x = 0; x < w; x += hexCos) {
let alternance = alternanceX = !alternanceX;
for(let y = 0; y < h; y += hexSin + opts.hexLength) {
alternance = !alternance;
vertices.push(new Vertex({
x: x - w / 2,
y: y + alternance * hexSin - h / 2
}))
}
}
}
}
Vertex.gen();
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, w, h);
const anim = () => {
window.requestAnimationFrame(anim);
tick += opts.timeStep;
ctx.fillStyle = opts.repaintColor;
ctx.fillRect(0, 0, w, h);
ctx.translate(w/2, h/2);
vertices.forEach((vertex) => vertex.step());
ctx.translate(-w/2, -h/2);
}
anim();
window.addEventListener('resize', () => {
w = c.width = window.innerWidth;
h = c.height = window.innerHeight;
Vertex.gen();
tick = 0;
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, w, h);
})
</script>
</body>
</html>
这是一个HTML5+Canvas的黑洞特效,需要的朋友可以直接下载使用,更多特效代码尽在PHP中文网。
本站所有资源都是由网友投搞发布,或转载各大下载站,请自行检测软件的完整性!本站所有资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己承担!如有侵权请联系我们删除下架,联系方式:admin@php.cn

