KeyframeEffect-setKeyframes.tentative.html (6373B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>KeyframeEffect.setKeyframes() for CSS transitions</title> 4 <!-- TODO: Add a more specific link for this once it is specified. --> 5 <link rel="help" href="https://drafts.csswg.org/css-transitions-2/#csstransition"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="support/helper.js"></script> 9 <div id="log"></div> 10 <script> 11 'use strict'; 12 13 test(t => { 14 const div = addDiv(t); 15 16 div.style.left = '0px'; 17 getComputedStyle(div).transitionProperty; 18 div.style.transition = 'left 100s'; 19 div.style.left = '100px'; 20 21 const transition = div.getAnimations()[0]; 22 transition.effect.setKeyframes({ left: ['200px', '300px', '100px'] }); 23 24 assert_equals(getComputedStyle(div).left, '200px'); 25 }, 'Keyframes set using setKeyframes() are reflected in computed style for' 26 + ' a running transition'); 27 28 test(t => { 29 const div = addDiv(t); 30 31 div.style.left = '0px'; 32 getComputedStyle(div).transitionProperty; 33 div.style.transition = 'left 100s'; 34 div.style.left = '100px'; 35 36 const transition = div.getAnimations()[0]; 37 transition.effect.setKeyframes({ top: ['0px', '100px', '300px'] }); 38 39 assert_equals(transition.transitionProperty, 'left'); 40 }, 'A transition with replaced keyframes still returns the original' 41 + ' transitionProperty'); 42 43 test(t => { 44 const div = addDiv(t); 45 46 div.style.left = '0px'; 47 getComputedStyle(div).transitionProperty; 48 div.style.transition = 'left 100s'; 49 div.style.left = '100px'; 50 51 const transition = div.getAnimations()[0]; 52 transition.effect.setKeyframes({ }); 53 54 assert_equals(transition.transitionProperty, 'left'); 55 }, 'A transition with no keyframes still returns the original' 56 + ' transitionProperty'); 57 58 function retarget_test(description, newKeyframes, reversed_start) { 59 test(t => { 60 const div = addDiv(t); 61 62 div.style.left = '0px'; 63 getComputedStyle(div).transitionProperty; 64 div.style.transition = 'left 100s linear'; 65 div.style.left = '100px'; 66 67 const transition = div.getAnimations()[0]; 68 transition.currentTime = 50 * MS_PER_SEC; 69 transition.effect.setKeyframes(newKeyframes); 70 71 // Reverse transition 72 div.style.left = '0px'; 73 const reversedTransition = div.getAnimations()[0]; 74 75 assert_approx_equals( 76 reversedTransition.effect.getComputedTiming().activeDuration, 77 50 * MS_PER_SEC, 1, 78 "The reversed transition has correctly reduced duration" 79 ); 80 assert_equals(reversedTransition.effect.getKeyframes()[0].left, reversed_start, 81 "The reversed transition starts at the expected point"); 82 }, description); 83 } 84 85 retarget_test("A transition with replaced keyframes animating the same property " + 86 "still exhibits normal reversing behavior.", 87 {left: ['200px', '300px', '100px']}, '300px'); 88 89 retarget_test("A transition with replaced keyframes animating a different property " + 90 "still exhibits normal reversing behavior (reversing from the base value).", 91 {top: ['200px', '300px', '100px']}, '100px'); 92 93 retarget_test("A transition with replaced keyframes animating nothing " + 94 "still exhibits normal reversing behavior (reversing from the base value).", 95 {}, '100px'); 96 97 test(t => { 98 const div = addDiv(t); 99 100 // Initialize the style. 101 div.style.left = '100px'; 102 div.style.top = '100px'; 103 div.style.transition = 'left 100s linear, top 100s linear'; 104 getComputedStyle(div).left; 105 106 // Start some transitions. 107 div.style.left ='200px'; 108 div.style.top = '200px'; 109 const transitions = div.getAnimations(); 110 111 // Hand control of the left property over to top's transition. The composite 112 // ordering of the animations is 'left' then 'top' since the transitions were 113 // generated in the same transition generation and follow Unicode ordering. 114 assert_equals(transitions[0].transitionProperty, 'left'); 115 transitions[0].effect.setKeyframes({}); 116 transitions[1].effect.setKeyframes([ 117 {left: '100px', top: '100px'}, 118 {left: '400px', top: '200px'}]) 119 getComputedStyle(div).left; 120 121 // These updates form a single style change, equivalent to setting times and 122 // then setting left. 123 transitions[0].currentTime = 50 * MS_PER_SEC; 124 div.style.left ='100px'; 125 transitions[1].currentTime = 60 * MS_PER_SEC; 126 127 // As there was a style change event, the new 'left' transition now has a 128 // higher value for the transition generation than the 'top' transition, 129 // reversing the order of the transitions returned by getAnimations. 130 const reversedTransition = div.getAnimations()[1]; 131 assert_equals(reversedTransition.transitionProperty, 'left', 132 "A reversed transition on left is produced"); 133 134 assert_approx_equals( 135 reversedTransition.effect.getComputedTiming().activeDuration, 136 50 * MS_PER_SEC, 1, 137 "The reversed transition has correctly reduced duration (based on the " + 138 "original left transition)."); 139 140 assert_equals(reversedTransition.effect.getKeyframes()[0].left, '280px', 141 "The reversed transition gets its start value from the other " + 142 "transition controlling left"); 143 144 }, "A transition with replaced keyframes animating nothing on a property " + 145 "being controlled by another modified transition exhibits normal " + 146 "reversing behavior and reverses from the other transition's current " + 147 "value."); 148 149 test(t => { 150 const div = addDiv(t); 151 152 // Initialize the style. 153 div.style.left = '100px'; 154 div.style.transition = 'left 100s linear'; 155 getComputedStyle(div).left; 156 157 // Start the transtion. 158 div.style.left ='200px'; 159 const transition = div.getAnimations()[0]; 160 161 // Update the keyframes and advance to the midpoint of the animation. 162 transition.effect.setKeyframes([ 163 { offset: 0, left: '-50px', composite: 'add', easing: 'linear'}]); 164 transition.currentTime = 50 * MS_PER_SEC; 165 assert_equals(getComputedStyle(div).left, '175px', 166 'The computed style is based on the updated keyframes'); 167 168 div.style.left = '100px'; 169 const reversedTransition = div.getAnimations()[0]; 170 assert_equals(reversedTransition.effect.getKeyframes()[0].left, '175px', 171 'The start value matches the \'before change\' value'); 172 173 }, 'A transition with replaced kefyrames and composite \'add\' exhibits ' + 174 'normal reversing behavior, and the effect is not double counted when ' + 175 'calculating the before change style'); 176 177 </script>