Skip to content

Commit

Permalink
feat: implement isPointInStroke
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Oct 4, 2019
1 parent f13d54a commit 2ba64df
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
19 changes: 19 additions & 0 deletions android/src/main/java/com/horcrux/svg/RNSVGRenderableManager.java
Expand Up @@ -13,6 +13,7 @@
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.Region;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -78,6 +79,24 @@ public void isPointInFill(int tag, ReadableMap options, Callback successCallback
isPointInFill(tag, src, successCallback, 0);
}

@SuppressWarnings("unused")
@ReactMethod
public void isPointInStroke(int tag, ReadableMap options, Callback successCallback) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) {
successCallback.invoke(false);
return;
}
svg.getPath(null, null);
svg.initBounds();
Region strokeRegion = svg.mStrokeRegion;
float scale = svg.mScale;
int x = (int) (options.getDouble("x") * scale);
int y = (int) (options.getDouble("y") * scale);
boolean hit = strokeRegion != null && strokeRegion.contains(x, y);
successCallback.invoke(hit);
}

@SuppressWarnings("unused")
@ReactMethod
public void getTotalLength(int tag, Callback successCallback) {
Expand Down
30 changes: 30 additions & 0 deletions ios/ViewManagers/RNSVGRenderableManager.m
Expand Up @@ -94,6 +94,36 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
attempt:0];
}

RCT_EXPORT_METHOD(isPointInStroke:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{
if (options == nil) {
RCTLogError(@"Invalid options given to isPointInFill, got: %@", options);
callback(@[[NSNumber numberWithBool:false]]);
return;
}
id xo = [options objectForKey:@"x"];
id yo = [options objectForKey:@"y"];
if (![xo isKindOfClass:NSNumber.class] ||
![yo isKindOfClass:NSNumber.class]) {
RCTLogError(@"Invalid x or y given to isPointInFill");
callback(@[[NSNumber numberWithBool:false]]);
return;
}
[self
withTag:reactTag
success:^(RNSVGRenderable *svg){
CGFloat x = (CGFloat)[xo floatValue];
CGFloat y = (CGFloat)[yo floatValue];
CGPoint point = CGPointMake(x, y);
BOOL hit = CGPathContainsPoint(svg.strokePath, nil, point, NO);
callback(@[[NSNumber numberWithBool:hit]]);
}
fail:^{
callback(@[[NSNumber numberWithBool:false]]);
}
attempt:0];
}

RCT_EXPORT_METHOD(getTotalLength:(nonnull NSNumber *)reactTag callback:(RCTResponseSenderBlock)callback)
{
[self
Expand Down
2 changes: 1 addition & 1 deletion src/elements/Shape.tsx
Expand Up @@ -99,7 +99,7 @@ export default class Shape<P> extends Component<P> {
return;
}
const handle = findNodeHandle(this.root as Component);
RNSVGRenderableManager.isPointInStroke(handle, options, callback); // TODO
RNSVGRenderableManager.isPointInStroke(handle, options, callback);
};
getTotalLength = (callback: () => void) => {
if (!callback) {
Expand Down
2 changes: 1 addition & 1 deletion src/elements/Svg.tsx
Expand Up @@ -88,7 +88,7 @@ export default class Svg extends Shape<
this.root && this.root.setNativeProps(props);
};

toDataURL = (callback: () => void, options: Object) => {
toDataURL = (callback: () => void, options?: Object) => {
if (!callback) {
return;
}
Expand Down

0 comments on commit 2ba64df

Please sign in to comment.