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: inaccurate mouse position #522

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,9 +1422,9 @@ export class Replayer {
return this.debugNodeNotFound(d, id);
}

const base = getBaseDimension(target);
const _x = x + base.x;
const _y = y + base.y;
const base = getBaseDimension(target, this.iframe);
const _x = x * base.absoluteScale + base.x;
const _y = y * base.absoluteScale + base.y;

this.mouse.style.left = `${_x}px`;
this.mouse.style.top = `${_y}px`;
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ export type mediaInteractionCallback = (p: mediaInteractionParam) => void;
export type DocumentDimension = {
x: number;
y: number;
// scale value relative to its parent iframe
relativeScale: number;
// scale value relative to the root iframe
absoluteScale: number;
};

export type Mirror = {
Expand Down
23 changes: 18 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,19 +550,32 @@ export function isIframeINode(node: INode): node is HTMLIFrameINode {
return node.__sn.type === NodeType.Element && node.__sn.tagName === 'iframe';
}

export function getBaseDimension(node: Node): DocumentDimension {
export function getBaseDimension(
node: Node,
rootIframe: Node,
): DocumentDimension {
const frameElement = node.ownerDocument?.defaultView?.frameElement;
if (!frameElement) {
if (!frameElement || frameElement === rootIframe) {
return {
x: 0,
y: 0,
relativeScale: 1,
absoluteScale: 1,
};
}

const frameDimension = frameElement.getBoundingClientRect();
const frameBaseDimension = getBaseDimension(frameElement);
const frameBaseDimension = getBaseDimension(frameElement, rootIframe);
// the iframe element may have a scale transform
const relativeScale = frameDimension.height / frameElement.clientHeight;
return {
x: frameDimension.x + frameBaseDimension.x,
y: frameDimension.y + frameBaseDimension.y,
x:
frameDimension.x * frameBaseDimension.relativeScale +
frameBaseDimension.x,
y:
frameDimension.y * frameBaseDimension.relativeScale +
frameBaseDimension.y,
relativeScale,
absoluteScale: frameBaseDimension.absoluteScale * relativeScale,
};
}
2 changes: 2 additions & 0 deletions typings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ export declare type mediaInteractionCallback = (p: mediaInteractionParam) => voi
export declare type DocumentDimension = {
x: number;
y: number;
relativeScale: number;
absoluteScale: number;
};
export declare type Mirror = {
map: idNodeMap;
Expand Down
2 changes: 1 addition & 1 deletion typings/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ export declare type AppendedIframe = {
builtNode: HTMLIFrameINode;
};
export declare function isIframeINode(node: INode): node is HTMLIFrameINode;
export declare function getBaseDimension(node: Node): DocumentDimension;
export declare function getBaseDimension(node: Node, rootIframe: Node): DocumentDimension;
export {};