clocks/src/clock.js

39 lines
903 B
JavaScript
Raw Normal View History

2026-01-24 13:57:03 -05:00
class Clock {
constructor(name, color, numWedges, radius, x, y) {
this.name = name
this.color = color
this.numWedges = numWedges
this.filledWedges = numWedges / 2
this.radius = radius
this.x = x
this.y = y
this.splitRadius = 10
}
draw(ctx) {
ctx.lineWidth = 3
for (let i = 0; i < this.numWedges; i++) this.drawWedge(ctx, i)
}
drawWedge(ctx, i) {
const width = ((2 * Math.PI) / this.numWedges)
const angle = width * i
const x = this.x + (Math.cos(angle) * this.splitRadius)
const y = this.y + (Math.sin(angle) * this.splitRadius)
ctx.beginPath()
ctx.strokeStyle = this.color
ctx.moveTo(x, y)
ctx.arc(x, y, this.radius, (angle - (width / 2)), (angle + width / 2))
ctx.lineTo(x, y)
ctx.stroke()
if (i < this.filledWedges) {
ctx.fillStyle = this.color
ctx.fill()
}
}
}
export default Clock