Save and
restoring attributes on canvas
When you use the save() method, all the attributes that you define before save()
are pushed into a stack You can push any number of attribute sets into the stack.
You can then ues the restore() method to pop each set of attributes out of the stack
in the reverse order in which they were pushed in.
In the following example, I am saving three sets of attributes and then restoring
them one by one for every fillText() method. When you execute the code, notice that
the attributes of the text get applied in the reverse order.
What can you store using the save() method?
You can save the following attributes: lineWidth, lineCap, lineJoin, miterLimit,
strokeStyle, fillStyle, createLinearGradient, createRadialGradient, createPatterns,
shadowColor, shadowOffsetX, shadowOffsetY, shadowBlur, globalCompositeOperation,
font, textAlign, textBaseline.
You can also save transformations, and clipping regions.
HTML Code
<html>
<head>
<script type="application/javascript">
function init() {
var canvas = document.getElementById("canvas");
if (canvas.getContext)
{
var ctx = canvas.getContext("2d");
Note: Try out the other compositing options in the above code to see how they work.
Also, note that the globalCompositeOperation attribute affects all drawing on the
canvas.