-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_stack.html
More file actions
73 lines (59 loc) · 2.19 KB
/
state_stack.html
File metadata and controls
73 lines (59 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!--
To change this template use Tools | Templates.
-->
<!DOCTYPE html>
<html>
<head>
<title>Canvas 2D Context API Primer</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<!--SCRIPTS-->
<script type="text/javascript">
/*
# Context 2D API: scale
The scale method modifies the transformation by multiplying the matrix by the specified factor.
# Standard Definition.
http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas/#dom-context-2d-scale
# Function signature
void scale(unrestricted double x, unrestricted double y);
*/
function draw(){
// Get a reference to the canvas
var canvas = document.getElementById('canvas');
// Get a reference to the drawing context
var ctx = canvas.getContext('2d');
var rectWidth = 150;
var rectHeight = 75;
ctx.save();
// save state 1
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.save();
// save state 2
ctx.rotate(Math.PI / 4);
ctx.save();
// save state 3
ctx.scale(2, 2);
ctx.fillStyle = 'blue';
ctx.fillRect(rectWidth / -2, rectHeight / -2, rectWidth, rectHeight);
ctx.restore();
// restore state 3
ctx.fillStyle = 'red';
ctx.fillRect(rectWidth / -2, rectHeight / -2, rectWidth, rectHeight);
ctx.restore();
// restore state 2
ctx.fillStyle = 'yellow';
ctx.fillRect(rectWidth / -2, rectHeight / -2, rectWidth, rectHeight);
ctx.restore();
// restore state 1
ctx.fillStyle = 'green';
ctx.fillRect(rectWidth / -2, rectHeight / -2, rectWidth, rectHeight);
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="350" height="400">
This browser or document mode doesn't support canvas
</canvas>
</body>
</html>