Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan Creamer
committed
Sep 25, 2020
1 parent
c2b67c8
commit eeef969
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const findPath = (element) => { | ||
const path = []; | ||
let current = element; | ||
|
||
while (current.parentNode) { | ||
const index = current.parentNode.children.indexOf(current); | ||
path.push(index); | ||
current = current.parentNode; | ||
} | ||
|
||
return path; | ||
} | ||
|
||
const walkPath = (root, path) => { | ||
let current = root; | ||
|
||
while (path.length) { | ||
current = current.children[path.pop()]; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
const findElement = (root, element) => { | ||
const path = findPath(element); | ||
const foundIt = walkPath(root, path); | ||
|
||
return foundIt; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const paint = (start, elapsed, duration, animate) => { | ||
if (elapsed < duration) { | ||
animate(elapsed); | ||
requestAnimationFrame((step) => paint(start, step - start, duration, animate)); | ||
} | ||
} | ||
|
||
const translate = (el, distance = 0, direction = 'X') => { | ||
el.dataset.translate = el.dataset.translate || JSON.stringify({ | ||
X: distance, | ||
Y: distance, | ||
}); | ||
const saved = JSON.parse(el.dataset.translate); | ||
saved[direction] = distance; | ||
el.dataset.translate = JSON.stringify(saved); | ||
|
||
el.style.transform = `translate${direction}(${saved[direction]}px)`; | ||
}; | ||
|
||
const moveElement = (duration, distance, direction, element) => { | ||
const onFrame = ( | ||
elapsed | ||
) => { | ||
const progress = elapsed / duration; | ||
const amount = progress * distance; | ||
|
||
translate(element, Math.min(amount, duration), direction); | ||
}; | ||
|
||
requestAnimationFrame((step) => paint(step, 0, duration, onFrame)); | ||
} | ||
moveElement(10000, 750, 'X', button); |