Skip to content

Commit e6c93aa

Browse files
committed
Update scroll customization explainer
1 parent 35195d8 commit e6c93aa

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

scroll-customization-api/explainer.md

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Scroll customization explained
1+
# Scroll customization
22

33
Many native mobile applications have [UI effects that interact with scrolling](https://github.com/w3c/css-houdini-drafts/blob/master/scroll-customization-api/UseCases.md), for example snapping the scroll position to image boundaries in an image carousell. To do this web developers must [resort](http://cubiq.org/iscroll-5) to re-implementing all of scrolling on top of raw input events. There are four major drawbacks with such an approach:
44
- Threaded scrolling is disabled, so any main thread work >16ms will lead to dropped frames.
@@ -10,14 +10,36 @@ The fundamental problem here is that scrolling on the web is a big "magical" bla
1010

1111
## Threaded scrolling
1212

13-
Modern browsers implement scrolling off of the main JavaScript thread to ensure that scrolling can be serviced reliably at 60fps. This proposal builds on [Compositor Worker](https://github.com/ianvollick/css-houdini-drafts/blob/master/composited-scrolling-and-animation/Explainer.md) to permit customizations that typically run in-sync with scrolling. In the future we may also want high performance applications to be able to opt-out of threaded scrolling and also use a variant of these APIs from the main JavaScript execution context.
13+
Modern browsers implement scrolling off of the main JavaScript thread to ensure that scrolling can be serviced reliably at 60fps. This proposal builds on [Compositor Worker](https://github.com/w3c/css-houdini-drafts/blob/master/composited-scrolling-and-animation/Explainer.md) to permit customizations that typically run in-sync with scrolling. In the future we may also want high performance applications to be able to opt-out of threaded scrolling and also use a variant of these APIs from the main JavaScript execution context.
1414

15-
## ScrollIntent - a primitive abstraction
15+
## ScrollIntent
1616

17-
In order to explain scrolling, we need to introduce an abstraction that sits above raw input (eg. `wheel` and `touchmove` events) but below the effect that scrolling has (changing `scrollTop` and generation of `scroll` events). `ScrollIntent` represents the user's desire to scroll by a specific number of pixels (as well as additional details about the scroll).
17+
In order to explain scrolling, we need to introduce an abstraction that sits above raw input (eg. `wheel` and `touchmove` events) but below the effect that scrolling has (changing `scrollTop` and generation of `scroll` events). `ScrollIntent` represents the user's desire to scroll by a specific number of pixels (as well as additional details about the scroll such has the phase and velocity). A `Element` has a new method called `applyScroll` which takes a `ScrollIntent` and applies it, which by default just means updating `scrollTop` and `scrollLeft`.
1818

19-
TODO: Picture showing touch/wheel/keyboard input -> scroll intent -> tagetting logic -> Element::applyScrollIntent -> set scrollTop
19+
![ScrollIntent](ScrollIntent.png?raw=true)
2020

21-
## Examples
21+
## Customizing `applyScroll`
22+
23+
The default `applyScroll` behavior can be overridden to do something competely different whenever the user attempts to scroll the element. For example, to support rotating a 3d carousell using all the native scroll physics and composition, you might use code like this (hypothetical syntax):
24+
25+
Main Thread
26+
```JavaScript
27+
var carousel_proxy = new CompositorProxy(carousel, [transform]);
28+
var worker = new CompositorWorker('carousel.js');
29+
worker.postMessage(carousel_proxy);
30+
```
31+
32+
On the CompositorWorker
33+
```JavaScript
34+
onmessage = function(e) {
35+
var carousel = e.data;
36+
carousel.applyScroll = function(scrollIntent) {
37+
this.transform.rotateX += scrollIntent.deltaX;
38+
scrollIntent.consumeDelta(scrollIntent.deltaX, 0);
39+
};
40+
};
41+
```
42+
43+
## More examples
2244

2345
TODO

0 commit comments

Comments
 (0)