summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-animations/scroll-timelines/scroll-timeline-range.html
blob: cc844cb748e6f2b024f7a04392101b77f162c740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<title>Scroll timelines and animation attachment ranges</title>
<link rel="help" src="https://drafts.csswg.org/scroll-animations-1/#named-timeline-range">
<link rel="help" src="https://drafts.csswg.org/scroll-animations-1/#animation-range">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/web-animations/testcommon.js"></script>
<script src="support/testcommon.js"></script>
<style>
  #scroller {
    overflow-y: scroll;
    width: 200px;
    height: 200px;
    scroll-timeline: --t1;
  }
  .spacer {
    height: 1100px;
  }
  #target {
    height:  100px;
    width: 0;
    font-size: 10px;
    background-color: green;
  }
  @keyframes grow {
    to { width: 200px }
  }
  .anim-1 {
    animation: auto grow linear;
    animation-timeline: --t1;
    animation-range-start:  25%;
    animation-range-end:  75%;
  }
  .anim-2 {
    animation: auto grow linear;
    animation-timeline: --t1;
    animation-range-start: 40px;
    animation-range-end:  700px;
  }
  .anim-3 {
    animation: auto grow linear;
    animation-timeline: --t1;
    animation-range-start:  calc(30% + 40px);
    animation-range-end:  calc(70% - 20px);
  }
  .anim-4 {
    animation: auto grow linear;
    animation-timeline: --t1;
    animation-range-start:  5em;
    animation-range-end:  calc(100% - 5em);
  }

</style>
<div id=scroller>
  <div id=target></div>
  <div class=spacer></div>
</div>
<script>
  // Test via web-animation API.

  async function test_range_waapi(t, options) {
    const timeline = new ScrollTimeline({source: scroller, axis: 'block'});
    const anim =
        target.animate([{ width: "200px" }],
                       {
                         timeline: timeline,
                         rangeStart: options.rangeStart,
                         rangeEnd: options.rangeEnd
                       });
    t.add_cleanup(() => {
      anim.cancel();
    });
    await anim.ready;
    scroller.scrollTop = 600;
    await waitForNextFrame();

    const expectedProgress =
        (600 - options.startOffset) / (options.endOffset - options.startOffset);
    assert_approx_equals(anim.effect.getComputedTiming().progress,
                         expectedProgress, 0.001);
  }

  promise_test(async t => {
    await test_range_waapi(t, {
      rangeStart: "25%",
      rangeEnd: "75%",
      startOffset: 250,
      endOffset: 750
    });
  }, 'Scroll timeline with percentage range [JavaScript API]');

  promise_test(async t => {
    await test_range_waapi(t, {
      rangeStart: "40px",
      rangeEnd: "700px",
      startOffset: 40,
      endOffset: 700
    });
  }, 'Scroll timeline with px range [JavaScript API]');

  promise_test(async t => {
    await test_range_waapi(t, {
      rangeStart: "calc(30% + 40px)",
      rangeEnd: "calc(70% - 20px)",
      startOffset: 340,
      endOffset: 680
    });
  }, 'Scroll timeline with calculated range [JavaScript API]');

promise_test(async t => {
    t.add_cleanup(() => {
      target.style.fontSize = '';
    });
    await test_range_waapi(t, {
      rangeStart: "5em",
      rangeEnd: "calc(100% - 5em)",
      startOffset: 50,
      endOffset: 950
    });
    target.style.fontSize =  '20px';
    await waitForNextFrame();
    const anim = target.getAnimations()[0];
    const expectedProgress = (600 - 100) / (900 - 100);
    assert_approx_equals(anim.effect.getComputedTiming().progress,
                         expectedProgress, 0.001);
  }, 'Scroll timeline with EM range [JavaScript API]');

  // Test via CSS.
  async function test_range_css(t, options) {
    t.add_cleanup(() => {
      target.classList.remove(options.animation);
    });
    target.classList.add(options.animation);
    const anim = target.getAnimations()[0];
    await anim.ready;
    scroller.scrollTop = 600;
    await waitForNextFrame();

    const expectedProgress =
        (600 - options.startOffset) / (options.endOffset - options.startOffset);
    assert_approx_equals(anim.effect.getComputedTiming().progress,
                         expectedProgress, 0.001);
  }

  promise_test(async t => {
    await test_range_css(t, {
      animation: "anim-1",
      startOffset: 250,
      endOffset: 750
    });
  }, 'Scroll timeline with percentage range [CSS]');

  promise_test(async t => {
    await test_range_css(t, {
      animation: "anim-2",
      startOffset: 40,
      endOffset: 700
    });
  }, 'Scroll timeline with px range [CSS]');

  promise_test(async t => {
    await test_range_css(t, {
      animation: "anim-3",
      startOffset: 340,
      endOffset: 680
    });
  }, 'Scroll timeline with calculated range [CSS]');

promise_test(async t => {
    t.add_cleanup(() => {
      target.style.fontSize = '';
    });
    await test_range_css(t, {
      animation: "anim-4",
      startOffset: 50,
      endOffset: 950
    });
    target.style.fontSize =  '20px';
    await waitForNextFrame();
    const anim = target.getAnimations()[0];
    const expectedProgress = (600 - 100) / (900 - 100);
    assert_approx_equals(anim.effect.getComputedTiming().progress,
                         expectedProgress, 0.001);
  }, 'Scroll timeline with EM range [CSS]');
</script>