Skip to content

Instantly share code, notes, and snippets.

@streetalchemist
Last active December 12, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streetalchemist/40679eb534ee523b3b8a to your computer and use it in GitHub Desktop.
Save streetalchemist/40679eb534ee523b3b8a to your computer and use it in GitHub Desktop.
Perfect for touch JavaScript
var gesture,
_this = this;
this.wheel = $('#Dial');
this.angle = 0;
if (window.MSGesture) {
gesture = new MSGesture();
gesture.target = this.wheel[0];
this.wheel.bind("MSGestureChange", this.didRotate).bind("MSPointerDown", this.addGesturePointer);
} else {
this.wheel.mousedown(this.startRotate);
$(window).mousemove(this.didRotate).mouseup(this.stopRotate);
}
({
addGesturePointer: function(e) {
if (e.originalEvent.pointerType !== 2) {
e.target.msSetPointerCapture(e.originalEvent.pointerId);
_this.startRotate(e.originalEvent);
_this.gesture = false;
return $(window).bind("MSPointerMove", _this.didRotate).bind("MSPointerUp", _this.stopRotate);
} else {
return _this.gesture && _this.gesture.addPointer(e.originalEvent.pointerId);
}
},
startRotate: function(e) {
var angle, currentPoint;
document.onselectstart = function(e) {
return false;
};
_this.rotating = true;
currentPoint = screenPointToSvg(e.clientX, e.clientY, _this.wheel[0]);
angle = Math.atan2(currentPoint[1] - _this.center_y + 55, currentPoint[0] - _this.center_x + 90);
angle = angle * LockpickGame.rad2degree;
_this.mouseStartAngle = angle;
return _this.dialStartAngle = _this.angle;
},
didRotate: function(e) {
var angle, currentPoint, rotate_transform;
if (!_this.rotating) {
return;
}
if (_this.gesture) {
_this.angle += e.originalEvent.rotation * LockpickGame.rad2degree;
} else {
if (e.originalEvent.pointerType) {
e = e.originalEvent;
}
currentPoint = screenPointToSvg(e.clientX, e.clientY, _this.wheel[0]);
angle = Math.atan2(currentPoint[1] - _this.center_y - 20, currentPoint[0] - _this.center_x + 0);
angle = angle * LockpickGame.rad2degree;
_this.angle = _this.normalizeAngle(_this.dialStartAngle) + (_this.normalizeAngle(angle) - _this.normalizeAngle(_this.mouseStartAngle));
}
_this.center_x = 374.3249938;
_this.center_y = 354.7909851;
rotate_transform = "rotate(" + _this.angle + " " + _this.center_x + " " + _this.center_y + ")";
return requestAnimationFrame(function() {
return _this.wheel.attr("transform", rotate_transform);
});
},
stopRotate: function(e) {
document.onselectstart = function(e) {
return true;
};
return _this.rotating = false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment