Skip to content

Commit

Permalink
fix: origin for animation
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhenye committed Aug 4, 2024
1 parent 831466a commit e359aea
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
}
.ASS-dialogue {
font-size: 0;
width: max-content;
position: absolute;
z-index: 0;
transform: translate(calc(var(--ass-align-h) * -1), calc(var(--ass-align-v) * -1));
}
.ASS-dialogue span {
display: inline-block;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function createDialogue(dialogue, store) {
const df = document.createDocumentFragment();
const { align, slices, start, end } = dialogue;
[
['--ass-align-h', ['left', 'center', 'right'][align.h]],
['--ass-align-v', ['bottom', 'center', 'top'][align.v]],
['--ass-align-h', ['0%', '50%', '100%'][align.h]],
['--ass-align-v', ['100%', '50%', '0%'][align.v]],
].forEach(([k, v]) => {
$div.style.setProperty(k, v);
});
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,8 @@ export function getPosition(dialogue, store) {
: allocate(dialogue, store);
}
// TODO: use % for x and y
return { x, y };
return {
x: x + [0, width / 2, width][align.h],
y: y + [height, height / 2, 0][align.v],
};
}
2 changes: 1 addition & 1 deletion src/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function renderer(dialogue, store) {
Object.assign(dialogue, { height });
const { x, y } = getPosition(dialogue, store);
Object.assign(dialogue, { x, y });
$div.style.cssText += `width:${width}px;height:${height}px;left:${x}px;top:${y}px;`;
$div.style.cssText += `left:${x}px;top:${y}px;`;
setTransformOrigin(dialogue, store.scale);
Object.assign(dialogue, getClipPath(dialogue, store));
if (dialogue.effect?.name?.startsWith('scroll')) {
Expand Down
14 changes: 4 additions & 10 deletions src/renderer/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ export function createTransform(tag) {

export function setTransformOrigin(dialogue, scale) {
const { align, width, height, x, y, $div } = dialogue;
const org = {};
if (dialogue.org) {
org.x = dialogue.org.x * scale;
org.y = dialogue.org.y * scale;
} else {
org.x = [x, x + width / 2, x + width][align.h];
org.y = [y + height, y + height / 2, y][align.v];
}
const orgX = (dialogue.org ? dialogue.org.x * scale : x) + [0, width / 2, width][align.h];
const orgY = (dialogue.org ? dialogue.org.y * scale : y) + [height, height / 2, 0][align.v];
for (let i = $div.childNodes.length - 1; i >= 0; i -= 1) {
const node = $div.childNodes[i];
if (node.dataset.rotate === '') {
// It's not extremely precise for offsets are round the value to an integer.
const tox = org.x - x - node.offsetLeft;
const toy = org.y - y - node.offsetTop;
const tox = orgX - x - node.offsetLeft;
const toy = orgY - y - node.offsetTop;
node.style.cssText += `transform-origin:${tox}px ${toy}px;`;
}
}
Expand Down

0 comments on commit e359aea

Please sign in to comment.