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/26e8d8b06b16e3fdaa56 to your computer and use it in GitHub Desktop.
Save streetalchemist/26e8d8b06b16e3fdaa56 to your computer and use it in GitHub Desktop.
Perfect for touch CoffeeScript
@wheel = $('#Dial')
@angle = 0
if window.MSGesture
gesture = new MSGesture()
gesture.target = @wheel[0]
@wheel.bind("MSGestureChange", @didRotate).bind("MSPointerDown", @addGesturePointer)
else
@wheel.mousedown(@startRotate)
$(window).mousemove(@didRotate).mouseup(@stopRotate)
addGesturePointer:(e)=>
#Handle Touch v. Mouse in IE10
if e.originalEvent.pointerType != 2 #Not Touch Input
e.target.msSetPointerCapture(e.originalEvent.pointerId)
@startRotate(e.originalEvent)
@gesture = false
$(window).bind("MSPointerMove", @didRotate).bind("MSPointerUp", @stopRotate)
else
@gesture && @gesture.addPointer(e.originalEvent.pointerId)
startRotate: (e)=>
document.onselectstart = (e)-> false
@rotating = true
currentPoint = screenPointToSvg(e.clientX, e.clientY, @wheel[0])
angle = Math.atan2(currentPoint[1] - @center_y + 55, currentPoint[0] - @center_x + 90)
angle = angle * LockpickGame.rad2degree
@mouseStartAngle = angle
@dialStartAngle = @angle
didRotate: (e)=>
return unless @rotating
if @gesture
@angle += e.originalEvent.rotation * LockpickGame.rad2degree
else
if e.originalEvent.pointerType
e = e.originalEvent
currentPoint = screenPointToSvg(e.clientX, e.clientY, @wheel[0])
angle = Math.atan2(currentPoint[1] - @center_y - 20, currentPoint[0] - @center_x + 0)
angle = angle * LockpickGame.rad2degree
@angle = @normalizeAngle(@dialStartAngle)+(@normalizeAngle(angle) - @normalizeAngle(@mouseStartAngle))
@center_x = 374.3249938
@center_y = 354.7909851
rotate_transform = "rotate(#{@angle} #{@center_x} #{@center_y})"
requestAnimationFrame(()=>
@wheel.attr("transform", rotate_transform)
)
stopRotate: (e)=>
document.onselectstart = (e)-> true
@rotating = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment