-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.dist.js
More file actions
302 lines (255 loc) · 9.36 KB
/
example.dist.js
File metadata and controls
302 lines (255 loc) · 9.36 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
var _core = _interopRequireDefault(require("@keyframes/core"));
var _keyframes = _interopRequireDefault(require("../src/keyframes.proximity"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
_core.default.plugin(_keyframes.default);
var $ = function $(selector) {
return document.querySelectorAll(selector)[0];
};
var blockElem = $('.block');
var bc2 = $('.block-container.bc2').getBoundingClientRect();
var block = new _core.default(blockElem);
var topPosition = {
top: '20px'
};
_core.default.define([{
name: 'initiate',
'0%': {
top: '-100px'
},
'100%': topPosition
}, {
name: 'makeTheMove',
'0%': topPosition,
'100%': {
top: "".concat(bc2.top - 20, "px")
}
}]);
window.onload = function () {
var readyToProximity = false;
var isMoving = false;
var directionSwitch = true;
block.play('initiate 1s ease 0s 1 normal forwards', function () {
readyToProximity = true;
});
block.proximity(function (obj) {
if (readyToProximity) {
blockElem.style.boxShadow = "red 0 0 ".concat(0.5 * obj.distancePercentage, "px");
}
});
blockElem.addEventListener('mouseover', function (e) {
if (!isMoving) {
isMoving = true;
var direction = directionSwitch ? 'normal' : 'reverse';
directionSwitch = !directionSwitch;
block.reset(function () {
return block.play("makeTheMove 2s ease 0s 1 ".concat(direction, " forwards"), function () {
isMoving = false;
});
});
}
});
};
},{"../src/keyframes.proximity":3,"@keyframes/core":2}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Keyframes =
/*#__PURE__*/
function () {
function Keyframes(elem) {
_classCallCheck(this, Keyframes);
this.elem = elem;
}
_createClass(Keyframes, [{
key: "isSupported",
value: function isSupported() {
return document.body.style.animationName !== undefined;
}
}, {
key: "reset",
value: function reset(callback) {
this.elem.style.animationPlayState = 'running';
this.elem.style.animation = 'none';
if (callback) {
setTimeout(callback, 0);
}
}
}, {
key: "pause",
value: function pause() {
this.elem.style.animationPlayState = 'paused';
}
}, {
key: "resume",
value: function resume() {
this.elem.style.animationPlayState = 'running';
}
}, {
key: "play",
value: function play(frameOptions, callback) {
var _this = this;
var animObjToStr = function animObjToStr(obj) {
var newObj = Object.assign({}, {
duration: '0s',
timingFunction: 'ease',
delay: '0s',
iterationCount: 1,
direction: 'normal',
fillMode: 'forwards'
}, obj);
return [newObj.name, newObj.duration, newObj.timingFunction, newObj.delay, newObj.iterationCount, newObj.direction, newObj.fillMode].join(' ');
};
var animationcss = '';
if (frameOptions.constructor === Array) {
var frameOptionsStrings = [];
for (var i = 0; i < frameOptions.length; i += 1) {
frameOptionsStrings.push(typeof frameOptions[i] === 'string' ? frameOptions[i] : animObjToStr(frameOptions[i]));
}
animationcss = frameOptionsStrings.join(', ');
} else if (typeof frameOptions === 'string') {
animationcss = frameOptions;
} else {
animationcss = animObjToStr(frameOptions);
}
var addEvent = function addEvent(type, eventCallback) {
var listenerName = "".concat(type, "Listener");
_this.elem.removeEventListener(type, _this[listenerName]);
_this[listenerName] = eventCallback;
_this.elem.addEventListener(type, _this[listenerName]);
};
this.elem.style.animationPlayState = 'running';
this.elem.style.animation = animationcss;
this.frameOptions = frameOptions;
addEvent('animationiteration', callback || frameOptions.complete);
addEvent('animationend', callback || frameOptions.complete);
}
}], [{
key: "createKeyframeTag",
value: function createKeyframeTag(id, css) {
var elem = document.createElement('style');
elem.innerHTML = css;
elem.setAttribute('class', 'keyframe-style');
elem.setAttribute('id', id);
elem.setAttribute('type', 'text/css');
document.getElementsByTagName('head')[0].appendChild(elem);
}
}, {
key: "generate",
value: function generate(frameData) {
var frameName = frameData.name || '';
var css = "@keyframes ".concat(frameName, " {");
for (var key in frameData) {
if (key !== 'name' && key !== 'media' && key !== 'complete') {
css += "".concat(key, " {");
for (var property in frameData[key]) {
css += "".concat(property, ":").concat(frameData[key][property], ";");
}
css += '}';
}
}
if (frameData.media) {
css = "@media ".concat(frameData.media, "{").concat(css, "}");
}
var frameStyle = document.getElementById(frameName);
if (frameStyle) {
frameStyle.innerHTML = css;
} else {
Keyframes.createKeyframeTag(frameName, css);
}
}
}, {
key: "define",
value: function define(frameData) {
if (frameData.length) {
for (var i = 0; i < frameData.length; i += 1) {
this.generate(frameData[i]);
}
} else {
this.generate(frameData);
}
}
}, {
key: "plugin",
value: function plugin(pluginFunc) {
pluginFunc(Keyframes);
}
}]);
return Keyframes;
}();
exports.default = Keyframes;
},{}],3:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _default = function _default(Keyframes) {
Keyframes.prototype.proximity = function (onMove) {
var _this = this;
this.onMouseMove = function (e) {
var mouse = {
x: e.pageX,
y: e.pageY
}; // Target edge
var target = _this.elem.getBoundingClientRect();
var targetCenter = {
x: Math.floor(target.left + target.width / 2),
y: Math.floor(target.top + target.height / 2)
};
var angle = Math.atan2(mouse.y - targetCenter.y, mouse.x - targetCenter.x);
var cosAngle = Math.abs(Math.cos(angle));
var sinAngle = Math.abs(Math.sin(angle));
var magnitude = target.width / 2 * sinAngle <= target.height / 2 * cosAngle ? target.width / 2 / cosAngle : target.height / 2 / sinAngle;
var targetEdge = {
x: targetCenter.x + Math.cos(angle) * magnitude,
y: targetCenter.y + Math.sin(angle) * magnitude
}; // Viewport edge
var viewport = {
width: window.innerWidth,
height: window.innerHeight
};
var viewPortMagnitude = viewport.width / 2 * sinAngle <= viewport.height / 2 * cosAngle ? viewport.width / 2 / cosAngle : viewport.height / 2 / sinAngle;
var viewPortEdge = {
x: targetCenter.x + Math.cos(angle) * viewPortMagnitude,
y: targetCenter.y + Math.sin(angle) * viewPortMagnitude
}; // Distances
var calculateDistance = function calculateDistance(vector) {
var offsets = {
top: target.top - vector.y,
right: vector.x - (target.left + target.width),
bottom: vector.y - (target.top + target.height),
left: target.left - vector.x
};
var dx = Math.pow(Math.max(offsets.left, 0, offsets.right), 2);
var dy = Math.pow(Math.max(offsets.top, 0, offsets.bottom), 2);
return Math.sqrt(dx + dy);
};
var mouseDistance = calculateDistance(mouse);
var viewPortDistance = calculateDistance(viewPortEdge);
var distancePercentage = 100 - Math.round(mouseDistance / viewPortDistance * 100);
onMove({
mouse: mouse,
mouseDistance: mouseDistance,
viewPortDistance: viewPortDistance,
distancePercentage: distancePercentage,
targetEdge: targetEdge,
viewPortEdge: viewPortEdge,
collision: !mouseDistance
});
};
window.addEventListener('mousemove', this.onMouseMove);
};
};
exports.default = _default;
},{}]},{},[1]);