Skip to content

Instantly share code, notes, and snippets.

@mmmpa
Created April 19, 2014 13:50
Show Gist options
  • Save mmmpa/11084980 to your computer and use it in GitHub Desktop.
Save mmmpa/11084980 to your computer and use it in GitHub Desktop.
class Drager {
public start_x: number = 0;
public start_y: number = 0;
public move_total_x: number = 0;
public move_total_y: number = 0;
public move_x: number = 0;
public move_y: number = 0;
public now_x: number = 0;
public now_y: number = 0;
public start_distance: number = 0;
public move_total_distance: number = 0;
public move_distance: number = 0;
public now_distance: number = 0;
constructor(start: JQueryEventObject, draging: any, draged: any, pinch: any = null) {
var move_func: any;
if (start.originalEvent.touches) {
var touch = start.originalEvent.touches[0];
this.init(touch.pageX, touch.pageY);
$(window)
.bind('touchmove', move_func = (e: JQueryEventObject) => {
var touch = e.originalEvent.touches[0];
this.compute(touch.pageX, touch.pageY);
if (e.originalEvent.touches.length >= 2) {
var touch1 = e.originalEvent.touches[1];
var w: number = touch.pageX - touch1.pageX;
var h: number = touch.pageY - touch1.pageY;
if (this.start_distance === 0) {
this.start_distance = Drager.getDistance(w, h);
this.now_distance = this.start_distance
} else {
var distance: number = Drager.getDistance(w, h)
this.move_total_distance = distance - this.start_distance;
this.move_distance = distance - this.now_distance;
this.now_distance = distance;
}
pinch && pinch(this);
}
draging(this);
})
.one('touchend', (e: JQueryEventObject) => {
$(window).unbind('touchmove', move_func);
draged(this);
delete this;
});
} else {
this.init(start.pageX, start.pageY);
$(window)
.bind('mousemove', move_func = (e: JQueryEventObject) => {
this.compute(e.pageX, e.pageY);
draging(this);
})
.one('mouseup', (e: JQueryEventObject) => {
$(window).unbind('mousemove', move_func);
draged(this);
delete this;
});
}
}
init(x: number, y: number) {
this.start_x = x;
this.start_y = y;
this.now_x = x;
this.now_y = y;
}
compute(x: number, y: number) {
this.move_total_x = x - this.start_x;
this.move_total_y = y - this.start_y;
this.move_x = x - this.now_x;
this.move_y = y - this.now_y;
this.now_x = x;
this.now_y = y;
}
static getDistance(w: number, h: number) {
return Math.sqrt(w * w + h * h);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment