// christmas-tree.js · 一份圣诞惊喜
const TAU = Math.PI * 2;
const rand = (a = 1, b = 0) => b + Math.random() * (a - b);
const lerp = (a, b, t) => a + (b - a) * t;
// 沿锥面撒点:越靠底部越密
function buildBranch(height, radius, count) {
const points = [];
for (let i = 0; i < count; i++) {
const t = Math.pow(Math.random(), 0.62);
const angle = rand(TAU);
const wobble = 1 + 0.05 * Math.sin(t * 26 + angle * 3);
const r = radius * (1 - t) * wobble * rand(1, 0.76);
points.push({
x: Math.cos(angle) * r,
y: -t * height,
z: Math.sin(angle) * r,
size: rand(2.1, 0.55),
});
}
return points;
}
// 缠绕在树身上的光带
function buildRibbons(radius, height) {
const ribbons = [];
for (let k = 0; k < 6; k++) {
const phase = k * 1.07, turns = 2.6, line = [];
for (let s = 0; s <= 1; s += 0.004) {
const a = phase + s * turns * TAU;
const r = radius * (1 - s) * 1.02;
line.push([Math.cos(a) * r, -s * height, Math.sin(a) * r]);
}
ribbons.push(line);
}
return ribbons;
}
// 树上的小彩灯
function buildLights(count = 110) {
const lights = [];
for (let i = 0; i < count; i++) {
const t = Math.pow(Math.random(), 0.8);
const a = rand(TAU), r = radius * (1 - t);
lights.push({
pos: [Math.cos(a) * r, -t * height, Math.sin(a) * r],
hue: pick(ACCENTS), twinkle: rand(TAU),
});
}
return lights;
}
const tree = buildBranch(H, R, 2600);
const ribbons = buildRibbons(R, H);
const lights = buildLights();
const snow = new Snowfall({ count: 1000, speed: 0.35 });
// 相机缓慢绕树旋转
function frame(now) {
const cam = now * 0.00016 + Math.sin(now * 0.0004) * 0.06;
tree.sort((a, b) => a.z - b.z);
for (const p of tree) {
const [x, z] = rotate(p.x, p.z, cam);
const s = FOCAL / (FOCAL + z);
dot(x * s, p.y * s, p.size * s, p.depth);
}
requestAnimationFrame(frame);
}
frame(performance.now());

一份圣诞惊喜

祝你天天开心
Merry Christmas 郑紫凝
配色
Powered by Netlify