Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
const ctx = canvas.getContext("2d"); ctx.fillStyle = "blue"; ctx.save(); // State 1: blue fill ctx.fillStyle = "red"; ctx.save(); // State 2: red fill ctx.fillStyle = "green"; ctx.fillRect(20, 20, 80, 80); // green ctx.restore(); // Back to State 2: red ctx.fillRect(120, 20, 80, 80); // red ctx.restore(); // Back to State 1: blue ctx.fillRect(220, 20, 80, 80); // blue
Result
Open