Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dom): account for translated parent in pointer position #7079

Merged
merged 5 commits into from
Feb 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/js/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from '../fullscreen-api';
import log from './log.js';
import {isObject} from './obj';
import computedStyle from './computed-style';
import * as browser from './browser';

/**
* Detect if a value is a string with any non-whitespace characters.
Expand Down Expand Up @@ -608,6 +609,33 @@ export function findPosition(el) {
*
*/
export function getPointerPosition(el, event) {
const translated = {
x: 0,
y: 0
};

if (browser.IS_IOS) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on iOS, iterate up the DOM tree and add up all the translation transforms so that we can modify the pageX and pageY coordinates for changedTouches to account for it.

let item = el;

while (item && item.nodeName.toLowerCase() !== 'html') {
const transform = computedStyle(item, 'transform');

if (/^matrix/.test(transform)) {
const values = transform.slice(7, -1).split(/,\s/).map(Number);

translated.x += values[4];
translated.y += values[5];
} else if (/^matrix3d/.test(transform)) {
const values = transform.slice(9, -1).split(/,\s/).map(Number);

translated.x += values[12];
translated.y += values[13];
}

item = item.parentNode;
}
}

const position = {};
const boxTarget = findPosition(event.target);
const box = findPosition(el);
Expand All @@ -619,6 +647,10 @@ export function getPointerPosition(el, event) {
if (event.changedTouches) {
offsetX = event.changedTouches[0].pageX - box.left;
offsetY = event.changedTouches[0].pageY + box.top;
if (browser.IS_IOS) {
offsetX -= translated.x;
offsetY -= translated.y;
}
}

position.y = (1 - Math.max(0, Math.min(1, offsetY / boxH)));
Expand Down