Skip to content

Instantly share code, notes, and snippets.

@dddent
Last active August 29, 2015 14:16
Show Gist options
  • Save dddent/6e3fabbeb3a9276c2ce6 to your computer and use it in GitHub Desktop.
Save dddent/6e3fabbeb3a9276c2ce6 to your computer and use it in GitHub Desktop.
This is just a file that contains my MyHelper class, in which I add useful classes and function that I need to use often when working with js.
/* Myhelper */
var MyHelper = {}
MyHelper.getValueOfPixelString = function(string) {
if(string.indexOf("px") > -1) {
res = Number(string.replace("px",""));
if(res) return res;
else {
console.log('ERROR in MyHelper.getValueOfPixelString():'
+ ' replacing "px" in passed string "' + string + '" did not return a'
+ ' number');
}
}
else {
console.log('ERROR in MyHelper.getValueOfPixelString():'
+ 'no string "px" found in passed string "' + string + '"');
return false;
}
}
MyHelper.Vector2 = function(x,y) {
this.x = x;
this.y = y;
}
MyHelper.Vector2.prototype.getLength = function() {
return Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2));
}
MyHelper.Vector2.prototype.getVectorTo = function(vector) {
return this.getSubstracted(vector);
}
MyHelper.Vector2.prototype.getDistanceTo = function(vector) {
return this.getVectorTo(vector).getLength();
}
MyHelper.Vector2.prototype.getNormalized = function() {
return new MyHelper.Vector2(
this.x/this.getLength(),
this.y/this.getLength()
)
}
MyHelper.Vector2.prototype.normalize = function() {
var norm = this.getNormalized();
this.x = norm.x;
this.y = norm.y;
}
MyHelper.Vector2.prototype.getAdded = function(vector) {
return new MyHelper.Vector2(this.x + vector.x, this.y + vector.y);
}
MyHelper.Vector2.prototype.add = function (vector) {
this.x += vector.x;
this.y += vector.y;
}
MyHelper.Vector2.prototype.getSubstracted = function (vector) {
return new MyHelper.Vector2(this.x - vector.x, this.y - vector.y);
}
MyHelper.Vector2.prototype.substract = function (vector) {
this.x -= vector.x;
this.y -= vector.y;
}
MyHelper.Vector2.prototype.getMultiplied = function (number) {
return new MyHelper.Vector2(this.x*number, this.y*number);
}
MyHelper.Vector2.prototype.multiply = function (number) {
this.x *= number;
this.y *= number;
}
MyHelper.absToRelPosition = function(x,y,elem) {
return new MyHelper.Vector2(
x-elem.position().left,
y-elem.position().top
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment