-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
61 lines (55 loc) · 1.73 KB
/
example.html
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
<!DOCTYPE HTML>
<html>
<head>
<style>
#test_canvas {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="test_canvas" width="200" height="200"></canvas>
<script src="CanvasMemory.js"></script>
<script>
// init
var canvas = document.querySelector('#test_canvas');
var ctx = canvas.getContext('2d');
var memctx = new CanvasMemory();
memctx.applyTo(ctx);
// some random canvas drawing methods,
// just to loose your coords
memctx.strokeStyle = "black";
memctx.fillStyle = "blue";
memctx.translate( canvas.width/2, canvas.height/2 );
memctx.moveTo(10, 10);
memctx.lineTo(20, 20);
memctx.lineTo(100, 100);
memctx.stroke();
memctx.strokeStyle = "green";
memctx.beginPath();
memctx.rect(10, 10, 10, 10);
memctx.stroke();
memctx.scale(1.2, 1.2);
memctx.rotate(-.3);
memctx.moveTo(50, 50);
memctx.beginPath();
memctx.lineTo(5, 5);
memctx.lineTo(30, 30);
memctx.rect(30, 30, 10, 10);
memctx.closePath();
memctx.stroke();
// and now, if you'd like to get current coordinates...
var pos = memctx.getCurrentCoords();
console.log("current drawing position:", pos);
// or current point of origin (0, 0)...
var origin = memctx.getCurrentOrigin();
console.log("current origin:", origin);
// or position of custom point (x, y) in current transform matrix ...
var point = memctx.getPointInCurrentMatrix(50, 50);
console.log("50, 50 in current transform matrix:", point);
// or go low-level and simply getTransform
var transform = memctx.getTransform();
console.log(transform);
</script>
</body>
</html>