JS Quiz: Hard: Canvas Context Restoration State Stack Order

Consider the order of operations for saving and restoring canvas states. What color will the final rectangle be?

const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

ctx.fillStyle = 'red';
ctx.save();

ctx.fillStyle = 'blue';
ctx.save();

ctx.fillStyle = 'green';
ctx.restore();

ctx.fillRect(0, 0, 10, 10);
  • red
  • blue
  • green
  • black (default)
0 voters