Created
August 10, 2023 20:28
-
-
Save danthemango/723c7f5a98f80bd8dc43fd5cef29e699 to your computer and use it in GitHub Desktop.
HTML Canvas boilerplate
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>canvas</title> | |
</head> | |
<body> | |
<canvas> </canvas> | |
<script> | |
window.addEventListener('load', function() { | |
const canvas = document.querySelector('canvas'); | |
const ctx = canvas.getContext('2d'); | |
canvas.width = 900; | |
canvas.height = 750; | |
ctx.fillRect(0,0,canvas.width, canvas.height); | |
ctx.lineWidth = 1; | |
ctx.strokeStyle = 'red'; | |
function draw(dt) { | |
ctx.rect(5, 10, 150, 100); | |
ctx.stroke(); | |
} | |
let pTime = 0; | |
function animate(time) { | |
let dt = time - pTime; | |
pTime = time; | |
draw(dt); | |
requestAnimationFrame(animate); | |
} | |
animate(pTime); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import script from other file:
<script src="script.js"></script>
,import css from another file:
<link rel="stylesheet" href="style.css">