Skip to content

Commit f9ff72c

Browse files
committed
feat: support change the current animation stage for transition animations.
1 parent 9a64a40 commit f9ff72c

File tree

12 files changed

+152
-59
lines changed

12 files changed

+152
-59
lines changed
3 KB
Loading
3.02 KB
Loading
3.05 KB
Loading
3.04 KB
Loading
3.01 KB
Loading

integration_tests/specs/css/css-transitions/all.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,48 @@ describe('Transition all', () => {
313313

314314
});
315315
});
316+
317+
it('toggle the transition class should the previous animation and run new animations', async (done) => {
318+
const container1 = document.createElement('div');
319+
document.body.appendChild(container1);
320+
setElementStyle(container1, {
321+
position: 'absolute',
322+
top: '100px',
323+
left: 0,
324+
padding: '20px',
325+
backgroundColor: '#999',
326+
});
327+
container1.appendChild(document.createTextNode('DIV'));
328+
await snapshot();
329+
330+
container1.addEventListener('transitionend', async () => {
331+
console.log('end');
332+
await snapshot();
333+
done();
334+
});
335+
336+
requestAnimationFrame(async () => {
337+
setElementStyle(container1, {
338+
transform: 'translate3d(200px, 0, 0)',
339+
transition: 'all 1s linear',
340+
});
341+
await snapshot();
342+
343+
setTimeout(async () => {
344+
setElementStyle(container1, {
345+
transform: 'translate3d(0px, 100px, 0)',
346+
});
347+
348+
await snapshot();
349+
350+
setTimeout(async () => {
351+
setElementStyle(container1, {
352+
transform: 'translate3d(200px, 200px, 0)',
353+
});
354+
355+
await snapshot();
356+
}, 500);
357+
}, 500);
358+
});
359+
});
316360
});

webf/lib/src/css/animation.dart

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ class Animation {
178178
return _currentTime;
179179
}
180180

181+
double get progress {
182+
return _effect?._progress ?? 0.0;
183+
}
184+
181185
set currentTime(double? newTime) {
182186
if (newTime == null) return;
183187

@@ -437,7 +441,15 @@ class _Interpolation {
437441
var begin;
438442
var end;
439443
Function lerp;
440-
_Interpolation(this.property, this.startOffset, this.endOffset, this.easing, this.begin, this.end, this.lerp) {
444+
445+
_Interpolation(
446+
{required this.property,
447+
required this.startOffset,
448+
required this.endOffset,
449+
required this.easing,
450+
required this.begin,
451+
required this.end,
452+
required this.lerp}) {
441453
easing ??= Curves.linear;
442454
}
443455

@@ -456,6 +468,7 @@ class KeyframeEffect extends AnimationEffect {
456468
RenderStyle renderStyle;
457469
Element? target;
458470
late List<_Interpolation> _interpolations;
471+
List<_Interpolation> get interpolations => _interpolations;
459472
double? _progress;
460473
double? _activeTime;
461474
late Map<String, List<Keyframe>> _propertySpecificKeyframeGroups;
@@ -516,13 +529,13 @@ class KeyframeEffect extends AnimationEffect {
516529
Function parseProperty = handlers[0];
517530

518531
_Interpolation interpolation = _Interpolation(
519-
property,
520-
startOffset,
521-
endOffset,
522-
_parseEasing(keyframes[startIndex].easing),
523-
parseProperty(left, renderStyle, property),
524-
parseProperty(right, renderStyle, property),
525-
handlers[1]);
532+
property: property,
533+
startOffset: startOffset,
534+
endOffset: endOffset,
535+
easing: _parseEasing(keyframes[startIndex].easing),
536+
begin: parseProperty(left, renderStyle, property),
537+
end: parseProperty(right, renderStyle, property),
538+
lerp: handlers[1]);
526539

527540
interpolations.add(interpolation);
528541
}
@@ -565,7 +578,6 @@ class KeyframeEffect extends AnimationEffect {
565578
if (_progress == null) {
566579
// If fill is backwards that will be null when animation finished
567580
_propertySpecificKeyframeGroups.forEach((String propertyName, value) {
568-
renderStyle.removeAnimationProperty(propertyName);
569581
String currentValue = renderStyle.target.style.getPropertyValue(propertyName);
570582
renderStyle.target.setRenderStyle(propertyName, currentValue);
571583
});
@@ -806,6 +818,7 @@ class KeyframeEffect extends AnimationEffect {
806818

807819
// The smallest positive double value that is greater than zero.
808820
static final double _epsilon = 4.94065645841247E-324;
821+
809822
// Permit 2-bits of quantization error. Threshold based on experimentation
810823
// with accuracy of fmod.
811824
static final double _calculationEpsilon = 2.0 * _epsilon;
@@ -901,32 +914,39 @@ class EffectTiming {
901914
// Defaults to 0. Although this is technically optional,
902915
// keep in mind that your animation will not run if this value is 0.
903916
double? _duration;
917+
904918
// The number of milliseconds to delay the start of the animation.
905919
// Defaults to 0.
906920
double? _delay;
921+
907922
// The rate of the animation's change over time.
908923
// Accepts the pre-defined values "linear", "ease", "ease-in", "ease-out", and "ease-in-out",
909924
// or a custom "cubic-bezier" value like "cubic-bezier(0.42, 0, 0.58, 1)".
910925
// Defaults to "linear".
911926
String? _easing;
912927
Curve? _easingCurve;
928+
913929
// Whether the animation runs forwards (normal), backwards (reverse),
914930
// switches direction after each iteration (alternate),
915931
// or runs backwards and switches direction after each iteration (alternate-reverse).
916932
// Defaults to "normal".
917933
PlaybackDirection? _direction;
934+
918935
// The number of milliseconds to delay after the end of an animation.
919936
// This is primarily of use when sequencing animations based on the end time of another animation.
920937
// Defaults to 0.
921938
double? _endDelay;
939+
922940
// Dictates whether the animation's effects should be reflected by the element(s) prior to playing ("backwards"),
923941
// retained after the animation has completed playing ("forwards"), or both. Defaults to "none".
924942
FillMode? _fill;
943+
925944
// Describes at what point in the iteration the animation should start.
926945
// 0.5 would indicate starting halfway through the first iteration for example,
927946
// and with this value set, an animation with 2 iterations would end halfway through a third iteration.
928947
// Defaults to 0.0.
929948
double? _iterationStart;
949+
930950
// The number of times the animation should repeat.
931951
// Defaults to 1, and can also take a value of Infinity to make it repeat for as long as the element exists.
932952
double? _iterations;

webf/lib/src/css/css_animation.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,8 @@ mixin CSSAnimationMixin on RenderStyle {
9494
}
9595

9696
final Map<String, Animation> _runningAnimation = {};
97-
final Map<String, String> _animationProperties = {};
9897
final Map<String, String> _cacheOriginProperties = {};
9998

100-
@override
101-
String? removeAnimationProperty(String propertyName) {
102-
String? prevValue = EMPTY_STRING;
103-
104-
if (_animationProperties.containsKey(propertyName)) {
105-
prevValue = _animationProperties[propertyName];
106-
_animationProperties.remove(propertyName);
107-
}
108-
109-
return prevValue;
110-
}
111-
11299
String _getSingleString(List<String> list, int index) {
113100
return list[index];
114101
}

webf/lib/src/css/origin.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import 'package:flutter/rendering.dart';
66
import 'package:webf/css.dart';
77

88
final RegExp _spaceRegExp = RegExp(r'\s+(?![^(]*\))');
9+
final RegExp _originRegExp = RegExp(r'CSSOrigin\([\w\W]+\)');
910

1011
class CSSOrigin {
1112
final Offset offset;
1213
final Alignment alignment;
1314
const CSSOrigin(this.offset, this.alignment);
1415

1516
static CSSOrigin? parseOrigin(String origin, RenderStyle renderStyle, String property) {
17+
// Interval stringify transform origin values.
18+
if (origin.startsWith('CSSOrigin')) {
19+
String content = _originRegExp.firstMatch(origin)![1]!;
20+
List<String> values = content.split(',');
21+
22+
return CSSOrigin(
23+
Offset(double.parse(values[0]), double.parse(values[1])),
24+
Alignment(double.parse(values[2]), double.parse(values[3])));
25+
}
26+
1627
if (origin.isNotEmpty) {
1728
List<String> originList = origin.trim().split(_spaceRegExp);
1829
String? x, y;

webf/lib/src/css/render_style.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ abstract class RenderStyle {
203203
void addFontRelativeProperty(String propertyName);
204204
void addRootFontRelativeProperty(String propertyName);
205205
void addColorRelativeProperty(String propertyName);
206-
String? removeAnimationProperty(String propertyName);
207206
double getWidthByAspectRatio();
208207
double getHeightByAspectRatio();
209208

0 commit comments

Comments
 (0)