-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimag.js
94 lines (92 loc) · 3.21 KB
/
minimag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// ------------------------------------------
// Minimag.js
// Micro library to make dom elements react magnetically to the mouse
// Copyright (c) 2020 Demilade Olaleye (@demiladeHQ)
// MIT license
// ------------------------------------------
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but only CommonJS-like environments that support module.exports,like Node.
module.exports = factory()
} else {
// Browser globals (root is window)
root.Minimag = factory()
}
}(typeof window !== 'undefined' ? window : global, function () {
var Minimag = function () {
'use strict'
var mouseTimeout = null
var mouse = {
x: 0,
y: 0,
moving: false
}
var transformStyles = {
tx: { previous: 0, current: 0, ease: 0.1 },
ty: { previous: 0, current: 0, ease: 0.1 }
}
var defaults = {
distance: 40,
distanceMultiplier: 20,
friction: 0.5
}
var minimagEls = document.querySelectorAll('[data-minimag]')
function lerp (a, b, n) {
return (1 - n) * a + n * b
};
function getDistance (x1, y1, x2, y2) {
var a = x1 - x2
var b = y1 - y2
return Math.hypot(a, b)
};
function setActive (el) {
el.dataset.minimag = 'active'
}
function removeActive (el) {
el.dataset.minimag = ''
}
function magnetize () {
if (mouse.moving) {
minimagEls.forEach(el => {
var elRect = el.getBoundingClientRect()
var elCenterX = elRect.left + elRect.width / 2
var elCenterY = elRect.top + elRect.height / 2
var triggerDistance = el.dataset.magDist * defaults.distanceMultiplier || defaults.distance
var direction = triggerDistance < 0 ? -1 : 1
var deltaX = ((mouse.x - elCenterX) * defaults.friction) * direction
var deltaY = ((mouse.y - elCenterY) * defaults.friction) * direction
var distanceFromEl = getDistance(mouse.x, mouse.y, elCenterX, elCenterY)
if (distanceFromEl < Math.abs(triggerDistance)) {
setActive(el)
el.style.transition = ''
transformStyles.tx.current = deltaX
transformStyles.ty.current = deltaY
for (var key in transformStyles) {
transformStyles[key].previous = lerp(transformStyles[key].previous, transformStyles[key].current, transformStyles[key].ease)
}
el.style.transform = 'translate3d('.concat(transformStyles.tx.previous, 'px, ').concat(transformStyles.ty.previous, 'px, 0)')
} else {
el.style.transform = null
el.style.transition = 'transform .4s cubic-bezier(.75,-0.5,0,1.75)'
removeActive(el)
};
})
};
requestAnimationFrame(magnetize)
};
magnetize()
document.addEventListener('mousemove', function (e) {
mouse.moving = true
mouse.x = e.pageX
mouse.y = e.pageY
clearTimeout(mouseTimeout)
mouseTimeout = setTimeout(() => {
mouse.moving = false
}, 100)
})
}
return Minimag
}))