Skip to content

Commit

Permalink
limit animation frame time rather than pausing on blur
Browse files Browse the repository at this point in the history
  • Loading branch information
trmid committed Aug 7, 2024
1 parent 8775533 commit 0287a3f
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions plinko.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ window.addEventListener("load", () => {
},
]

window.addEventListener("keydown", (e) => {
if (e.key === " ") {
gameState.state = "done"
}
})

// Create 2D context
const ctx = canvas.getContext("2d")

Expand Down Expand Up @@ -178,20 +172,6 @@ window.addEventListener("load", () => {
}
window.addEventListener("resize", resize)

// Listen for focus and blur events
window.addEventListener("blur", () => {
if (gameState.state === "playing") {
gameState.state = "paused"
}
})
window.addEventListener("focus", () => {
if (gameState.state === "paused") {
gameState.state = "playing"
lastFrameTime = performance.now()
play()
}
})

// Function to get the game state for the next frame
const getNextFrameState = (state) => {
const n = JSON.parse(JSON.stringify(state))
Expand Down Expand Up @@ -317,7 +297,8 @@ window.addEventListener("load", () => {
// Request next frame
requestAnimationFrame(play)
const now = performance.now()
totalPlayTime += now - lastFrameTime
const timeSinceLastFrame = Math.min(frameStepMs, now - lastFrameTime) // limit animation frame time to frame step
totalPlayTime += timeSinceLastFrame
lastFrameTime = now

const nextState = getNextFrameState(gameState)
Expand Down

0 comments on commit 0287a3f

Please sign in to comment.