p5js/2.太阳公公钟.html
2022-12-14 01:09:46 +08:00

110 lines
3.6 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="/css/style.css" />
<title>太阳公公钟</title>
<script src="/p5/p5.min.js"></script>
<script src="/p5/addons/p5.sound.min.js"></script>
<script src="/lib/utils.js"></script>
<script>
const 分钟毫秒数 = 60 * 1000;
const 小时毫秒数 = 3600 * 1000;
const 半天毫秒数 = 3600 * 12 * 1000;
const 时针角度除数 = 半天毫秒数 / 360; // 120000
const 分针角度除数 = 小时毫秒数 / 360; // 10000
const 秒针角度除数 = 分钟毫秒数 / 360; // 1000/6
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
frameRate(60);
}
function draw() {
background("skyblue");
push();
translate(width / 2, height / 2);
// 画阳光
stroke(255, 255, 0, 64);
strokeWeight(4);
const 阳光数量 = 36;
const 阳光飞行距离 = 10 + (frameCount % 40) * 10;
for (let i = 0; i < 阳光数量; i++) {
line(0, -阳光飞行距离, 0, -阳光飞行距离 - 50);
rotate(360 / 阳光数量);
}
// 画太阳轮廓
stroke(255, 255, 0, 255);
strokeWeight(8);
fill("#FFCC00");
circle(0, 0, 200);
// 画太阳表情
noFill();
arc(-40, 0, 40, 40, -135, -45);
arc(40, 0, 40, 40, -135, -45);
arc(0, 20, 80, 80, 45, 135);
// 画时刻度
strokeWeight(4);
for (let i = 0, n = 12; i < n; i++) {
line(0, 85, 0, 100);
rotate(360 / n);
}
// 画分刻度
strokeWeight(1);
for (let i = 0, n = 60; i < n; i++) {
line(0, 90, 0, 100);
rotate(360 / n);
}
const 当前时间 = Date.now() + 小时毫秒数 * 8;
const angleHour = (当前时间 % 半天毫秒数) / 时针角度除数;
const angleMinute = (当前时间 % 小时毫秒数) / 分针角度除数;
const angleSecond = (当前时间 % 分钟毫秒数) / 秒针角度除数;
// 画时针
push();
stroke(0);
strokeWeight(4);
rotate(angleHour);
line(0, 10, 0, -60);
pop();
// 画分针
push();
stroke(0);
strokeWeight(4);
rotate(angleMinute);
line(0, 10, 0, -80);
pop();
// 画秒针
push();
strokeWeight(2);
stroke(255, 0, 0);
rotate(angleSecond);
line(0, 10, 0, -85);
fill(0);
circle(0, 0, 5);
pop();
pop();
textInfo("太阳公公钟", "2022-12-11");
}
</script>
</head>
<body>
<main></main>
</body>
</html>