Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel previous ripple on click #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,39 @@ CSS.paintWorklet.addModule('./ripple-worklet.js');

if (!window.performance) window.performance = { now: Date.now.bind(Date) };

if (!window.requestAnimationFrame) window.requestAnimationFrame = function(cb) { return setTimeout(doAnim, cb); };
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(cb) { return setTimeout(doAnim, 16, cb); };
window.cancelAnimationFrame = function(req) { clearTimeout(req); }
}
function doAnim(cb) { cb(performance.now()); }

function ripple(evt) {
var button = this,
function ripple(button, evt) {
var request,
rect = button.getBoundingClientRect(),
x = evt.clientX - rect.left,
y = evt.clientY - rect.top,
start = performance.now();
button.classList.add('animating');
requestAnimationFrame(function raf(now) {
request = requestAnimationFrame(function raf(now) {
var count = Math.floor(now - start);
button.style.cssText = '--ripple-x: ' + x + '; --ripple-y: ' + y + '; --animation-tick: ' + count + ';';
if (count > 1000) {
button.classList.remove('animating');
button.style.cssText = '--animation-tick: 0;';
return;
}
requestAnimationFrame(raf);
})
request = requestAnimationFrame(raf);
});
return function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just return the rAF/timer ID here and avoid the cancellation proxy altogether.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually that could let this be re-collapsed to a single method:

addEventListener('click', function(e) {
  if (this.ripple) cancelAnimationFrame(this.ripple)

  ...
  this.ripple = requestAnimationFrame(rAF)
})

Copy link
Author

@MattiasBuelens MattiasBuelens May 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just return the rAF/timer ID here and avoid the cancellation proxy altogether.

ripple() can't return a request ID, because that ID changes after every rAF call.

Actually that could let this be re-collapsed to a single method: [...]

We could indeed store the ID in a property on button, rather than keeping it inside a closure. However, I'm not really a fan of expando properties. 😛


I tried to keep my changes minimal to focus on solving just this one problem. But if you're okay with it, we can definitely think about refactoring it a bit. 😄

Personally, I would lean towards using a class and have one instance per element to (re)start the ripple effect. It'd be very similar to the current solution, except that all state moves from closures into class instances. For example:

[].forEach.call(document.querySelectorAll('.ripple'), function (el) {
    var rippler = new Rippler(el);
    el.addEventListener('click', function (evt) {
    	rippler.start(evt); // (re)-start animation
    });
});

cancelAnimationFrame(request);
}
}
[].forEach.call(document.querySelectorAll('.ripple'), function (el) {
el.addEventListener('click', ripple);
});
var cancelRipple;
el.addEventListener('click', function (evt) {
if (cancelRipple) {
cancelRipple();
}
cancelRipple = ripple(this, evt);
});
});