Created
November 26, 2013 15:36
-
-
Save atleastimtrying/7660554 to your computer and use it in GitHub Desktop.
multi touch drag the dots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pos = [0,0] | |
origin = [0,0] | |
init = -> | |
$('.dot').on 'touchstart': startDrag | |
startDrag = (event)=> | |
dot = $ event.currentTarget | |
dot.on | |
'touchmove': moveDrag | |
'touchend': endDrag | |
offset = dot.offset() | |
pos = [offset.left, offset.top] | |
origin = getCoors event | |
moveDrag = (event)=> | |
event.preventDefault() | |
dot = $ event.currentTarget | |
currentPos = getCoors event | |
deltaX = currentPos[0] - @origin[0] | |
deltaY = currentPos[1] - @origin[1] | |
dot.css | |
left: ((@pos[0] + deltaX) + 30) + 'px' | |
top: ((@pos[1] + deltaY) + 30) + 'px' | |
endDrag = (event)=> | |
dot = $ event.currentTarget | |
dot.off | |
'touchmove': moveDrag | |
'touchend': endDrag | |
getCoors = (e)-> | |
event = e.originalEvent | |
coors = [] | |
if event.targetTouches and event.targetTouches.length | |
thisTouch = event.targetTouches[0] | |
coors[0] = thisTouch.clientX | |
coors[1] = thisTouch.clientY | |
else | |
coors[0] = event.clientX | |
coors[1] = event.clientY | |
coors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment