Skip to content

Commit 5321937

Browse files
committed
Update cursor position on frame request
1 parent 8813569 commit 5321937

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

dist/cce.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cce.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Simple element cursor plugin v1.0.0
2+
* Simple element cursor plugin v1.1.0
33
*
44
* Copyright (c) 2018 Levi Cole <me@thelevicole.com>
55
* Licensed under MIT (http://opensource.org/licenses/MIT)
@@ -16,18 +16,21 @@
1616
preserveCursor: false
1717
}, options);
1818

19+
// Set default element styles
1920
$cursor.css({
2021
'position': 'fixed',
2122
'pointer-events': 'none',
2223
'z-index': 99999
2324
});
2425

25-
if (!options.preserve_cursor) {
26+
// Hide default browser cursor
27+
if (!options.preserveCursor) {
2628
$('head').append( $('<style>', {
2729
text: '*, *:before, *:after { cursor: none; }'
2830
}) );
2931
}
3032

33+
// Define global variables
3134
let mouse_x = 0,
3235
mouse_y = 0;
3336

@@ -37,47 +40,31 @@
3740
let id = '',
3841
node = '';
3942

40-
const move = () => {
41-
// Convert cursor position into percentage
42-
const x_percent = (mouse_x + options.offsetX) / window_x * 100;
43-
const y_percent = (mouse_y + options.offsetY) / window_y * 100;
44-
45-
$cursor.show().css({
46-
top: y_percent + '%',
47-
left: x_percent + '%'
48-
});
49-
50-
$cursor.attr('data-id', id);
51-
$cursor.attr('data-el', node);
52-
};
53-
54-
$(window).on('resize', function(event) {
55-
window_x = $(window).width();
56-
window_y = $(window).height();
57-
58-
move();
59-
});
60-
43+
// Update global variables on document events
6144
$(document).on('ready mousemove resize', function(event) {
6245
const $target = $(event.target);
6346

64-
mouse_x = event.clientX;
65-
mouse_y = event.clientY;
47+
window_x = $(window).width();
48+
window_y = $(window).height();
49+
50+
mouse_x = event.clientX;
51+
mouse_y = event.clientY;
6652

67-
id = $target.attr('id') || $target.closest('[id]').attr('id') || '';
68-
node = $target.prop('nodeName').toLowerCase();
69-
70-
move();
53+
id = $target.attr('id') || $target.closest('[id]').attr('id') || '';
54+
node = $target.prop('nodeName').toLowerCase();
7155
});
7256

57+
// Hide element if cursor leaves the document
7358
$(document).on('mouseleave', function() {
7459
$cursor.hide();
7560
});
7661

62+
// Hide element if cursor enters an iframe
7763
$('iframe').on('mouseenter', function() {
7864
$cursor.hide();
7965
});
8066

67+
// Add click event to element
8168
$(document).on('mousedown mouseup', function(event) {
8269
let event_name = '';
8370

@@ -87,6 +74,28 @@
8774

8875
$cursor.attr('data-event', event_name);
8976
});
77+
78+
/**
79+
* Tell the browser that we wish to perform an animation
80+
* @see https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
81+
*/
82+
window.requestAnimationFrame(function animation() {
83+
84+
// Convert cursor position into percentage
85+
const x_percent = (mouse_x + options.offsetX) / window_x * 100;
86+
const y_percent = (mouse_y + options.offsetY) / window_y * 100;
87+
88+
$cursor.show().css({
89+
top: y_percent + '%',
90+
left: x_percent + '%'
91+
});
92+
93+
$cursor.attr('data-id', id);
94+
$cursor.attr('data-el', node);
95+
96+
window.requestAnimationFrame(animation);
97+
98+
});
9099
};
91100

92101
})(jQuery || window.jQuery);

0 commit comments

Comments
 (0)