Clocks manage their own canvases
This commit is contained in:
parent
f2724998f7
commit
76b8eed77b
@ -3,8 +3,9 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>Clocks</title>
|
<title>Clocks</title>
|
||||||
<script src="src/main.js" type="module"></script>
|
<script src="src/main.js" type="module"></script>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<canvas width="1900" height="1000">You need JavaScript enabled to see the clocks</canvas>
|
<div id="clockArea"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
32
src/clock.js
32
src/clock.js
@ -1,25 +1,41 @@
|
|||||||
class Clock {
|
class Clock {
|
||||||
constructor(name, color, numWedges, radius, x, y) {
|
constructor(name, color, numWedges, radius) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.color = color
|
this.color = color
|
||||||
this.numWedges = numWedges
|
this.numWedges = numWedges
|
||||||
this.filledWedges = numWedges / 2
|
this.filledWedges = numWedges / 2
|
||||||
this.radius = radius
|
this.radius = radius
|
||||||
this.x = x
|
|
||||||
this.y = y
|
|
||||||
this.splitRadius = 10
|
this.splitRadius = 10
|
||||||
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
draw(ctx) {
|
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)
|
||||||
ctx.lineWidth = 3
|
ctx.lineWidth = 3
|
||||||
for (let i = 0; i < this.numWedges; i++) this.drawWedge(ctx, i)
|
for (let i = 0; i < this.numWedges; i++) this.drawWedge(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
drawWedge(ctx, i) {
|
drawWedge(i) {
|
||||||
|
const { ctx } = this
|
||||||
const width = ((2 * Math.PI) / this.numWedges)
|
const width = ((2 * Math.PI) / this.numWedges)
|
||||||
const angle = width * i
|
const angle = width * i
|
||||||
const x = this.x + (Math.cos(angle) * this.splitRadius)
|
const x = (this.canvas.width / 2) + (Math.cos(angle) * this.splitRadius)
|
||||||
const y = this.y + (Math.sin(angle) * this.splitRadius)
|
const y = (this.canvas.height / 2) + (Math.sin(angle) * this.splitRadius)
|
||||||
|
|
||||||
ctx.beginPath()
|
ctx.beginPath()
|
||||||
ctx.strokeStyle = this.color
|
ctx.strokeStyle = this.color
|
||||||
|
|||||||
47
src/main.js
47
src/main.js
@ -3,41 +3,24 @@ import Resources from "./resources.js"
|
|||||||
import Morale from "./morale.js"
|
import Morale from "./morale.js"
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
const canvas = document.querySelector("canvas")
|
const clockArea = document.querySelector("#clockArea")
|
||||||
|
const clocks = [
|
||||||
|
new Clock("zed", "black", 4, 100),
|
||||||
|
new Clock("food runs out", "red", 9, 100),
|
||||||
|
new Clock("raining", "blue", 3, 100),
|
||||||
|
new Clock("ennui", "purple", 25, 100)
|
||||||
|
]
|
||||||
|
clocks.map(clock => clock.htmlElement.call(clock)).forEach(clockHtml => clockArea.appendChild(clockHtml))
|
||||||
|
|
||||||
const ctx = canvas.getContext("2d")
|
// const r = new Resources("ho", "green", 12, 600, 100, 400)
|
||||||
|
// const m = new Morale("oh no", "red", 600, 100, 500)
|
||||||
const c = new Clock("hi",
|
function draw(_ts) {
|
||||||
"blue",
|
clocks.forEach(clock => clock.draw.call(clock))
|
||||||
9,
|
// r.draw(ctx)
|
||||||
100,
|
// m.draw(ctx)
|
||||||
200,
|
|
||||||
200
|
|
||||||
)
|
|
||||||
|
|
||||||
const r = new Resources("ho",
|
|
||||||
"green",
|
|
||||||
12,
|
|
||||||
600,
|
|
||||||
100,
|
|
||||||
400
|
|
||||||
)
|
|
||||||
|
|
||||||
const m = new Morale("oh no",
|
|
||||||
"red",
|
|
||||||
600,
|
|
||||||
100,
|
|
||||||
500
|
|
||||||
)
|
|
||||||
|
|
||||||
function draw(x) {
|
|
||||||
ctx.fillStyle = "white"
|
|
||||||
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
||||||
c.draw(ctx)
|
|
||||||
r.draw(ctx)
|
|
||||||
m.draw(ctx)
|
|
||||||
window.requestAnimationFrame(draw)
|
window.requestAnimationFrame(draw)
|
||||||
}
|
}
|
||||||
|
|
||||||
window.requestAnimationFrame(draw)
|
window.requestAnimationFrame(draw)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user