git @ Cat's Eye Technologies Dipple / master javascript / full-browser-canvas.html
master

Tree @master (Download .tar.gz)

full-browser-canvas.html @masterraw · history · blame

<!DOCTYPE html>
<!--
SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
For more information, please refer to <https://unlicense.org/>
SPDX-License-Identifier: Unlicense
-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Full Browser Canvas</title>
  <style>
html, body, canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    line-height: 0;  /* this is important - removes gap at bottom */
}
  </style>
</head>
<body>
<canvas id="canvas">
Your browser doesn't support displaying an HTML5 canvas.
</canvas>
</body>
<script>
var draw = function() {
    var canvas = document.getElementById('canvas');
    canvas.width = canvas.clientWidth;
    canvas.height = canvas.clientHeight;
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = 'red';
    ctx.fillRect(0, 0, canvas.width / 2, canvas.height / 2);
    ctx.fillStyle = 'blue';
    ctx.fillRect(canvas.width / 2, canvas.height / 2, canvas.width, canvas.height);
};
window.addEventListener("load", draw);
window.addEventListener("resize", draw);
</script>