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
150 lines (142 loc) · 3.78 KB
/
vector.js
File metadata and controls
150 lines (142 loc) · 3.78 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
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 jQuery.Vector
* A vector class
* @constructor creates a new vector instance from the arguments. Example:
* @codestart
* new jQuery.Vector(1,2)
* @codeend
*
*/
$.Vector = function(){
this.update( $.makeArray(arguments) );
};
$.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 $.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 {$.Vector}
*/
plus: function() {
var args = arguments[0] instanceof $.Vector ?
arguments[0].array :
$.makeArray(arguments),
arr=this.array.slice(0),
vec = new $.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 $.Vector ?
arguments[0].array :
$.makeArray(arguments),
arr=this.array.slice(0), vec = new $.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 $.Vector ?
arguments[0].array :
$.makeArray(arguments),
arr=this.array.slice(0), vec = new $.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;
}
};
$.Event.prototype.vector = function(){
if(this.originalEvent.synthetic){
var doc = document.documentElement, body = document.body;
return new $.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 $.Vector(this.pageX, this.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"]());
}
})