Skip to content

Commit

Permalink
feat: strokeDasharray with Animated (#2089)
Browse files Browse the repository at this point in the history
PR adding the proper handling of strokeDasharray prop for when used with Animated and Reanimated. Code based on #1464 by @bardliu, but with the newest state of the repository.
  • Loading branch information
WoLewicki committed Jul 7, 2023
1 parent 24dba65 commit 78aaffa
Show file tree
Hide file tree
Showing 40 changed files with 219 additions and 23 deletions.
3 changes: 2 additions & 1 deletion TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Test1845 from './src/Test1845';
import Test2080 from './src/Test2080';
import PointerEventsBoxNone from './src/PointerEventsBoxNone';
import Test2071 from './src/Test2071';
import Test2089 from './src/Test2089';

export default function App() {
return <Test1718 />;
return <ColorTest />;
}
66 changes: 66 additions & 0 deletions TestsExample/src/Test2089.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, {useEffect} from 'react';
import Animated, {
useSharedValue,
withTiming,
Easing,
withRepeat,
interpolate,
useDerivedValue,
useAnimatedProps,
} from 'react-native-reanimated';
import {Svg, Circle} from 'react-native-svg';

export default () => {
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
const animatedValue = useSharedValue(0);
const timingAnimatedValue = useDerivedValue(() =>
withRepeat(
withTiming(animatedValue.value, {
duration: 1250,
easing: Easing.out(Easing.cubic),
}),
-1,
),
);
const animatedProps = useAnimatedProps(() => ({
strokeDasharray: [
interpolate(
timingAnimatedValue.value,
[0, 50, 100],
[8.1681408993, 40.8407044967, 8.1681408993],
),
interpolate(
timingAnimatedValue.value,
[0, 50, 100],
[73.513268094, 40.8407045967, 73.513268094],
),
],
}));

useEffect(() => {
animatedValue.value = 100;
}, []);

return (
<Animated.View
style={{
flex: 1,
paddingTop: 200,
justifyContent: 'center',
alignContent: 'center',
}}>
<Svg>
<AnimatedCircle
cx="15"
cy="15"
r="13"
stroke="red"
strokeWidth="2.5"
strokeLinecap="round"
animatedProps={animatedProps}
fill="none"
/>
</Svg>
</Animated.View>
);
};
22 changes: 22 additions & 0 deletions android/src/main/java/com/horcrux/svg/RenderableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ public void setStrokeDasharray(@Nullable ReadableArray strokeDasharray) {
invalidate();
}

public void setStrokeDasharray(@Nullable String strokeDasharray) {
if (strokeDasharray != null) {
String stringValue = strokeDasharray.trim();
stringValue = stringValue.replaceAll(",", " ");
String[] strings = stringValue.split(" ");
ArrayList<SVGLength> list = new ArrayList<>(strings.length);
for (String length : strings) {
list.add(new SVGLength(length));
}
this.strokeDasharray = new SVGLength[Math.max(list.size(), 2)];
for (int i = 0; i < list.size(); i++) {
this.strokeDasharray[i] = list.get(i);
}
if (list.size() == 1) {
this.strokeDasharray[1] = this.strokeDasharray[0];
}
} else {
this.strokeDasharray = null;
}
invalidate();
}

public void setStrokeDashoffset(float strokeDashoffset) {
this.strokeDashoffset = strokeDashoffset * mScale;
invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,11 @@ public void setStrokeDasharray(T node, @Nullable ReadableArray strokeDasharray)
node.setStrokeDasharray(strokeDasharray);
}

@ReactProp(name = "strokeDasharray")
public void setStrokeDasharray(T node, @Nullable String strokeDasharray) {
node.setStrokeDasharray(strokeDasharray);
}

@ReactProp(name = "strokeDashoffset")
public void setStrokeDashoffset(T node, float strokeDashoffset) {
node.setStrokeDashoffset(strokeDashoffset);
Expand Down
11 changes: 8 additions & 3 deletions android/src/main/java/com/horcrux/svg/SVGLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private SVGLength() {
unit = UnitType.NUMBER;
}

private SVGLength(String length) {
SVGLength(String length) {
length = length.trim();
int stringLength = length.length();
int percentIndex = stringLength - 1;
Expand Down Expand Up @@ -143,8 +143,13 @@ static ArrayList<SVGLength> arrayFrom(Dynamic dynamic) {
}
case String:
{
ArrayList<SVGLength> list = new ArrayList<>(1);
list.add(new SVGLength(dynamic.asString()));
String stringValue = dynamic.asString().trim();
stringValue = stringValue.replaceAll(",", " ");
String[] strings = stringValue.split(" ");
ArrayList<SVGLength> list = new ArrayList<>(strings.length);
for (String length : strings) {
list.add(new SVGLength(length));
}
return list;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGCircleManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGClipPathManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGEllipseManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGForeignObjectManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGGroupManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGImageManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGLineManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGMarkerManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGMaskManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGPathManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGPatternManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void setProperty(T view, String propName, @Nullable Object value) {
mViewManager.setStrokeLinejoin(view, value == null ? 0 : ((Double) value).intValue());
break;
case "strokeDasharray":
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
if (value instanceof String) {
mViewManager.setStrokeDasharray(view, (String) value);
} else if (value instanceof ReadableArray) {
mViewManager.setStrokeDasharray(view, (ReadableArray) value);
}
break;
case "strokeDashoffset":
mViewManager.setStrokeDashoffset(view, value == null ? 0f : ((Double) value).floatValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface RNSVGRectManagerInterface<T extends View> {
void setStrokeLinecap(T view, int value);
void setStrokeLinejoin(T view, int value);
void setStrokeDasharray(T view, @Nullable ReadableArray value);
void setStrokeDasharray(T view, @Nullable String value);
void setStrokeDashoffset(T view, float value);
void setStrokeMiterlimit(T view, float value);
void setVectorEffect(T view, int value);
Expand Down
Loading

0 comments on commit 78aaffa

Please sign in to comment.