Skip to content

Instantly share code, notes, and snippets.

@jaredwilli
Created August 14, 2012 18:45
Show Gist options
  • Select an option

  • Save jaredwilli/3351616 to your computer and use it in GitHub Desktop.

Select an option

Save jaredwilli/3351616 to your computer and use it in GitHub Desktop.
Canvas Vector Class and template thing
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
if (typeof Object.create !== "function") {
Object.create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
}
var timerID = 0;
var tStart = null;
function UpdateTimer() {
if(timerID) {
clearTimeout(timerID);
clockID = 0;
}
if(!tStart)
tStart = new Date();
var tDate = new Date();
var tDiff = tDate.getTime() - tStart.getTime();
tDate.setTime(tDiff);
document.theTimer.theTime.value = ""
+ tDate.getMinutes() + ":"
+ tDate.getSeconds();
timerID = setTimeout("UpdateTimer()", 1000);
}
function Start() {
tStart = new Date();
document.theTimer.theTime.value = "00:00";
timerID = setTimeout("UpdateTimer()", 1000);
}
function Stop() {
if(timerID) {
clearTimeout(timerID);
timerID = 0;
}
tStart = null;
}
function Reset() {
tStart = null;
document.theTimer.theTime.value = "00:00";
}
//-->
</script>
// http://www.keyframesandcode.com/resources/javascript/deconstructed/jquery/
var Vector = {
init: function() {},
prototype: {
init: function() {}
},
create: function() {
var object = Object.create(this);
object.parent = this;
object.init.apply(object, arguments);
return object;
},
instance: function() {
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance, arguments);
return instance;
},
proxy: function() {
var thisObject = this;
return (function() {
return func.apply(thisObject, arguments);
});
},
include: function(obj) {
var included = obj.included || obj.setup;
delete obj.included;
delete obj.extended;
delete obj.setup;
for (var i in obj) {
this.fn[i] = obj[i];
}
if (included) {
included.apply(this);
}
},
extend: function(obj) {
var extended = obj.extended || obj.setup;
delete obj.included
delete obj.extended;
delete obj.setup;
for (var i in obj) {
this.fn[i] = obj[i];
}
if (extended) {
extended.apply(this);
}
delete extended;
}
};
Vector.fn = Vector.prototype;
Vector.fn.proxy = Vector.proxy;
var Vector = function(x, y) {
this.x = x;
this.y = y;
return this;
};
Vector.prototype = {
x: 0,
y: 0,
clone: function() {
return new Vector(this.x, this.y);
},
set: function(v) {
this.x = v.x;
this.y = v.y;
return this;
},
add: function(v) {
this.x += v.x;
this.y += v.y;
return this;
},
sub: function(v) {
this.x -= v.x;
this.y -= v.y;
return this;
},
dot: function(v) {
return this.x * v.x + this.y * v.y;
},
length: function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
distance: function(v) {
var xx = this.x - v.x;
var yy = this.y - v.y;
return Math.sqrt(xx * xx + yy * yy);
},
theta: function() {
return Math.atan2(this.y, this.x);
},
norm: function() {
var len = this.length();
this.x /= len;
this.y /= len;
return this;
},
rotate: function(a) {
var ca = Math.cos(a);
var sa = Math.sin(a);
with(this) {
var rx = x * ca - y * sa;
var ry = x * sa + y * ca;
x = rx;
y = ry;
}
return this;
},
invert: function() {
this.x = -this.x;
this.y = -this.y;
return this;
},
scale: function(s) {
this.x *= s;
this.y *= s;
return this;
}
};
var Vector3D = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
};
Vector3D.prototype = {
x: 0,
y: 0,
z: 0,
clone: function() {
return new Vector(this.x, this.y, this.z);
},
set: function(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
},
add: function(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
},
sub: function(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
},
dot: function(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
},
length: function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
cross: function(a) {
return new Vector3D(this.y * a.z - this.z * a.y, this.z * a.x - this.x * a.z, this.x * a.y - this.y * a.x);
},
distance: function(b) {
var c = this.x - b.x;
var d = this.y - b.y;
var a = this.z - b.z;
return Math.sqrt(c * c + d * d + a * a);
},
thetaTo: function(b) {
var a = this.y * b.z - this.z * b.y,
d = this.z * b.x - this.x * b.z,
c = this.x * b.y - this.y * b.x;
return Math.atan2( Math.sqrt(a * a + d * d + c * c), this.dot(b));
},
scale: function(s) {
this.x *= s;
this.y *= s;
this.z *= s
return this;
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment