Skip to content

Instantly share code, notes, and snippets.

@danthemango
Created August 10, 2023 20:28
Show Gist options
  • Save danthemango/723c7f5a98f80bd8dc43fd5cef29e699 to your computer and use it in GitHub Desktop.
Save danthemango/723c7f5a98f80bd8dc43fd5cef29e699 to your computer and use it in GitHub Desktop.
HTML Canvas boilerplate
<!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>
@danthemango
Copy link
Author

import script from other file: <script src="script.js"></script>,
import css from another file: <link rel="stylesheet" href="style.css">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment