This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg_panzoom.js
100 lines (99 loc) · 2.1 KB
/
svg_panzoom.js
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
define(function()
{
function svg_panzoom(el)
{
return new panzoom(el);
}
function panzoom(svgDocument)
{
var box,obox,doc,lastxmouse,lastymouse,dragging,aspect;
function mousewheel(e)
{
if (!e) e = window.event;
var d = e.detail ? -e.detail:e.wheelDelta/120;
var pad = 5;
if( e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}
zoominx(d*pad);
}
function mousedown(e)
{
if (!e) e = window.event;
lastxmouse=e.clientX;
lastymouse=e.clientY;
dragging=true;
}
function mouseup()
{
dragging=false;
}
function mousemove(e)
{
if( dragging)
{
if (!e) e = window.event;
var xmouse=e.clientX,
ymouse=e.clientY,
dx = xmouse-lastxmouse,
dy = ymouse-lastymouse;
lastxmouse=xmouse;
lastymouse=ymouse;
pan( dx,dy);
}
}
function pan(x,y)
{
var rw = box.width/doc.parentNode.clientWidth,
rh = box.height/doc.parentNode.clientHeight,
rr = rw>rh?rw:rh;
box.x -= x*rr;
box.y -= y*rr;
doc.setAttribute('viewBox', [box.x,box.y,box.width,box.height].join(' '));
}
function zoominx(x)
{
if( aspect < 1)
{
var y = x*aspect;
}
else
{
var y=x;
x=y/aspect;
}
box.x += x;
box.y += y;
box.width += -2*x;
box.height += -2*y;
doc.setAttribute('viewBox', [box.x,box.y,box.width,box.height].join(' '));
}
function zoom(r)
{
box.x = obox.x;
box.y = obox.y;
box.width = obox.width;
box.height = obox.height;
zoominx((r-1)*obox.width*0.5);
}
//init
doc = svgDocument;
doc.setAttribute('width','100%');
doc.setAttribute('height','100%');
box = doc.getBBox();
box = { x:box.x,y:box.y,width:box.width,height:box.height};
obox = { x:box.x,y:box.y,width:box.width,height:box.height}; //original box
aspect = obox.height/obox.width;
doc.addEventListener('mousewheel', mousewheel, false);
doc.addEventListener('mousemove', mousemove, false);
doc.addEventListener('mousedown', mousedown, false);
doc.addEventListener('mouseup', mouseup, false);
zoominx(-10);
//public
this.pan=pan;
this.zoom=zoom;
}
return svg_panzoom;
});