vuikit
Version:
A responsive Vue UI library for web site interfaces based on UIkit
81 lines (76 loc) • 2.22 kB
JavaScript
/**
* Vuikit 0.8.10
* (c) 2018 Miljan Aleksic
* @license MIT
**/
/* Substantial part of the code is adapted from UIkit,
Copyright (c) 2013-2018 YOOtheme GmbH, getuikit.com */
import { on } from './event';
import { offset } from './dimensions';
function MouseTracker () {}
MouseTracker.prototype = {
positions: [],
position: null,
init: function init () {
var this$1 = this;
this.positions = [];
this.position = null;
var ticking = false;
this.unbind = on(document, 'mousemove', function (e) {
if (ticking) {
return
}
setTimeout(function () {
var time = Date.now();
var ref = this$1.positions;
var length = ref.length;
if (length && (time - this$1.positions[length - 1].time > 100)) {
this$1.positions.splice(0, length);
}
this$1.positions.push({time: time, x: e.pageX, y: e.pageY});
if (this$1.positions.length > 5) {
this$1.positions.shift();
}
ticking = false;
}, 5);
ticking = true;
});
},
cancel: function cancel () {
if (this.unbind) {
this.unbind();
}
},
movesTo: function movesTo (target) {
if (this.positions.length < 2) {
return false
}
var p = offset(target);
var position = this.positions[this.positions.length - 1];
var ref = this.positions;
var prevPos = ref[0];
if (p.left <= position.x && position.x <= p.right && p.top <= position.y && position.y <= p.bottom) {
return false
}
var points = [
[{x: p.left, y: p.top}, {x: p.right, y: p.bottom}],
[{x: p.right, y: p.top}, {x: p.left, y: p.bottom}]
];
if (p.right <= position.x) {
} else if (p.left >= position.x) {
points[0].reverse();
points[1].reverse();
} else if (p.bottom <= position.y) {
points[0].reverse();
} else if (p.top >= position.y) {
points[1].reverse();
}
return !!points.reduce(function (result, point) {
return result + (slope(prevPos, point[0]) < slope(position, point[0]) && slope(prevPos, point[1]) > slope(position, point[1]))
}, 0)
}
};
function slope (a, b) {
return (b.y - a.y) / (b.x - a.x)
}
export { MouseTracker };