p5js/2.太阳公公钟.html

113 lines
3.9 KiB
HTML
Raw Normal View History

2022-12-13 13:26:05 +00:00
<!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 一天毫秒数 = 3600 * 24 * 1000;
const 时针角度除数 = 半天毫秒数 / 360;
const 分针角度除数 = 小时毫秒数 / 360;
const 秒针角度除数 = 分钟毫秒数 / 360;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
frameRate(60);
noCursor();
}
function draw() {
background("skyblue");
let x = width / 2 || mouseX;
let y = height / 2 || mouseY;
// 画阳光
push();
translate(x, y);
stroke(255, 255, 0, 128);
strokeWeight(4);
let distance = 1 + (frameCount % 30);
for (let i = 0, n = 36; i < n; i++) {
line(0, -50 - distance * 10, 0, -100 - distance * 10);
rotate(360 / n);
}
// 画太阳
stroke(255, 255, 0, 255);
fill("#FFCC00");
strokeWeight(8);
circle(0, 0, 100);
noFill();
arc(-20, 0, 20, 20, -135, -45);
arc(20, 0, 20, 20, -135, -45);
arc(0, 10, 40, 40, 45, 135);
// 画时刻度
strokeWeight(2);
for (let i = 0, n = 12; i < n; i++) {
line(0, 40, 0, 50);
rotate(360 / n);
}
// 画分刻度
strokeWeight(1);
for (let i = 0, n = 60; i < n; i++) {
line(0, 45, 0, 50);
rotate(360 / n);
}
pop();
const 当前时间 = Date.now() + 小时毫秒数 * 8;
const angleHour = (当前时间 % 半天毫秒数) / 时针角度除数;
const angleMinute = (当前时间 % 小时毫秒数) / 分针角度除数;
const angleSecond = (当前时间 % 分钟毫秒数) / 秒针角度除数;
push();
translate(x, y);
// 画时针
push();
stroke(0);
strokeWeight(3);
rotate(angleHour);
line(0, 5, 0, -30);
pop();
// 画分针
push();
stroke(0);
strokeWeight(3);
rotate(angleMinute);
line(0, 5, 0, -40);
pop();
// 画秒针
push();
stroke(255, 0, 0);
strokeWeight(1);
rotate(angleSecond);
line(0, 5, 0, -45);
circle(0, 0, 2);
pop();
pop();
translate(0, 0);
noStroke();
fill(0);
text("作品:太阳公公钟", 30, height - 70);
text("作者:赵海洋爸爸", 30, height - 50);
text("日期2022/12/11", 30, height - 30);
}
</script>
</head>
<body>
<main></main>
</body>
</html>