-
Notifications
You must be signed in to change notification settings - Fork 2
/
snaptut-freetransformplay.js
188 lines (144 loc) · 6.16 KB
/
snaptut-freetransformplay.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
180
181
182
183
184
185
(function() {
var lineAttributes = { stroke: 'red', strokeWidth: 2, strokeDasharray: "5,5" };
Snap.plugin( function( Snap, Element, Paper, global ) {
var ftOption = {
handleFill: "silver",
handleStrokeDash: "5,5",
handleStrokeWidth: "2",
handleLength: "75",
handleRadius: "7",
handleLineWidth: 2,
};
Element.prototype.ftCreateHandles = function() {
this.ftInit();
var freetransEl = this;
var bb = freetransEl.getBBox();
var rotateDragger = this.paper.circle(bb.cx + bb.width + ftOption.handleLength, bb.cy, ftOption.handleRadius ).attr({ fill: ftOption.handleFill });
var translateDragger = this.paper.circle(bb.cx, bb.cy, ftOption.handleRadius ).attr({ fill: ftOption.handleFill });
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();
freetransEl.ftHighlightBB();
return this;
};
Element.prototype.ftInit = function() {
this.data("angle", 0);
this.data("scale", 1);
this.data("tx", 0);
this.data("ty", 0);
return this;
};
Element.prototype.ftCleanUp = function() {
var myClosureEl = this;
var myData = ["angle", "scale", "scaleFactor", "tx", "ty", "otx", "oty", "bb", "bbT", "initialTransformMatrix", "handlesGroup", "joinLine"];
myData.forEach( function( el ) { myClosureEl.removeData([el]) });
return this;
};
Element.prototype.ftStoreStartCenter = function() {
this.data('ocx', this.attr('cx') );
this.data('ocy', this.attr('cy') );
return this;
}
Element.prototype.ftStoreInitialTransformMatrix = function() {
this.data('initialTransformMatrix', this.transform().localMatrix );
return this;
};
Element.prototype.ftGetInitialTransformMatrix = function() {
return this.data('initialTransformMatrix');
};
Element.prototype.ftRemoveHandles = function() {
this.undblclick();
this.data( "handlesGroup").remove();
this.data( "bbT" ) && this.data("bbT").remove();
this.data( "bb" ) && this.data("bb").remove();
this.dblclick( function() { this.ftCreateHandles() } ) ;
this.ftCleanUp();
return this;
};
Element.prototype.ftDrawJoinLine = function( handle ) {
var lineAttributes = { stroke: ftOption.handleFill, strokeWidth: ftOption.handleStrokeWidth, strokeDasharray: ftOption.handleStrokeDash };
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 );
};
return this;
};
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 });
this.data("bbT") && this.ftHighlightBB();
return this;
};
Element.prototype.ftHighlightBB = function() {
this.data("bbT") && this.data("bbT").remove();
this.data("bb") && this.data("bb").remove();
this.data("bbT", this.paper.rect( rectObjFromBB( this.getBBox(1) ) )
.attr({ fill: "none", stroke: ftOption.handleFill, strokeDasharray: ftOption.handleStrokeDash })
.transform( this.transform().global.toString() ) );
this.data("bb", this.paper.rect( rectObjFromBB( this.getBBox() ) )
.attr({ fill: "none", stroke: ftOption.handleFill, strokeDasharray: ftOption.handleStrokeDash }) );
return this;
};
});
function rectObjFromBB ( bb ) {
return { x: bb.x, y: bb.y, width: bb.width, height: bb.height }
}
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) );
}
})();