Skip to content

Commit cd667d0

Browse files
committed
feat(android): implement getTotalLength and getPointAtLength
1 parent 78c4f20 commit cd667d0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

android/src/main/java/com/horcrux/svg/RNSVGRenderableManager.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package com.horcrux.svg;
1111

12+
import android.graphics.PathMeasure;
13+
1214
import com.facebook.react.bridge.Callback;
1315
import com.facebook.react.bridge.ReactApplicationContext;
1416
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -72,4 +74,25 @@ public void isPointInFill(int tag, ReadableMap options, Callback successCallback
7274
float[] src = new float[] { x, y };
7375
isPointInFill(tag, src, successCallback, 0);
7476
}
77+
78+
@SuppressWarnings("unused")
79+
@ReactMethod
80+
public void getTotalLength(int tag, Callback successCallback) {
81+
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
82+
PathMeasure pm = new PathMeasure(svg.getPath(null, null), false);
83+
successCallback.invoke(pm.getLength());
84+
}
85+
86+
@SuppressWarnings("unused")
87+
@ReactMethod
88+
public void getPointAtLength(int tag, ReadableMap options, Callback successCallback) {
89+
float length = (float)options.getDouble("length");
90+
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
91+
PathMeasure pm = new PathMeasure(svg.getPath(null, null), false);
92+
float pathLength = pm.getLength();
93+
float[] pos = new float[2];
94+
float[] tan = new float[2];
95+
pm.getPosTan(Math.max(0, Math.min(length, pathLength)), pos, tan);
96+
successCallback.invoke(pos[0], pos[1]);
97+
}
7598
}

0 commit comments

Comments
 (0)