w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.5k stars 661 forks source link

[css-easing-2] Provide JavaScript API for CSS easing functions #6261

Open mattgperry opened 3 years ago

mattgperry commented 3 years ago

With new proposals for complex easing functions, the amount of API surface JavaScript animation libraries are duplicating is to increase.

This is a waste of bundle size when these functions are already present in the browser. This cubic-bezier implementation alone can cost 1kb minified, so I'm eager that when new easing curves appear there's already requirement for them to be available to JS authors.

It would also ensure browser-based animation authoring tools like Framer could use the current browser's exact implementation when displaying and editing easing curves.

Proposal

A new Easing namespace, which contains access to the specced easing curves:

// cubic-bezier
const curve = Easing.cubicBezier(.17,.67,.83,.67)
const easedProgress = curve(0.5)

// steps
const stepped = Easing.steps(4, "end")
const easedProgress = stepped(0.5)

This would also allow the composition of easing functions:

stepped(curve(0.5))

And their usage outside of the Web Animations API, for instance drawing visualisations:

Screenshot of Framer's bezier editing tool

Loirooriol commented 3 years ago

Maybe inside CSS, though? CSS.easing.cubicBezier(.17,.67,.83,.67)

mattgperry commented 3 years ago

Yep! Not precious over the namespace, gotta make that clear đŸ˜€

Jamesernator commented 3 years ago

These are probably a good candidate for typed OM, CSS transforms already have something similar in that you can get out a matrix with new CSSTranslate(CSS.px(3), CSS.px(5)).toMatrix().

I'm not entirely sure why there's only a few functions available in typed OM, but it'd make sense to have something like that for easing:

const easing = new CSSEasingCubicBezier(CSS.number(.17),CSS.number(.67),CSS.number(.83),CSS.number(.67));
console.log(easing.valueAt(0.5));
tabatkins commented 3 years ago

I'm not entirely sure why there's only a few functions available in typed OM,

Because CSS is a big beast, and the design of Typed OM allows for us to define things gradually. ^_^

But yeah, Typed OM definitely needs the easing functions as values at some point, and when we do, having a convenience method that lets you explicitly evaluate it at a given progress value makes a ton of sense.