Repository navigation
Description
I would like to revive work on custom effects, an idea currently unofficially drafted in Web Animations Level 2. I have filed a patch for WebKit to support the CustomEffect interface as well as a new document.timeline.animate() method. The motivation is to bridge the gap between the poorly-named and rudimentary requestAnimationFrame() and Web Animations allowing authors to harness the full power of the Web Animations model such that scripted animations may be paused, resumed, seeked, etc.
Some example usage:
const animation = new Animation;
animation.effect = new CustomEffect(progress => { … }, 1000);
animation.play();This code is equivalent to the more idiomatic:
document.timeline.animate(progress => { … }, 1000);The idea here is that document.timeline.animate() should be to CustomEffect what element.animate() is to KeyframeEffect.
The section on custom effects in the current level 2 spec starts with this issue:
This whole feature needs to be revisited. The current thinking is that rather than having custom effects, we should simply have an onupdate callback on each animation effect. That would allow, for example, augmenting an existing effect with a function that performs logging or triggers additional actions at certain times. With the current arrangement, doing this would require adding a parent group just to achieve this.
I personally think that a dedicated CustomEffect interface is a simple and purposeful way to specify an animation where its application is performed by script. I expect that it is simpler to specify how this specific class of effects would work rather than trying to add an onupdate callback that would apply to keyframe effects as well.
You'll also notice the lack of a target for CustomEffect. I believe that it should not be necessary to specify a target for a custom effect since it may not target a single node, or even a node at all, but rather a JS object controlling the rendering of a scene using <canvas> APIs.
That being said, I am very open to all feedback to work towards exposing callback-based animations in Web Animations. All work conducted in WebKit is behind an off-by-default experimental feature flag and we have no intention to expose this to the Web until we have consensus on the way forward.
Cc @birtles @flackr @stephenmcgruer @kevers-google @majido @smfr @grorg @hober @ogerchikov @BorisChiou @hiikezoe
Activity
birtles commentedon Dec 6, 2021
That all makes sense to me. Thanks for picking this up.
okikio commentedon Jan 31, 2022
Is there a related CSS value interpolation API?
graouts commentedon Feb 1, 2022
I hadn't considered that, but it's something that's worth exploring alongside this proposal. Would you have something to propose?
okikio commentedon Feb 1, 2022
@graouts
I'm not super experienced in interpolation and/or making proposals for the csswg, I wouldn't mind though, however, I'm mostlikely going to need some help.
cc @mattgperry
mattgperry commentedon Feb 1, 2022
Hey @graouts
This came out of a discussion between @okikio and myself about the limitations custom effects leaves us with. I'm super excited about custom effects but I think there's a remaining black hole when talking about interpolating complex values like
KeyframeEffectthat would limit the ability for many JS libraries to leverage existing interpolators already present in the browser.My first thought was having
CustomEffectalso optionally support keyframes but I think this limits what is clearly a low-level API. For example, it wouldn't allow for custom easing functions likecircInetc. Whereas a stand-alone interpolator API would:Ideally the interpolator would support unclamped progress values so we could support overshoot easing.
Supported interpolators that would be helpful:
20px5px,100%0)bluergba(0,0,0, .5))nonescale(3) translateX(100px))5px 5px rgba(0,0,0,0.1)10px 10px rgba(0,0,0,0.05), 2px 2px 2px rgba(0,0,0,0.2))All of these (and more) are already leveraged within browsers, I think direct access would pair very well with
CustomEffect.okikio commentedon Feb 1, 2022
In addition to what @mattgperry posted something like this would also be awesome,
The API would very similar to the currently discussed CSS counter part
mix()#581 (comment)birtles commentedon Feb 2, 2022
See also #6697 and #6700.
flackr commentedon Sep 12, 2024
I think that the timing with respect to other effects needs to be clear either way. I think having a set of post-animation update callbacks is probably simpler to be honest.
The other thing that may be cleaner about an update callback is that we may be able to skip it if the effect easing resulted in no change. E.g.
I agree that conceptually it makes sense to not have a target, though it could be a nice feature if we could skip custom effects if the target (e.g. the canvas being drawn to) was not in view or at least if the target is detached.
flackr commentedon Sep 26, 2024
I think @bramus also had a good use case where an update callback would be much simpler ergonomically than needing to create a separate animation.
bramus commentedon Sep 26, 2024
Thanks for pointing me to this thread, @flackr.
I’m currently building a demo that uses a scroll-driven animation on an
input[type=range]. The progress of that SDA is then used to rotate a 3D element on the page in response. You can try it out in Chrome Canary with the Experimental Web Platform Features flag enabled: as you drag the slider, the 3D model rotates: https://codepen.io/bramus/pen/VwoYoLROne thing I found missing while building this is having
progressevent on theAnimationitself to hook this all onto. Yes, I can read the animation’s progress, but I need a trigger for when to read it.Right now, I rely on the input’s
input+changeevents to read the animation progress. While using these two events in this case here works, my reflex as an author is to listen to the animation’s progress instead as my brain is in animation mode, not in input mode. Would such a progress event be available, I can easily take the animation + listener and attach it to some other input mechanisms in CSS (such as a regularScrollTimeline) without needing to set up other JS listeners for other types of events (such asscroll).Using a
CustomEffectwould be possible here, but that would require me to create aCustomEffectin JS on top of the already CSS existing animation. This feels like a lot of extra work just to get notified of an already existing animation’s tick.I believe a
progressorupdateevent on the animation would be a more convenient way for authors to achieve what I want to do here.bramus commentedon Oct 22, 2024
Another situation: Part of the
sda-utilitiespackage I created is atrackProgressfunction. Authors can use this to Synchronize videos, 3D-models, etc. to Scroll-Driven Animations but also to sync things toDocumentTimeline-based animations as well as all future types of timelines we can come up with (e.g.PointerTimelineandMediaPlaybackTimeline)The implementation of that function itself pretty nasty as it relies on
requestAnimationFrameto constantly read the progress of the animation.With
animation.progressavailable I could replace some of the logic to useanimation.progress, yet it would not allow me remove therequestAnimationFrameas I want the library to support any type of timeline, current and future.Again, an
updateorprogressevent on the existing animation would offer a way out here, as that allows me to ditchrequestAnimationFrame.ydaniv commentedon Oct 24, 2024
TBH, I don't see much difference between the 2 methods, except for the effect's
target.Animationthat wraps them with a correspondingtimeline.KeyframeEffectOptionsthat sets their timing options.And regarding using the target to play/pause:
I think the playback management would better be solved using
AnimationTrigger's, though having the animation GC'd when a target is removed could be nice.In @bramus's example the main issue not being able to get the the pseudo-element, unless you create grab the animation set by CSS and take the
animation.timeline, so it could look something like this:We could also decide that
CustomEffectalso takestarget, and then provide it as argument for the update callback. Could be useful for reusable drawing functions.Regarding sync between other effects, I suppose this is something that should probably be left for nesting effects via
GroupEffect. We would need to define the order of operations of different effects.But this could also be a good incentive for making progress on
GroupEffect's (:So to summarize, for me this boils down to ergonomics. So naming some use-cases to make this more concrete:
There are basically 2 goals here:
In most of the use-cases I personally encounter in my work it's mostly more straightforward to use the
CustomEffectsyntax, because syncing with other animations is less of an issue.But I also don't see a reason why we can't have both?
flackr commentedon Oct 24, 2024
When I say skip, this is a (up to now implementation) detail that affects main frame generations. There are many sites with a lot of complex content that is not yet scrolled on screen (or has been scrolled off screen). Conceptually all animations tick all of the time, but since the offscreen animations do not present any visual change chrome can skip generating frames if all of the updating content is offscreen. When a main frame is generated all animations are updated to the current time such that all styles are as if the animation has been active.
With a custom effect callback, we could not do this sort of an optimization unless it was part of the API.
This skipping isn't about playback management, when we skip animations they conceptually continue playing, this is about optimizing frame generation. I also don't think this is something that developers usually do unless, as you alluded to, they intentionally want to pause playback. Most developers I've talked to don't manage all of the content on the site.
With CSS animations it didn't need to be in spec since a developer wouldn't be able to tell as if they request an animation frame the animations are updated then. However, we wouldn't know for custom effects what they change so it would need to be specified if they are skipped in certain circumstances.
And, having a rooted node to get the animations from for getAnimations
ydaniv commentedon Oct 24, 2024
Thanks for in-depth explanation! I get it now. But it seems that the only difference between the two is having a target, right?
My main concern here is that creating a dummy
KeyframeEffectwith an empty object seems a bit awkward for an API. I suppose it would be better to also keep thetargetargument forCustomEffectas well. Then the UA could still optimize frames as you mentioned.Yes, I guess working on a large scale, generic tool forced us to take less chances and manage these more strictly.
Right! So adding the
targetargument should enable that too.15 remaining items
New explainer for Animation Custom Effects aiming to address w3c/cssw…
[web-animations] dispatch a 'progress' event when animations are updated
bramus commentedon Oct 9, 2026
I haven’t looked into very much detail in all of it, but a quick check tells me this is looking great!
While I can’t come up with use-cases on the spot right now, I’m leaning to exposing the full object. For the simple use-cases, authors can always just write
({ progress }) => { … }.Counter question: should progress be clamped to
[0,1]? I am thinking ofViewTimeline-driven effects here.Yes please.
I would assume that the custom effects get applied after other effects like
KeyframeEffects are. That way, theCustomEffectcan use up-to-date style information.[web-animations] dispatch a 'progress' event when animations are updated
graouts commentedon Oct 9, 2026
The main one I can think of is wanting to achieve accumulation in which case you need access to the current iteration. Other than that, it's not obvious what else authors would want to use from the computed effect timing aside from the current iteration progress.
I don't think this proposal should change anything here, the
progressis the current iteration progress.👍
I was thinking in terms of the order of animations returned by
getAnimations()but you're asking about a separate topic. In this proposal, custom effects are applied as the timelines current time advances during the update animations and send events procedure. In WebKit's implementation, this means that elements that are the target of a keyframe effect will be marked as "dirty", but styles aren't resolved there and then. Style resolution will occur at the first opportunity, which could be forced, for instance, by a custom effect's callback querying a target's computed style, or thereadypromise doing the same, etc. And as timelines advance their current time, at least in WebKit, no effort is made to iterate over a timeline's animations in a specific order. The composition order is only followed when resolving styles.So I don't think your assumption would check out and I think it would be a challenge to make it work.
[web-animations] dispatch a 'progress' event when animations are updated
[web-animations] dispatch a 'progress' event when animations are updated
[web-animations] dispatch a 'progress' event when animations are updated
ydaniv commentedon Oct 11, 2026
Thanks, @graouts! Overall this is looking great, appreciate including all API forms, I think all are very useful.
Since
durationis initiallyauto, I propose that forCustomEffectsit will be computed toInfinityfor time-based timelines, instead of0. The idea is to allow easily enable the commonrAF(rAF())loop use-case.And then, since
duration==Infinityit would be great to have theprogressexposelocalTime, we don't necessarily need to get current iteration and multiply, etc. Also, if we do this, we probably need to compute some (all?) of the other timing options to their initial values, such asiterations,easing, iterationStart, anddirection`.This way you get a nice default for the common case:
I also think it would be useful to expose the effect itself as a parameter of the effect/progress callback, either way we decide, either as a second param or part of a property bag.
Not sure about
null, but authors may, in some cases, want to invoke the callback when leavingactivephase, i.e. with progress-based timeline when exiting the active attachment range without a fill behavior in that direction, or when the timeline becomes inactive. For example imagine a canvas effect with a PointerTimeline, where you want to disable the effect.I think there will be many use-cases for that, where it's handy to have the target DOM element tied to the effect.
Maybe we can follow existing behavior? Today
Animation's compose in order of calls to.play(), right? Is there a special composition withCSSAnimationeffects?We already have a resolution for that (:
css-meeting-bot commentedon Oct 21, 2026
The CSS Working Group just discussed
[web-animations-2] Custom effects.The full IRC log of that discussion
<ydaniv> graouts: purpose of Custom Effects it to harness WAAPI to provide callback based animations<ydaniv> ... one of the use-cases is to replace what ppl usually do, using rAF for looping animations
<ydaniv> ... and then use progress for time-based effects
<ydaniv> ... instead you could use the WAAPI with more timing information, harnessing WAAPI powers for time-based animations
<ydaniv> ... another aspect is not just the CE interface, but also for progress playback event on regular style based animations
<ydaniv> ... we have an explainer with usage scenarios and introducing the interfaces
<ydaniv> ... the hope here is to add it to web-animations-2
<ydaniv> ... we have experimental impls and we want to add it to the spec
<ydaniv> astearns: you mentioned this is unofficially drafted already in spec
<bramus> q+
<smfr> q+
<ydaniv> graouts: yes, this is history, been dormant, want to reintroduce as a brand new proposal
<ydaniv> q+
<astearns> ack bramus
<ydaniv> bramus: thanks for working on this! there's both needs for the CEs and the progress event, big fan of it, want to see it moved on
<astearns> ack smfr
<ydaniv> smfr: would like to give more background. long term we would like to move away from rAF. It's a terrible API
<TabAtkins> +1
<ydaniv> ... it's just a one-shot simple callback, the browser doesn't have any idea what you might be doing there
<ydaniv> ... current strategy is to fire the rAF callback at first oportunity which is not optimal
<ydaniv> ... so we want authors to be able to control frame rate, etc
<fantasai> scribe+
<emilio> q+
<astearns> ack ydaniv
<fantasai> ydaniv: Want to join Bramus
<fantasai> ... really like that we introduced all the scenarios into the explainer
<fantasai> ... both are very important, both running simple callback loop and the focus event
<fantasai> ... I think the explainer answers most of the use cases
<fantasai> ... I just added a comment about, perhaps to make the simple use case of just wanting to run an endless time-based loop
<lea> q?
<fantasai> ... maybe we could allow infinite durations for custom effects
<lea> q+
<noamr> q+
<fantasai> ... and then simply duration auto would compute to 'infinity' for custom effects
<fantasai> ... and thena ll you need to do is adjust the callback
<fantasai> ... don't need to mess around with infinite iterations, or invent a duration
<fantasai> ... [missed]
<fantasai> ... just get a progress that's the local time
<fantasai> ... so your progress is what rAF used to get
<fantasai> ... and then you can do simple animations on canvas and stuff like that
<flackr> q+
<astearns> ack emilio
<ydaniv> emilio: in general seems interesting
<ydaniv> ... one of the things that will hit ppl is that it's a bit different from rAF, and worry a bit about [missed]
<ydaniv> graouts: touched on it in explainer, 2 different things here, CEs and progress events
<ydaniv> ... they trigger at different times
<ydaniv> ... CEs trigger at timeline update
<ydaniv> ... but progress events trigger on the last update after all animation timelines updated
<ydaniv> ... 2 windows here
<ydaniv> ... so open question is whehter we need 2 different places to update? or a single one
<astearns> zakim, close queue
<Zakim> ok, astearns, the speaker queue is closed
<ydaniv> emilio: in general all the things that happen on that phase can be a micro task chceck point
<ydaniv> ... and browsers can handle running script in that window. spec currently doesn't say anything about it
<ydaniv> ... it's effectively defined in the processing model
<ydaniv> graouts: usually we you have a keyframe effect on CSS, updating the time is not the actual effect of the animation, it would run in a later time
<ydaniv> ... it would be possible to define such later time when animations are updated to trigger these callbacks
<ydaniv> ... maybe we could queue up all events and deliver in this kind of single phase
<ydaniv> emilio: either a micro task or something explicit
<ydaniv> ... those 2 concerns might not be a big issue at this point
<ydaniv> ... the fact that they might do some random stuff , it's accounted for
<astearns> q?
<ydaniv> flackr: I expected after alll animations updated but just before [missed]
<ydaniv> ... just before rAF
<ydaniv> graouts: yea that might work
<ydaniv> +1 to flackr
<ydaniv> astearns: we'll pause here and let graouts update the explainer, and allow ppl adopt it later
<lea> scribe+
<astearns> ack lea
<lea> lea: First, +1 to use case. I haven’t seen this before, so this suggestion may be completely off, but one limitation is that AFAIK you can’t trigger a JS-defined animation from CSS
<lea> lea: so what if this was about being able to define a custom keyframes animation via JS? Keyframes are about mapping progress to CSS, so that seems to be the right primitive for mapping progress to custom effects, and then we can use the existing CSS animations machinery to trigger it, rather than having the trigger live in JS too.
<lea> lea: And then CSS could also define an animation with the same name, to be used as a fallback
<ydaniv> q+
<lea> s/mapping progress to CSS/mapping progress to a visual output/
<ydaniv> graouts: I think the progress event let's you do that
<ydaniv> ... in some cases the animations would be existing, but yeah, if you want to trigger from CSS and trigger progress you need a dummy animaiton
<ydaniv> ... please add an issue
<lea> smfr: Can't you already do that via animation events?
<ydaniv> lea: will do
<astearns> ack noamr
<lea> scribe-
<ydaniv> noamr: was missing something, from smfr it sounds like an optimization, like animation worklet, which allows you to run stuff in a separate thread
<astearns> ack flackr
<ydaniv> graouts: good point
<ydaniv> flackr: I had about idea of auto duration be infinite, which feels weird to me
<flackr> s/had/heard
<ydaniv> ... would like to here a distinction as to why we should do that
<astearns> ack dbaron
<ydaniv> dbaron: I'm a bit detached, but one thought was, might be useful, is that some of the discussion made me think of worklets, where they had a limited access to stuff
<kbabbitt> +1 to exploring worklets here
<ydaniv> graouts: sounds like what noamr said, right?
<ydaniv> ... I hear you, we can touch that on the explainer, as to why to do it this way or the other
<ydaniv> ... needs to be discussed there
<flackr> if we used worklets, it wouldn't be a suitable replacement for requestAnimationFrame
<ydaniv> astearns: that's it, please go to issue and continue there
Custom effect callbacks should be queued as timelines are udpated and…
Custom effect callbacks should be queued as timelines are udpated and…
Custom effect callbacks should be queued as timelines are updated and…
graouts commentedon Dec 1, 2026
While the notion of
progressevents is related to custom effects in terms of use case, it's an orthogonal feature so I filed [web-animations-2] add aprogressevent and filed a pull request.