mouse-event-offset
Advanced tools
Comparing version 2.0.0 to 3.0.0
49
index.js
@@ -0,30 +1,25 @@ | ||
var rootPosition = { left: 0, top: 0 } | ||
module.exports = function offset(ev, options) { | ||
ev = ev || window.event; | ||
module.exports = mouseEventOffset | ||
function mouseEventOffset (ev, target, out) { | ||
target = target || ev.currentTarget || ev.srcElement | ||
if (!Array.isArray(out)) { | ||
out = [ 0, 0 ] | ||
} | ||
var cx = ev.clientX || 0 | ||
var cy = ev.clientY || 0 | ||
var rect = getBoundingClientOffset(target) | ||
out[0] = cx - rect.left | ||
out[1] = cy - rect.top | ||
return out | ||
} | ||
var target = ev.currentTarget || ev.srcElement | ||
var rect = (options && options.clientRect) || getOffset(target), | ||
clientX = options && options.clientX, | ||
clientY = options && options.clientY | ||
clientX = typeof clientX === 'number' ? clientX : ev.clientX | ||
clientY = typeof clientY === 'number' ? clientY : ev.clientY | ||
return { x: clientX - rect.left, y: clientY - rect.top } | ||
function getBoundingClientOffset (element) { | ||
if (element === window || | ||
element === document || | ||
element === document.body) { | ||
return rootPosition | ||
} else { | ||
return element.getBoundingClientRect() | ||
} | ||
} | ||
var tmpRect = { left: 0, top: 0 } | ||
function getOffset(element) { | ||
if (element === document.body || element === window) { | ||
tmpRect.left = 0 | ||
tmpRect.top = 0 | ||
} else { | ||
var r = element.getBoundingClientRect() | ||
tmpRect.left = r.left | ||
tmpRect.top = r.top | ||
} | ||
return tmpRect | ||
} |
{ | ||
"name": "mouse-event-offset", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "get relative position from an event", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,11 +5,12 @@ # mouse-event-offset | ||
Gets the offsetX/offsetY from a mouse event, relative to the top left of the target (i.e. clicked) element. This uses `currentTarget` (i.e. the element that you specified for the event) and falls back to `srcElement`. | ||
Computes the `[ offsetX, offsetY ]` from a mouse or touch event, relative to the top left of the `target` element. | ||
```js | ||
var offset = require('mouse-event-offset') | ||
var events = require('dom-events') | ||
events.on(element, 'click', function(e) { | ||
var pos = offset(e) | ||
console.log(pos.x, pos.y) | ||
window.addEventListener('touchstart', function (ev) { | ||
var target = ev.currentTarget | ||
var touch = ev.changedTouches[0] | ||
var pos = offset(touch, target) | ||
//=> [ 128, 52 ] | ||
}) | ||
@@ -21,12 +22,18 @@ ``` | ||
### `offset(event[, options])` | ||
### `position = offset(event, [target], [out])` | ||
Pass an event object to the function, and a position with `{ x, y }` is returned. The options: | ||
Pass a MouseEvent or TouchEvent as `event`. | ||
- `clientRect` is a pre-computed bounding client rect or offset (e.g. to avoid reflows), defaults to use the target's `getBoundingClientRect()` (or offset `(0, 0)` in the case of window/body) | ||
- `clientX` is the client x position, defaults to `event.clientX` | ||
- `clientY` is the client y position, defaults to `event.clientY` | ||
Optionally, you can specify a `target` element which the touch event should be relative to. Defaults to `event.currentTarget`, falling back to `event.srcElement` for older IE. | ||
You can also specifiy `out` to store the position in that array, and avoid creating a new one. | ||
## Changelog | ||
- 3.0: new version; simpler API, less assumptions, avoids GC thrashing | ||
- 2.0: uses `ev.currentTarget` instead of `ev.target` | ||
- 1.0: simple implementation using `ev.target || ev.srcElement` | ||
## License | ||
MIT, see [LICENSE.md](http://github.com/mattdesl/mouse-event-offset/blob/master/LICENSE.md) for details. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23
38
4243