forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
248 lines (242 loc) · 6.43 KB
/
vector.js
File metadata and controls
248 lines (242 loc) · 6.43 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
steal('jquery', function($){
var getSetZero = function(v){ return v !== undefined ? (this.array[0] = v) : this.array[0] },
getSetOne = function(v){ return v !== undefined ? (this.array[1] = v) : this.array[1]};
/**
* @constructor jQuery.Vector
* @parent jquerypp
*
* `jQuery.Vector` represents a multi dimensional vector with shorthand methods for
* working with two dimensions.
*
* It is mainly used in [jQuery.event.drag drag] & [jQuery.event.drop drop] events.
*
* new jQuery.Vector(1,2)
*/
$.Vector = function(arr) {
var array = $.isArray(arr) ? arr : $.makeArray(arguments);
this.update(array);
};
$.Vector.prototype =
/* @Prototype*/
{
/**
* @function jQuery.Vector.prototype.app app
* @signature `jQuery.vector.app([f])`
*
* @body
* Applies the function to every item in the vector and returns a new vector.
*
* @param {Function} f The function to apply
* @return {jQuery.Vector} A new $.Vector instance
*/
app: function( f ) {
var i, newArr = [];
for ( i = 0; i < this.array.length; i++ ) {
newArr.push(f(this.array[i], i));
}
return new $.Vector(newArr);
},
/**
* @function jQuery.Vector.prototype.plus plus
* @signature `jQuery.vector.plus()`
*
* @body
*
* Adds two vectors together and returns a new instance. Example:
*
* new $.Vector(1,2).plus(2,3) //-> (3, 5)
* new $.Vector(3,5).plus(new Vector(4,5)) //-> (7, 10)
*
* @return {$.Vector}
*/
plus: function() {
var i, args = arguments[0] instanceof $.Vector ? arguments[0].array : $.makeArray(arguments),
arr = this.array.slice(0),
vec = new $.Vector();
for ( i = 0; i < args.length; i++ ) {
arr[i] = (arr[i] ? arr[i] : 0) + args[i];
}
return vec.update(arr);
},
/**
* @function jQuery.Vector.prototype.minus minus
* @signature `jQuery.vector.minus()`
*
* @body
* Subtract one vector from another and returns a new instance. Example:
*
* new $.Vector(4, 5).minus(2, 1) //-> (2, 4)
*
* @return {jQuery.Vector}
*/
minus: function() {
var i, args = arguments[0] instanceof $.Vector ? arguments[0].array : $.makeArray(arguments),
arr = this.array.slice(0),
vec = new $.Vector();
for ( i = 0; i < args.length; i++ ) {
arr[i] = (arr[i] ? arr[i] : 0) - args[i];
}
return vec.update(arr);
},
/**
* @function jQuery.Vector.prototype.equals equals
* @signature `jQuery.vector.equal()`
*
* @body
*
* @return {jQuery.Vector}
* Returns the current vector if it is equal to the vector passed in.
*
* `null` if otherwise.
*/
equals: function() {
var i, args = arguments[0] instanceof $.Vector ? arguments[0].array : $.makeArray(arguments),
arr = this.array.slice(0),
vec = new $.Vector();
for ( i = 0; i < args.length; i++ ) {
if ( arr[i] != args[i] ) {
return null;
}
}
return vec.update(arr);
},
/**
* @function jQuery.Vector.prototype.x x
* @signature `jQuery.vector.x()`
*
* @body
* Returns the first value of the vector.
* You can also access the same value through the following aliases the
* vector.left() and vector.width()
* aliases.
*
* For example:
*
* var v = new $.Vector(2, 5);
* v.x() //-> 2
* v.left() //-> 2
* v.width() //-> 2
*
* @return {Number} The first value of the vector
*/
x: getSetZero,
/**
* @hide
* Alias for [jQuery.Vector.prototype.x].
*
* @return {Number}
*/
left: getSetZero,
/**
* @hide
* Alias for [jQuery.Vector.prototype.x].
*
* @return {Number}
*/
width: getSetZero,
/**
* @function jQuery.Vector.prototype.y y
* @signature `jQuery.vector.y()`
*
* @body
* Returns the second value of the vector.
* You can also access the same value through the [jQuery.Vector.prototype.top vector.top()]
* and [jQuery.Vector.prototype.height vector.height()] aliases.
*
* For example:
*
* var v = new $.Vector(2, 5);
* v.y() //-> 5
* v.top() //-> 5
* v.height() //-> 5
*
* @return {Number} The first value of the vector
*/
y: getSetOne,
/**
* @hide
* Alias for [jQuery.Vector.prototype.y].
*
* @return {Number}
*/
top: getSetOne,
/**
* @hide
* Alias for [jQuery.Vector.prototype.y].
*
* @return {Number}
*/
height: getSetOne,
/**
* @function jQuery.Vector.prototype.toString toString
* @signature `jQuery.vector.toString()`
*
* @body
*
* Returns a string representation of the vector in the form of (x,y,...)
*
* var v = new $.Vector(4, 6, 1, 3);
* v.toString() //-> (4, 6, 1, 3)
*
* @return {String}
*/
toString: function() {
return "(" + this.array.join(', ') + ")";
},
/**
* @function jQuery.Vector.prototype.update update
* @signature `jQuery.vector.update([array])`
*
* @body
*
* Replaces the vectors contents
*
* var v = new $.Vector(2, 3);
*
* @param {Object} array
*/
update: function( array ) {
var i;
if ( this.array ) {
for ( i = 0; i < this.array.length; i++ ) {
delete this.array[i];
}
}
this.array = array;
for ( i = 0; i < array.length; i++ ) {
this[i] = this.array[i];
}
return this;
}
};
$.Event.prototype.vector = function() {
// Get the first touch element for touch events
var touches = "ontouchend" in document && this.originalEvent.changedTouches &&
this.originalEvent.changedTouches.length ? this.originalEvent.changedTouches[0] : this;
if ( this.originalEvent.synthetic ) {
var doc = document.documentElement,
body = document.body;
return new $.Vector(touches.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0),
touches.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0));
} else {
return new $.Vector(touches.pageX, touches.pageY);
}
};
$.fn.offsetv = function() {
if ( this[0] == window ) {
return new $.Vector(window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft, window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop);
} else {
var offset = this.offset() || {};
return new $.Vector(offset.left, offset.top);
}
};
$.fn.dimensionsv = function( which ) {
if ( this[0] == window || !which ) {
return new $.Vector(this.width(), this.height());
}
else {
return new $.Vector(this[which + "Width"](), this[which + "Height"]());
}
};
return $;
});