更多>
最新下载
24小时阅读排行榜
- 1 小黑盒加速器官方下载_小黑盒游戏加速器最新版
- 2 mc.js网页版在线玩 mc.js游戏世界入口
- 3 Edge浏览器收藏网页的快捷键
- 4 VSCode用户设置同步功能
- 5 联想ThinkPlus真无线和摩托罗拉VerveBuds哪款更耐用_联想与摩托罗拉蓝牙耳机综合对比
- 6 芒果发黑还能吃吗 芒果坏了如何辨别
- 7 如何为Laravel创建自定义命令_Artisan自定义命令行工具开发
- 8 vscode怎么用git比较分支差异_vscode比较两个git分支之间差异的方法
- 9 iPhone17Pro怎样查看存储空间_iPhone17Pro存储占用情况查看与清理方法
- 10 windows10如何设置文件夹背景颜色或图片_windows10文件夹背景自定义方法
- 11 Go语言:高效实现IP地址范围检查
- 12 edge浏览器启动时如何恢复上次的会话_Edge自动恢复标签页设置
- 13 php调用内存优化技巧_php调用垃圾回收机制优化
- 14 Go语言中获取Map元素数量的正确姿势
- 15 c++中静态多态和动态多态的实现_c++编译期与运行期多态机制对比
更多>
最新教程
-
- Node.js 教程
- 7456 2025-08-28
-
- CSS3 教程
- 1049330 2025-08-27
-
- Rust 教程
- 11829 2025-08-27
-
- Vue 教程
- 14062 2025-08-22
-
- PostgreSQL 教程
- 10820 2025-08-21
-
- Git 教程
- 5151 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

