Skip to content

Instantly share code, notes, and snippets.

@SergeiGolos
Created October 6, 2012 16:32
Show Gist options
  • Save SergeiGolos/3845393 to your computer and use it in GitHub Desktop.
Save SergeiGolos/3845393 to your computer and use it in GitHub Desktop.
TypeScript: Mouse Odometer
class MultiVector {
values: number[];
size: number;
constructor(values: number[], size: number) {
this.values = values;
this.size = size;
}
distance(previous : MultiVector): number {
var temp = 0;
for (var i:number = 0; i< this.size; i++){
var itemElement = this.values[i] - previous.values[i];
temp += itemElement * itemElement;
}
return Math.sqrt(temp);
};
}
class MouseOdometer {
display : any;
distance : number;
lastPoint : MultiVector;
constructor() {
this.display = document.createElement("div");
this.display.innerText = "0"
this.distance = 0;
document.body.appendChild(this.display);
window.onmousemove = e=> {
var newPoint = new MultiVector([e.clientX, e.clientY], 2);
this.distance += newPoint.distance(this.lastPoint || new MultiVector([0,0], 2));
this.lastPoint = newPoint;
this.display.innerText = this.distance.toString();
}
}
}
var x = new MouseOdometer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment