forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
167 lines (159 loc) · 4.27 KB
/
vector.js
File metadata and controls
167 lines (159 loc) · 4.27 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
steal.then(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] }
/**
* @class
* A vector class
* @constructor creates a new vector instance from the arguments. Example:
* @codestart
* new jQuery.Vector(1,2)
* @codeend
*
*/
jQuery.Vector = function(){
this.update( jQuery.makeArray(arguments) );
};
jQuery.Vector.prototype =
/* @Prototype*/
{
/**
* Applys the function to every item in the vector. Returns the new vector.
* @param {Function} f
* @return {jQuery.Vector} new vector class.
*/
app: function( f ) {
var newArr = [];
for(var i=0; i < this.array.length; i++)
newArr.push( f( this.array[i] ) );
var vec = new jQuery.Vector();
return vec.update(newArr);
},
/**
* Adds two vectors together. Example:
* @codestart
* new Vector(1,2).plus(2,3) //-> <3,5>
* new Vector(3,5).plus(new Vector(4,5)) //-> <7,10>
* @codeend
* @return {jQuery.Vector}
*/
plus: function() {
var args = arguments[0] instanceof jQuery.Vector ?
arguments[0].array :
jQuery.makeArray(arguments),
arr=this.array.slice(0),
vec = new jQuery.Vector();
for(var i=0; i < args.length; i++)
arr[i] = (arr[i] ? arr[i] : 0) + args[i];
return vec.update(arr);
},
/**
* Like plus but subtracts 2 vectors
* @return {jQuery.Vector}
*/
minus: function() {
var args = arguments[0] instanceof jQuery.Vector ?
arguments[0].array :
jQuery.makeArray(arguments),
arr=this.array.slice(0), vec = new jQuery.Vector();
for(var i=0; i < args.length; i++)
arr[i] = (arr[i] ? arr[i] : 0) - args[i];
return vec.update(arr);
},
/**
* Returns the current vector if it is equal to the vector passed in.
* False if otherwise.
* @return {jQuery.Vector}
*/
equals: function() {
var args = arguments[0] instanceof jQuery.Vector ?
arguments[0].array :
jQuery.makeArray(arguments),
arr=this.array.slice(0), vec = new jQuery.Vector();
for(var i=0; i < args.length; i++)
if(arr[i] != args[i]) return null;
return vec.update(arr);
},
/*
* Returns the 2nd value of the vector
* @return {Number}
*/
x : getSetZero,
width : getSetZero,
/**
* Returns the first value of the vector
* @return {Number}
*/
y : getSetOne,
height : getSetOne,
/**
* Same as x()
* @return {Number}
*/
top : getSetOne,
/**
* same as y()
* @return {Number}
*/
left : getSetZero,
/**
* returns (x,y)
* @return {String}
*/
toString: function() {
return "("+this.array[0]+","+this.array[1]+")";
},
/**
* Replaces the vectors contents
* @param {Object} array
*/
update: function( array ) {
if(this.array){
for(var i =0; i < this.array.length; i++) delete this.array[i];
}
this.array = array;
for(var i =0; i < array.length; i++) this[i]= this.array[i];
return this;
}
};
jQuery.Event.prototype.vector = function(){
if(this.originalEvent.synthetic){
var doc = document.documentElement, body = document.body;
return new jQuery.Vector(this.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0),
this.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0));
}else{
return new jQuery.Vector(this.pageX, this.pageY);
}
}
jQuery.fn.offsetv = function() {
if(this[0] == window){
return new jQuery.Vector(window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft,
window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop)
}else{
var offset = this.offset();
return new jQuery.Vector(offset.left, offset.top);
}
};
jQuery.fn.dimensionsv = function(){
if(this[0] == window)
return new jQuery.Vector(this.width(), this.height());
else
return new jQuery.Vector(this.outerWidth(), this.outerHeight());
}
jQuery.fn.centerv = function(){
return this.offsetv().plus( this.dimensionsv().app(function(u){return u /2;}) )
}
jQuery.fn.makePositioned = function() {
return this.each(function(){
var that = jQuery(this);
var pos = that.css('position');
if (!pos || pos == 'static') {
var style = { position: 'relative' };
if (window.opera) {
style.top = '0px';
style.left = '0px';
}
that.css(style);
}
});
};
})