Skip to content

Commit a17c5bc

Browse files
committed
update animation anchor and size to appear at sentence/vocab location even when image is present
1 parent b87d0b9 commit a17c5bc

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

script.user.js

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name JPDB Userscript (6a67)
33
// @namespace http://tampermonkey.net/
4-
// @version 0.1.160
4+
// @version 0.1.161
55
// @description Script for JPDB that adds some styling and functionality
66
// @match *://jpdb.io/*
77
// @grant GM_addStyle
@@ -1539,7 +1539,8 @@
15391539
size: { width: 100, height: 100 },
15401540
opacity: 1,
15411541
playBehind: false,
1542-
rotation: 0
1542+
rotation: 0,
1543+
alignTop: false
15431544
};
15441545

15451546
const animOptions = { ...defaultOptions, ...options };
@@ -1570,11 +1571,17 @@
15701571
const containerWidth = lottieContainer.offsetWidth;
15711572
const containerHeight = lottieContainer.offsetHeight;
15721573

1573-
const left = rect.left + (targetWidth - containerWidth) / 2;
1574-
const top = rect.top + (targetHeight - containerHeight) / 2;
1575-
1576-
lottieContainer.style.left = left + 'px';
1577-
lottieContainer.style.top = top + 'px';
1574+
if (animOptions.alignTop) {
1575+
const left = rect.left + (targetWidth - containerWidth) / 2;
1576+
const top = rect.top;
1577+
lottieContainer.style.left = left + 'px';
1578+
lottieContainer.style.top = top + 'px';
1579+
} else {
1580+
const left = rect.left + (targetWidth - containerWidth) / 2;
1581+
const top = rect.top + (targetHeight - containerHeight) / 2;
1582+
lottieContainer.style.left = left + 'px';
1583+
lottieContainer.style.top = top + 'px';
1584+
}
15781585
}
15791586
};
15801587

@@ -1641,22 +1648,43 @@
16411648
}
16421649
const answerBox = document.querySelector('.answer-box');
16431650
const target = answerBox || document.querySelector('.result.kanji')?.querySelector('.plain').firstElementChild;
1644-
const rect = target.getBoundingClientRect();
1651+
let rect = target.getBoundingClientRect();
1652+
1653+
if (target.querySelector('img')) {
1654+
const elements = Array.from(target.children).filter((child) => child.tagName !== 'IMG');
1655+
rect = elements.reduce(
1656+
(acc, el) => {
1657+
const elRect = el.getBoundingClientRect();
1658+
return {
1659+
top: Math.min(acc.top, elRect.top),
1660+
left: Math.min(acc.left, elRect.left),
1661+
right: Math.max(acc.right, elRect.right),
1662+
bottom: Math.max(acc.bottom, elRect.bottom)
1663+
};
1664+
},
1665+
{ top: Infinity, left: Infinity, right: -Infinity, bottom: -Infinity }
1666+
);
1667+
1668+
rect.width = rect.right - rect.left;
1669+
rect.height = rect.bottom - rect.top;
1670+
}
16451671

16461672
if (target) {
16471673
playLottieAnimation(target, WARM['smallFireworkAnimation'], {
16481674
loop: false,
16491675
autoplay: true,
16501676
renderer: 'svg',
16511677
size: { width: rect.height * 3, height: rect.height },
1652-
opacity: 0.5
1678+
opacity: 0.5,
1679+
alignTop: true
16531680
});
16541681
playLottieAnimation(target, WARM['bigFireworkAnimation'], {
16551682
loop: false,
16561683
autoplay: true,
16571684
renderer: 'svg',
16581685
size: { width: rect.height * 3, height: rect.height },
1659-
opacity: 0.5
1686+
opacity: 0.5,
1687+
alignTop: true
16601688
});
16611689

16621690
// const html = document.querySelector('html');
@@ -1810,22 +1838,44 @@
18101838
if (USER_SETTINGS.enableButtonSound()) {
18111839
playSound(CONFIG.soundUrlReveal);
18121840
}
1813-
const rect = target.getBoundingClientRect();
1841+
let rect = target.getBoundingClientRect();
1842+
1843+
if (target.querySelector('img')) {
1844+
const elements = Array.from(target.children).filter((child) => child.tagName !== 'IMG');
1845+
rect = elements.reduce(
1846+
(acc, el) => {
1847+
const elRect = el.getBoundingClientRect();
1848+
return {
1849+
top: Math.min(acc.top, elRect.top),
1850+
left: Math.min(acc.left, elRect.left),
1851+
right: Math.max(acc.right, elRect.right),
1852+
bottom: Math.max(acc.bottom, elRect.bottom)
1853+
};
1854+
},
1855+
{ top: Infinity, left: Infinity, right: -Infinity, bottom: -Infinity }
1856+
);
1857+
1858+
rect.width = rect.right - rect.left;
1859+
rect.height = rect.bottom - rect.top;
1860+
}
1861+
18141862
playLottieAnimation(target, WARM['explosionAnimation'], {
18151863
loop: false,
18161864
autoplay: true,
18171865
renderer: 'svg',
18181866
speed: 1.5,
18191867
size: { width: rect.width, height: rect.height },
1820-
opacity: 0.5
1868+
opacity: 0.5,
1869+
alignTop: true
18211870
});
18221871
playLottieAnimation(target, WARM['bigFireworkAnimation'], {
18231872
loop: false,
18241873
autoplay: true,
18251874
renderer: 'svg',
18261875
speed: 1.5,
18271876
size: { width: rect.width, height: rect.height },
1828-
opacity: 0.5
1877+
opacity: 0.5,
1878+
alignTop: true
18291879
});
18301880
return true;
18311881
}

0 commit comments

Comments
 (0)