-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfreetload.html
182 lines (137 loc) · 5.71 KB
/
freetload.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
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
180
<!DOCTYPE html>
<html>
<head>
<title>Snap.svg Freetransform attempt</title>
<script src="analytics.js" async></script>
</head>
<body>
<div id="info1">Info1</div><div id="info2">Info2</div>
<div id="info3">Info3</div><div id="info4">Info4</div>
<div id="info5">Info5</div>
<svg id="svgout"></svg>
<link href="/snapstyle.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="snap.svg.js"></script>
<script>
var s = Snap("#svgout");
(function() {
var lineAttributes = { stroke: 'red', strokeWidth: 2, strokeDasharray: "5,5" };
Snap.plugin( function( Snap, Element, Paper, global ) {
Element.prototype.ftCreateHandles = function() {
this.ftInit();
var freetransEl = this;
var bb = freetransEl.getBBox();
var rotateDragger = this.paper.circle(bb.cx + bb.width + 100, bb.cy, 10).attr({ fill: "red" });
var translateDragger = this.paper.circle(bb.cx, bb.cy, 10).attr({ fill: "red" });
var joinLine = freetransEl.ftDrawJoinLine( rotateDragger );
var handlesGroup = this.paper.g( joinLine, rotateDragger, translateDragger );
freetransEl.data( "handlesGroup", handlesGroup );
freetransEl.data( "joinLine", joinLine);
freetransEl.data( "scaleFactor", calcDistance( bb.cx, bb.cy, rotateDragger.attr('cx'), rotateDragger.attr('cy') ) );
translateDragger.drag( elementDragMove.bind( translateDragger, freetransEl ),
elementDragStart.bind( translateDragger, freetransEl ),
elementDragEnd.bind( translateDragger, freetransEl ) );
freetransEl.undblclick();
freetransEl.data("dblclick", freetransEl.dblclick( function() { this.ftRemoveHandles() } ) );
rotateDragger.drag(
dragHandleRotateMove.bind( rotateDragger, freetransEl ),
dragHandleRotateStart.bind( rotateDragger, freetransEl ),
dragHandleRotateEnd.bind( rotateDragger, freetransEl )
);
freetransEl.ftStoreInitialTransformMatrix();
};
Element.prototype.ftInit = function() {
this.data("angle", 0);
this.data("scale", 1);
this.data("tx", 0);
this.data("ty", 0);
};
Element.prototype.ftCleanUp = function() {
var myClosureEl = this;
var myData = ["angle", "scale", "scaleFactor", "tx", "ty", "otx", "oty", "initialTransformMatrix", "handlesGroup", "joinLine"];
myData.forEach( function( el ) { myClosureEl.removeData([el]) });
};
Element.prototype.ftStoreStartCenter = function() {
this.data('ocx', this.attr('cx') );
this.data('ocy', this.attr('cy') );
}
Element.prototype.ftStoreInitialTransformMatrix = function() {
this.data('initialTransformMatrix', this.transform().localMatrix );
};
Element.prototype.ftGetInitialTransformMatrix = function() {
return this.data('initialTransformMatrix');
};
Element.prototype.ftRemoveHandles = function() {
this.undblclick();
this.data( "handlesGroup").remove();
this.dblclick( function() { this.ftCreateHandles() } ) ;
this.ftCleanUp();
};
Element.prototype.ftDrawJoinLine = function( handle ) {
var rotateHandle = handle.parent()[1];
var thisBB = this.getBBox();
var objtps = this.ftTransformedPoint( thisBB.cx, thisBB.cy);
if( this.data("joinLine") ) {
this.data("joinLine").attr({ x1: objtps.x, y1: objtps.y, x2: rotateHandle.attr('cx'), y2: rotateHandle.attr('cy') });
} else {
return this.paper.line( thisBB.cx, thisBB.cy, handle.attr('cx'), handle.attr('cy') ).attr( lineAttributes );
}
};
Element.prototype.ftTransformedPoint = function( x, y ) {
var transform = this.transform().diffMatrix;
return { x: transform.x( x,y ) , y: transform.y( x,y ) };
};
Element.prototype.ftUpdateTransform = function() {
var tstring = "t" + this.data("tx") + "," + this.data("ty") + this.ftGetInitialTransformMatrix().toTransformString() + "r" + this.data("angle") + 'S' + this.data("scale" );
this.attr({ transform: tstring });
};
});
function elementDragStart( mainEl, x, y, ev ) {
this.parent().selectAll('circle').forEach( function( el, i ) {
el.ftStoreStartCenter();
} );
mainEl.data("otx", mainEl.data("tx") || 0);
mainEl.data("oty", mainEl.data("ty") || 0);
};
function elementDragMove( mainEl, dx, dy, x, y ) {
var dragHandle = this;
this.parent().selectAll('circle').forEach( function( el, i ) {
el.attr({ cx: +el.data('ocx') + dx, cy: +el.data('ocy') + dy });
} );
mainEl.data("tx", mainEl.data("otx") + +dx);
mainEl.data("ty", mainEl.data("oty") + +dy);
mainEl.ftUpdateTransform();
mainEl.ftDrawJoinLine( dragHandle );
}
function elementDragEnd( mainEl, dx, dy, x, y ) {
};
function dragHandleRotateStart( mainElement ) {
this.ftStoreStartCenter();
};
function dragHandleRotateEnd( mainElement ) {
};
function dragHandleRotateMove( mainEl, dx, dy, x, y, event ) {
var handle = this;
var mainBB = mainEl.getBBox();
handle.attr({ cx: +handle.data('ocx') + dx, cy: +handle.data('ocy') + dy });
mainEl.data("angle", Snap.angle( mainBB.cx, mainBB.cy, handle.attr('cx'), handle.attr('cy') ) - 180);
var distance = calcDistance( mainBB.cx, mainBB.cy, handle.attr('cx'), handle.attr('cy') );
mainEl.data("scale", distance / mainEl.data("scaleFactor") );
mainEl.ftUpdateTransform();
mainEl.ftDrawJoinLine( handle );
};
function calcDistance(x1,y1,x2,y2) {
return Math.sqrt( Math.pow( (x1 - x2), 2) + Math.pow( (y1 - y2), 2) );
}
})();
var myEl = s.rect(280,80,200,20).transform('t20,20r20');
var text = s.text(250,20,"Double click main object to add/remove handles, drag centre handle or outer handles to move.");
var text2 = s.text(250,40,"Note this isnt well tested, so dont use in anything important as is!");
myEl.ftCreateHandles();
Snap.load( "Dreaming_tux.svg", function( frag ) {
var g = s.g();
g.append(frag);
g.ftCreateHandles();
} );
</script>
</body>