2026-01-24 13:57:03 -05:00
|
|
|
class Clock {
|
2026-01-26 06:34:10 -05:00
|
|
|
constructor(name, color, numWedges, radius) {
|
2026-01-24 13:57:03 -05:00
|
|
|
this.name = name
|
|
|
|
|
this.color = color
|
|
|
|
|
this.numWedges = numWedges
|
|
|
|
|
this.filledWedges = numWedges / 2
|
|
|
|
|
this.radius = radius
|
|
|
|
|
this.splitRadius = 10
|
2026-01-26 06:34:10 -05:00
|
|
|
this.canvas = document.createElement("canvas")
|
|
|
|
|
this.canvas.width = (this.radius * 2) + 40
|
|
|
|
|
this.canvas.height = (this.radius * 2) + 40
|
|
|
|
|
this.ctx = this.canvas.getContext("2d")
|
2026-01-24 13:57:03 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-26 06:34:10 -05:00
|
|
|
htmlElement() {
|
|
|
|
|
const card = document.createElement("div")
|
|
|
|
|
card.classList.add("card")
|
|
|
|
|
card.appendChild(this.canvas)
|
|
|
|
|
const title = document.createElement("span")
|
|
|
|
|
title.innerHTML = this.name
|
|
|
|
|
card.appendChild(title)
|
|
|
|
|
return card
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw() {
|
|
|
|
|
const { ctx } = this
|
|
|
|
|
ctx.fillStyle = "white"
|
|
|
|
|
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
|
2026-01-24 13:57:03 -05:00
|
|
|
ctx.lineWidth = 3
|
2026-01-26 06:34:10 -05:00
|
|
|
for (let i = 0; i < this.numWedges; i++) this.drawWedge(i)
|
2026-01-24 13:57:03 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-26 06:34:10 -05:00
|
|
|
drawWedge(i) {
|
|
|
|
|
const { ctx } = this
|
2026-01-24 13:57:03 -05:00
|
|
|
const width = ((2 * Math.PI) / this.numWedges)
|
|
|
|
|
const angle = width * i
|
2026-01-26 06:34:10 -05:00
|
|
|
const x = (this.canvas.width / 2) + (Math.cos(angle) * this.splitRadius)
|
|
|
|
|
const y = (this.canvas.height / 2) + (Math.sin(angle) * this.splitRadius)
|
2026-01-24 13:57:03 -05:00
|
|
|
|
|
|
|
|
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
|