Last active
January 18, 2018 09:46
-
-
Save dennisslimmers/903e3c88fa2dc8fe4bcd7df7033ebc1b to your computer and use it in GitHub Desktop.
Typescript code for calculating relative coordinates in %
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private calculateRelativeCoords(event: MouseEvent): IRelativeCoords { | |
const target: JQuery = $(this.target); | |
const offset = target.offset(); | |
const parentOffset = target.offsetParent(); | |
let relativeX = ((event.pageX - 15) - offset.left); | |
let relativeY = ((event.pageY - 15) - offset.top); | |
relativeX = (relativeX / target.width()) * 100; | |
relativeY = (relativeY / target.height()) * 100; | |
relativeX = Math.round(relativeX * 1000) / 1000; | |
relativeY = Math.round(relativeY * 1000) / 1000; | |
return { relativeX: relativeX, relativeY: relativeY } as IRelativeCoords; | |
} | |
export interface IRelativeCoords { | |
relativeX: number; | |
relativeY: number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment