以前、時計の針が動くものをp5.jsで作ったのですが、コードが長くて冗長な感じになってしまいました。
もっと短くできるだろうと思い、やってみました。
コードをシンプルにする
↓clockhand.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cx, cy; | |
let a; | |
let w; | |
function setup() { | |
createCanvas(windowWidth, windowHeight, P2D); | |
cx = width / 2; | |
cy = height / 2; | |
angle = 0; | |
} | |
function draw() { | |
background(200, 100); | |
secondHand(second() * 6 - 90, 180); | |
minuteHand(minute() * 6 - 90, 120); | |
hourHand(hour() * 30 - 90, 80); | |
} | |
function secondHand(a, w) { | |
line(cx, cy, cx + cos(radians(a)) * w, cy + sin(radians(a)) * w); | |
} | |
function minuteHand(a, w) { | |
line(cx, cy, cx + cos(radians(a)) * w, cy + sin(radians(a)) * w); | |
} | |
function hourHand(a, w) { | |
line(cx, cy, cx + cos(radians(a)) * w, cy + sin(radians(a)) * w); | |
} |
コードをシンプルにするにはclassを使わないといけないと思いこんでいて、なかなかできませんでした。
classってどんな時に使えばいいのだろう?