Skip to content

Commit

Permalink
fix: animated transform last frame (#2553)
Browse files Browse the repository at this point in the history
# Summary

When using the Animated API for animations, it sends the last frame as
JavaScript-parsed (matrix) updates in addition to native (transform)
updates. ~~As a result, we need to ignore one of them.~~ I believe
there's no need to differentiate between native and JavaScript
updates—we can simply save both to the same value (mMatrix). By doing
so, we can avoid duplicating the transforms.

| Before | After |
|--------|--------|
| <video
src="https://github.com/user-attachments/assets/868cc778-4b88-4473-85b5-9665b4b241aa">
| <video
src="https://github.com/user-attachments/assets/c6d17b7b-7c9a-47c3-8286-2d9b5720f261">
|




## Test Plan

```jsx
import React, {useEffect} from 'react';
import {Animated, useAnimatedValue, View} from 'react-native';
import {Rect, Svg} from 'react-native-svg';

const AnimatedRect = Animated.createAnimatedComponent(Rect);
function AnimatedJumpIssue() {
  const animatedValue = useAnimatedValue(100);
  return (
    <>
      <View style={{borderColor: 'black', borderWidth: 1}}>
        <Svg height="100" width="400">
          <AnimatedRect
            x="0"
            y="0"
            width="100"
            height="100"
            fill="black"
            transform={[{translateX: animatedValue}]}
          />
        </Svg>
      </View>
      <Button
        title="Press me"
        onPress={() => {
          Animated.timing(animatedValue, {
            toValue: 200,
            duration: 3000,
            useNativeDriver: true,
          }).start();
        }}
      />
    </>
  );
}
```

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| Android |    ✅      |
| iOS |    ✅      |
| macOS |    ✅      |
  • Loading branch information
jakex7 authored Nov 28, 2024
1 parent d125be1 commit c617dec
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 46 deletions.
5 changes: 1 addition & 4 deletions android/src/main/java/com/horcrux/svg/GroupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ void setupGlyphContext(Canvas canvas) {
if (mMatrix != null) {
mMatrix.mapRect(clipBounds);
}
if (mTransform != null) {
mTransform.mapRect(clipBounds);
}
mGlyphContext = new GlyphContext(mScale, clipBounds.width(), clipBounds.height());
}

Expand Down Expand Up @@ -258,7 +255,7 @@ Path getPath(final Canvas canvas, final Paint paint, final Region.Op op) {

@Override
int hitTest(final float[] src) {
if (!mInvertible || !mTransformInvertible) {
if (!mInvertible) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/horcrux/svg/RenderableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ private void setupPaint(Paint paint, float opacity, ReadableArray colors) {

@Override
int hitTest(final float[] src) {
if (mPath == null || !mInvertible || !mTransformInvertible) {
if (mPath == null || !mInvertible) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ public void setTransform(VirtualView node, @Nullable ReadableArray matrix) {
}

Matrix m = node.getMatrix();
node.mTransform = m;
node.mTransformInvertible = m.invert(node.mInvTransform);
node.mMatrix = m;
node.mInvertible = m.invert(node.mInvMatrix);
}

@ReactProp(name = "transform")
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/horcrux/svg/TSpanView.java
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ int hitTest(final float[] src) {
if (mContent == null) {
return super.hitTest(src);
}
if (mPath == null || !mInvertible || !mTransformInvertible) {
if (mPath == null || !mInvertible) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/horcrux/svg/UseView.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void draw(Canvas canvas, Paint paint, float opacity) {

@Override
int hitTest(float[] src) {
if (!mInvertible || !mTransformInvertible) {
if (!mInvertible) {
return -1;
}

Expand Down
5 changes: 1 addition & 4 deletions android/src/main/java/com/horcrux/svg/VirtualView.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ public abstract class VirtualView extends ReactViewGroup {
float mOpacity = 1f;
Matrix mCTM = new Matrix();
Matrix mMatrix = new Matrix();
Matrix mTransform = new Matrix();
Matrix mInvCTM = new Matrix();
Matrix mInvMatrix = new Matrix();
final Matrix mInvTransform = new Matrix();
boolean mInvertible = true;
boolean mCTMInvertible = true;
boolean mTransformInvertible = true;
private RectF mClientRect;

int mClipRule;
Expand Down Expand Up @@ -249,7 +247,7 @@ void render(Canvas canvas, Paint paint, float opacity) {
*/
int saveAndSetupCanvas(Canvas canvas, Matrix ctm) {
int count = canvas.save();
mCTM.setConcat(mMatrix, mTransform);
mCTM.set(mMatrix);
canvas.concat(mCTM);
mCTM.preConcat(ctm);
mCTMInvertible = mCTM.invert(mInvCTM);
Expand Down Expand Up @@ -360,7 +358,6 @@ Path getClipPath(Canvas canvas, Paint paint) {
? mClipNode.getPath(canvas, paint)
: mClipNode.getPath(canvas, paint, Region.Op.UNION);
clipPath.transform(mClipNode.mMatrix);
clipPath.transform(mClipNode.mTransform);
switch (mClipRule) {
case CLIP_RULE_EVENODD:
clipPath.setFillType(Path.FillType.EVEN_ODD);
Expand Down
3 changes: 1 addition & 2 deletions apple/Elements/RNSVGForeignObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ - (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect
self.ctm = svgToClientTransform;
self.screenCTM = current;

CGAffineTransform transform = CGAffineTransformConcat(self.matrix, self.transforms);
CGPoint mid = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGPoint center = CGPointApplyAffineTransform(mid, transform);
CGPoint center = CGPointApplyAffineTransform(mid, self.matrix);

self.bounds = bounds;
if (!isnan(center.x) && !isnan(center.y)) {
Expand Down
7 changes: 2 additions & 5 deletions apple/Elements/RNSVGGroup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ - (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect
self.ctm = svgToClientTransform;
self.screenCTM = current;

CGAffineTransform transform = CGAffineTransformConcat(self.matrix, self.transforms);
CGPoint mid = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGPoint center = CGPointApplyAffineTransform(mid, transform);
CGPoint center = CGPointApplyAffineTransform(mid, self.matrix);

self.bounds = bounds;
if (!isnan(center.x) && !isnan(center.y)) {
Expand All @@ -163,7 +162,6 @@ - (void)setupGlyphContext:(CGContextRef)context
}
#endif // macOS]
clipBounds = CGRectApplyAffineTransform(clipBounds, self.matrix);
clipBounds = CGRectApplyAffineTransform(clipBounds, self.transforms);
CGFloat width = CGRectGetWidth(clipBounds);
CGFloat height = CGRectGetHeight(clipBounds);

Expand Down Expand Up @@ -200,7 +198,7 @@ - (CGPathRef)getPath:(CGContextRef)context
CGMutablePathRef __block path = CGPathCreateMutable();
[self traverseSubviews:^(RNSVGNode *node) {
if ([node isKindOfClass:[RNSVGNode class]] && ![node isKindOfClass:[RNSVGMask class]]) {
CGAffineTransform transform = CGAffineTransformConcat(node.matrix, node.transforms);
CGAffineTransform transform = node.matrix;
CGPathAddPath(path, &transform, [node getPath:context]);
CGPathAddPath(path, &transform, [node markerPath]);
node.dirty = false;
Expand All @@ -216,7 +214,6 @@ - (CGPathRef)getPath:(CGContextRef)context
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint transformed = CGPointApplyAffineTransform(point, self.invmatrix);
transformed = CGPointApplyAffineTransform(transformed, self.invTransform);

if (!CGRectContainsPoint(self.pathBounds, transformed)) {
return nil;
Expand Down
3 changes: 1 addition & 2 deletions apple/Elements/RNSVGImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,8 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
self.ctm = svgToClientTransform;
self.screenCTM = current;

CGAffineTransform transform = CGAffineTransformConcat(self.matrix, self.transforms);
CGPoint mid = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGPoint center = CGPointApplyAffineTransform(mid, transform);
CGPoint center = CGPointApplyAffineTransform(mid, self.matrix);

self.bounds = bounds;
if (!isnan(center.x) && !isnan(center.y)) {
Expand Down
4 changes: 1 addition & 3 deletions apple/Elements/RNSVGUse.mm
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
self.ctm = svgToClientTransform;
self.screenCTM = current;

CGAffineTransform transform = CGAffineTransformConcat(self.matrix, self.transforms);
CGPoint mid = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGPoint center = CGPointApplyAffineTransform(mid, transform);
CGPoint center = CGPointApplyAffineTransform(mid, self.matrix);

self.bounds = bounds;
if (!isnan(center.x) && !isnan(center.y)) {
Expand All @@ -190,7 +189,6 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint transformed = CGPointApplyAffineTransform(point, self.invmatrix);
transformed = CGPointApplyAffineTransform(transformed, self.invTransform);
RNSVGNode const *definedTemplate = [self.svgView getDefinedTemplate:self.href];
if (event) {
self.active = NO;
Expand Down
4 changes: 2 additions & 2 deletions apple/RNSVGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE;
@property (nonatomic, assign) CGAffineTransform ctm;
@property (nonatomic, assign) CGAffineTransform screenCTM;
@property (nonatomic, assign) CGAffineTransform matrix;
@property (nonatomic, assign) CGAffineTransform transforms;
@property (nonatomic, assign) CGAffineTransform invmatrix;
@property (nonatomic, assign) CGAffineTransform invTransform;
@property (nonatomic, assign) BOOL active;
@property (nonatomic, assign) BOOL dirty;
@property (nonatomic, assign) BOOL merging;
Expand Down Expand Up @@ -148,4 +146,6 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE;

- (CGFloat)getCanvasHeight;

- (void)setTransforms:(CGAffineTransform)transforms;

@end
12 changes: 3 additions & 9 deletions apple/RNSVGNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ - (instancetype)init
self.opaque = false;
#endif
self.matrix = CGAffineTransformIdentity;
self.transforms = CGAffineTransformIdentity;
self.invTransform = CGAffineTransformIdentity;
_merging = false;
_dirty = false;
}
Expand Down Expand Up @@ -250,11 +248,11 @@ - (void)setMatrix:(CGAffineTransform)matrix

- (void)setTransforms:(CGAffineTransform)transforms
{
if (CGAffineTransformEqualToTransform(transforms, _transforms)) {
if (CGAffineTransformEqualToTransform(transforms, _matrix)) {
return;
}

_transforms = transforms;
_matrix = transforms;
[self invalidate];
}

Expand Down Expand Up @@ -377,7 +375,7 @@ - (CGPathRef)getClipPath:(CGContextRef)context
if (_cachedClipPath) {
CGPathRelease(_cachedClipPath);
}
CGAffineTransform transform = CGAffineTransformConcat(_clipNode.matrix, _clipNode.transforms);
CGAffineTransform transform = _clipNode.matrix;
_cachedClipPath = CGPathCreateCopyByTransformingPath([_clipNode getPath:context], &transform);
}

Expand Down Expand Up @@ -630,8 +628,6 @@ - (void)prepareForRecycle
self.opaque = false;
#endif
self.matrix = CGAffineTransformIdentity;
self.transforms = CGAffineTransformIdentity;
self.invTransform = CGAffineTransformIdentity;
_merging = false;
_dirty = false;

Expand All @@ -652,9 +648,7 @@ - (void)prepareForRecycle
_ctm = CGAffineTransformIdentity;
_screenCTM = CGAffineTransformIdentity;
_matrix = CGAffineTransformIdentity;
_transforms = CGAffineTransformIdentity;
_invmatrix = CGAffineTransformIdentity;
_invTransform = CGAffineTransformIdentity;
_active = NO;
_skip = NO;
if (_markerPath) {
Expand Down
5 changes: 1 addition & 4 deletions apple/RNSVGRenderable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
// This needs to be painted on a layer before being composited.
CGContextSaveGState(context);
CGContextConcatCTM(context, self.matrix);
CGContextConcatCTM(context, self.transforms);
CGContextSetAlpha(context, self.opacity);

[self beginTransparencyLayer:context];
Expand Down Expand Up @@ -530,8 +529,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
}

CGAffineTransform vbmatrix = self.svgView.getViewBoxTransform;
CGAffineTransform transform = CGAffineTransformConcat(self.matrix, self.transforms);
CGAffineTransform matrix = CGAffineTransformConcat(transform, vbmatrix);
CGAffineTransform matrix = CGAffineTransformConcat(self.matrix, vbmatrix);

CGRect bounds = CGRectMake(0, 0, CGRectGetWidth(clientRect), CGRectGetHeight(clientRect));
CGPoint mid = CGPointMake(CGRectGetMidX(pathBounds), CGRectGetMidY(pathBounds));
Expand Down Expand Up @@ -687,7 +685,6 @@ - (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
}

CGPoint transformed = CGPointApplyAffineTransform(point, self.invmatrix);
transformed = CGPointApplyAffineTransform(transformed, self.invTransform);

if (!CGRectContainsPoint(self.pathBounds, transformed) && !CGRectContainsPoint(self.markerBounds, transformed)) {
return nil;
Expand Down
11 changes: 6 additions & 5 deletions apple/Utils/RNSVGFabricConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ void setCommonNodeProps(const T &nodeProps, RNSVGNode *node)
nodeProps.matrix.at(4),
nodeProps.matrix.at(5));
}
auto newTransform = nodeProps.resolveTransform(MinimalLayoutMetrics);
CATransform3D transform3d = RCTCATransform3DFromTransformMatrix(newTransform);
CGAffineTransform transform = CATransform3DGetAffineTransform(transform3d);
node.invTransform = CGAffineTransformInvert(transform);
node.transforms = transform;
if (nodeProps.transform.operations.size() > 0) {
auto newTransform = nodeProps.resolveTransform(MinimalLayoutMetrics);
CATransform3D transform3d = RCTCATransform3DFromTransformMatrix(newTransform);
CGAffineTransform transform = CATransform3DGetAffineTransform(transform3d);
node.transforms = transform;
}
node.mask = RCTNSStringFromStringNilIfEmpty(nodeProps.mask);
node.markerStart = RCTNSStringFromStringNilIfEmpty(nodeProps.markerStart);
node.markerMid = RCTNSStringFromStringNilIfEmpty(nodeProps.markerMid);
Expand Down
1 change: 0 additions & 1 deletion apple/ViewManagers/RNSVGNodeManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ - (RNSVGPlatformView *)view
{
CATransform3D transform3d = json ? [RCTConvert CATransform3D:json] : defaultView.layer.transform;
CGAffineTransform transform = CATransform3DGetAffineTransform(transform3d);
view.invTransform = CGAffineTransformInvert(transform);
view.transforms = transform;
}
RCT_EXPORT_VIEW_PROPERTY(mask, NSString)
Expand Down

0 comments on commit c617dec

Please sign in to comment.