Last active
April 6, 2020 04:05
-
-
Save pot-code/23f39881735684e720ebfad159e047a5 to your computer and use it in GitHub Desktop.
[canvas] canvas collection #canvas
This file contains 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
const $graph = document.getElementById("graph"); | |
const pixelRatio = Math.ceil(window.devicePixelRatio); | |
const context = $graph.getContext("2d"); | |
const width = $graph.offsetWidth; | |
const height = $graph.offsetHeight; | |
$graph.height = height * pixelRatio; | |
$graph.width = width * pixelRatio; | |
if (pixelRatio > 1) { | |
context.scale(pixelRatio, pixelRatio); | |
} |
This file contains 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
class BaseFramework { | |
constructor(canvas) { | |
this.canvas = canvas; | |
this.nextTick = Date.now(); | |
this.active = true; | |
window.addEventListener("blur", this.pause); | |
window.addEventListener("focus", this.resume); | |
} | |
clearCanvas() { | |
const { ctx, width, height } = this.canvas; | |
ctx.clearRect(0, 0, width, height); | |
} | |
drawBackground() { | |
const { ctx, width, height } = this.canvas; | |
ctx.fillStyle = "#000000"; | |
ctx.fillRect(0, 0, width, height); | |
} | |
pause = () => { | |
this.active = false; | |
}; | |
resume = () => { | |
this.active = true; | |
this.nextTick = Date.now(); | |
this.tick(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment