Created
June 21, 2020 08:04
-
-
Save joeng03/a267fa378b0059700d28bb842165bdf5 to your computer and use it in GitHub Desktop.
This file contains 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
function move_by_vector(element,direction,duration=1000) { | |
var elStyle = window.getComputedStyle(element); | |
var x_isNegated =(direction[0]<0)?true:false; | |
var y_isNegated =(direction[1]<0)?true:false; | |
var x_coord= elStyle.getPropertyValue('left').replace("px", ""); | |
var y_coord= elStyle.getPropertyValue('top').replace("px", ""); | |
var x_destination = Number(x_coord) + direction[0]; | |
var y_destination = Number(y_coord) + direction[1]; | |
var x_frameDistance = direction[0]/ (duration / 10); | |
var y_frameDistance = direction[1] / (duration / 10); | |
var x_finished=false; | |
var y_finished=false; | |
function moveAFrame() { | |
elStyle = window.getComputedStyle(element); | |
if(!x_finished){ | |
x_coord = elStyle.getPropertyValue('left').replace("px", ""); | |
var x_newLocation = Number(x_coord) + x_frameDistance; | |
var x_beyondDestination = ( (!x_isNegated && x_newLocation>=x_destination) || (x_isNegated && x_newLocation<=x_destination) );} | |
if(!y_finished){ | |
y_coord = elStyle.getPropertyValue('top').replace("px", ""); | |
var y_newLocation = Number(y_coord) + y_frameDistance; | |
var y_beyondDestination = ( (!y_isNegated && y_newLocation>=y_destination) || (y_isNegated && y_newLocation<=y_destination) );} | |
if (x_beyondDestination) { | |
element.style['left'] = x_destination + "px"; | |
var x_finished=true; | |
} | |
else { | |
element.style['left'] = x_newLocation + "px"; | |
} | |
if (y_beyondDestination) { | |
element.style['top'] = y_destination + "px"; | |
var x_finished=true; | |
} | |
else { | |
element.style['top'] = y_newLocation + "px"; | |
} | |
if(x_finished && y_finished){ | |
clearInterval(movingFrames); | |
} | |
} | |
var movingFrames = setInterval(moveAFrame, 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment