-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsvgtry.html
109 lines (93 loc) · 2.75 KB
/
svgtry.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<script src="jquery-1.8.0.min.js"></script>
<!--
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg"
width="1024px" height="600px" viewbox="-512 -300 1024 600">
<g id="g">
<text x="-400" y="0" fill="black" style="font-size:015">-400</text>
<text x="-300" y="0" fill="black" style="font-size:15">-300</text>
<text x="-200" y="0" fill="black" style="font-size:15">-200</text>
<text x="-100" y="0" fill="black" style="font-size:15">-100</text>
<text x="0" y="0" fill="black" style="font-size:15">0</text>
<text x="100" y="0" fill="black" style="font-size:15">100</text>
<text x="200" y="0" fill="black" style="font-size:15">200</text>
<text x="300" y="0" fill="black" style="font-size:15">300</text>
<text x="400" y="0" fill="black" style="font-size:15">400</text>
</g>
</svg>
-->
<script language="javascript">
// the SVG file and its graphics content
svg = document.getElementById("svg");
g = document.getElementById("g");
// the initial values
var is_zooming = 0;
var is_moving = 0;
var mouse_down = 0;
// the initial view is [-512,+512]
// large values of zoom = zooming in
var zoom = 1;
var xcenter = 0;
var zoomfactor = 1.03;
var mousex;
// bindings (should these be on the SVG instead of the document?)
$(document).mousedown(
function(e) {
mouse_down = 1;
if (e.shiftKey) {shiftmousedown(e);}
else if (e.ctrlKey) {ctrlmousedown(e);}
else {mousedown(e);}
})
$(document).mouseup(mouseup)
$(document).mousemove(
function(e) {
if (mouse_down) {
mousemove(e);
}
}
);
// recenter/scale the document when any change is made
// (doesn't actually redraw, the SVG handles that)
function redraw() {
// var str = "translate(" + -xcenter + ",0) scale(" + zoom +",1)";
// below is either zoom x only or zoom both
var str = "translate(" + -xcenter*zoom + ",0) scale(" + zoom +","+1+")";
// var str = "translate(" + -xcenter*zoom + ",0) scale(" + zoom +","+zoom+")";
// WRONG: var str = "scale(" + zoom +",1) translate("+ -xcenter*zoom + ",0)";
g.setAttribute("transform",str);
}
function mousemove(e) {
xcenter = oldcenter - (e.pageX-oldmouse)/zoom;
redraw();
}
function shiftmousedown() {
is_zooming = 1;
holdzoom(zoomfactor);
}
// when mouse button pressed...
function mousedown(e) {
is_moving = 1;
// record the current xcenter and mouse position for mousemove()
oldcenter = xcenter;
// <h>this old mouse...</h>
oldmouse = e.pageX;
}
function ctrlmousedown(e) {
is_zooming = 1;
holdzoom(1./zoomfactor);
}
function mouseup(e) {
is_zooming = 0;
is_moving = 0;
mouse_down = 0;
redraw();
// alert("Z:"+zoom+", XC:"+xcenter);
}
// keep zooming by zf
function holdzoom(zf) {
if (is_zooming) {
zoom=zoom*zf;
redraw();
setTimeout("holdzoom("+zf+")", 1);
}
}
</script>