summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-12-13 21:27:38 +0100
committerTor Andersson <tor@ccxvii.net>2024-01-08 16:36:48 +0100
commit142f36d7300d285e31d7c4b41f0b7779312e3dc6 (patch)
treefef2649c99f894624903400a41bf4c0dc47b551e
parent69730a35b75f73f61d6eb350aaccbdc79cee36aa (diff)
downloadtable-battles-142f36d7300d285e31d7c4b41f0b7779312e3dc6.tar.gz
Fix animation of nested components.
-rw-r--r--play.js37
1 files changed, 22 insertions, 15 deletions
diff --git a/play.js b/play.js
index cdebef5..6d8eca6 100644
--- a/play.js
+++ b/play.js
@@ -36,9 +36,12 @@ function register_animation(e, duration) {
function remember_position(e) {
if (e.parentElement) {
let rect = e.getBoundingClientRect()
+ let prect = e.parentElement.getBoundingClientRect()
e.my_parent = e.parentElement
e.my_x = rect.x
e.my_y = rect.y
+ e.my_p_x = prect.x
+ e.my_p_y = prect.y
} else {
e.my_parent = null
e.my_x = 0
@@ -47,21 +50,25 @@ function remember_position(e) {
}
function animate_position(e) {
- // TODO: fix double-animated dice when cards are removed
- if (e.parentElement) {
- if (e.my_parent) {
- let rect = e.getBoundingClientRect()
- let dx = e.my_x - rect.x
- let dy = e.my_y - rect.y
- if (dx !== 0 || dy !== 0) {
- e.animate(
- [
- { transform: `translate(${dx}px, ${dy}px)`, },
- { transform: "translate(0, 0)", },
- ],
- { duration: e.my_duration, easing: "ease" }
- )
- }
+ if (e.parentElement && e.my_parent) {
+ let dx = 0, dy = 0
+ let rect = e.getBoundingClientRect()
+ if (e.parentElement === e.my_parent) {
+ let prect = e.parentElement.getBoundingClientRect()
+ dx = (e.my_x - e.my_p_x) - (rect.x - prect.x)
+ dy = (e.my_y - e.my_p_y) - (rect.y - prect.y)
+ } else {
+ dx = e.my_x - rect.x
+ dy = e.my_y - rect.y
+ }
+ if (dx !== 0 || dy !== 0) {
+ e.animate(
+ [
+ { transform: `translate(${dx}px, ${dy}px)`, },
+ { transform: "translate(0, 0)", },
+ ],
+ { duration: e.my_duration, easing: "ease" }
+ )
}
}
}