Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active April 27, 2020 18:29
Show Gist options
  • Select an option

  • Save sandrabosk/c5f28b23893ba1cf53d44fed4d372a85 to your computer and use it in GitHub Desktop.

Select an option

Save sandrabosk/c5f28b23893ba1cf53d44fed4d372a85 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Canvas State</title>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<!-- add js file in the end when the DOM is already loaded -->
<script src="js/index.js"></script>
</body>
</html>
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
const cWidth = canvas.width;
const cHeight = canvas.height;
function drawCanvas(x, y, w, h, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
}
drawCanvas(10, 20, 30, 30, 'turquoise');
ctx.save();
drawCanvas(50, 70, 30, 30, 'orangeRed');
ctx.save();
drawCanvas(120, 150, 30, 30, 'magenta');
ctx.restore();
drawCanvas(200, 70, 30, 30);
// ctx.save(); // => this would give us back orangeRed
ctx.restore();
drawCanvas(250, 20, 30, 30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment