-
Notifications
You must be signed in to change notification settings - Fork 10
/
raphael.zoom.js
181 lines (150 loc) · 3.88 KB
/
raphael.zoom.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* raphael.zoom 0.0.4
*
* Copyright (c) 2010 Wout Fierens - http://boysabroad.com
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
// get all elements in the paper
Raphael.fn.elements = function() {
var b = this.bottom,
r = [];
while (b) {
r.push(b);
b = b.next;
}
return r;
}
// initialize zoom of paper
Raphael.fn.initZoom = function(zoom) {
var elements = this.elements();
this.zoom = zoom || 1;
for (var i = 0; i < elements.length; i++) {
elements[i].initZoom();
}
return this;
}
// set the zoom of all elements
Raphael.fn.setZoom = function(zoom) {
if (!zoom) return;
var elements = this.elements();
if (!this.zoom)
this.initZoom();
for (var i = 0; i < elements.length; i++) {
elements[i].setZoom(zoom);
}
this.zoom = zoom;
return this;
}
// initialize zoom of element
Raphael.el.initZoom = function(zoom) {
var sw = parseFloat(this.attr("stroke-width")) || 0;
zoom = zoom || this.paper.zoom;
if (this.type != "text") sw /= zoom;
this.zoom = zoom;
this.zoom_memory = {
"stroke-width": sw,
rotation: 360
};
this.setStrokeWidth(sw / zoom);
if (this.type == "text") {
var fs = parseFloat(this.attr("font-size")) || 0
this.zoom_memory["font-size"] = fs;
this.zoom_memory["x"] = (parseFloat(this.attrs["x"]) || 0) / zoom;
this.zoom_memory["y"] = (parseFloat(this.attrs["y"]) || 0) / zoom;
}
return this;
}
// zoom element preserving some original values
Raphael.el.setZoom = function(zoom) {
if (!zoom) return;
if (!this.zoom_memory)
this.initZoom();
// scale to zoom
var new_zoom = zoom / this.zoom;
this.scale(new_zoom, new_zoom, 0, 0);
this.applyScale();
// save new zoom
this.zoom = zoom;
this.setStrokeWidth(this.zoom_memory["stroke-width"]);
if (this.type == "text")
this.attr({
"font-size": this.zoom_memory["font-size"] * zoom,
"x": this.zoom_memory["x"] * zoom,
"y": this.zoom_memory["y"] * zoom
});
return this;
}
// set element zoomed attributes
Raphael.el.setAttr = function() {
if (typeof arguments[0] == "string") {
attr = {};
attr[arguments[0]] = arguments[1];
} else {
attr = arguments[0];
}
for (var key in attr) {
switch(key) {
case "stroke-width":
this.setStrokeWidth(attr[key]);
break;
case "font-size":
this.setFontSize(attr[key]);
break;
case "x":
case "y":
if (this.type == "text")
this.zoom_memory[key] = attr[key] / this.zoom;
this.attr(key, attr[key]);
break;
default:
this.attr(key, attr[key]);
break;
}
}
return this;
}
// set element translation
Raphael.el.setTranslation = function(x, y) {
if (this.type == "text")
this.setAttr({
x: this.attrs["x"] + x,
y: this.attrs["y"] + y
});
else
this.translate(x,y);
return this;
}
// set element rotation
Raphael.el.setRotation = function(angle, x, y) {
if (!this.zoom_memory) this.initZoom();
if (angle == 0)
angle = 360;
this.rotate(angle, x, y);
this.zoom_memory.rotation = angle;
this.transformations = [];
this._.rt = { cx: null, cy: undefined, deg: 360 };
return this;
}
// set element zoomed stroke width
Raphael.el.setStrokeWidth = function(value) {
if (value == 0 || (value = parseFloat(value))) {
this.attr({ "stroke-width": value * this.zoom });
this.zoom_memory["stroke-width"] = value;
}
return this;
}
// set element font size
Raphael.el.setFontSize = function(value) {
if (value == 0 || (value = parseFloat(value))) {
this.attr({ "font-size": value * this.zoom });
this.zoom_memory["font-size"] = value;
}
return this;
}
// apply the current scale and reset it to 1
Raphael.el.applyScale = function() {
this._.sx = 1;
this._.sy = 1;
this.scale(1, 1);
return this;
}