Skip to content

Commit

Permalink
Fix ReactNativeART arc drawing on Android
Browse files Browse the repository at this point in the history
Summary:
**Motivation**: Arc drawing has been broken on Android for some time. dgladkov submitted a PR, which ended up having a bug and was never merged. This PR should fix that bug as well as provide screenshots to prove it works.

**Reproducing the Bug:** dgladkov made a simple test app which helps to illustrate the bug. The repo can be found [here](https://github.com/dgladkov/RNArtArcDrawingBug). The demo app illustrates that on iOS, wedges are drawn correctly, but Android only draws full circles. [Direct Link to iOS Before](https://github.com/dgladkov/RNArtArcDrawingBug/blob/master/images/ios.png). [Direct Link to Android Before](https://github.com/dgladkov/RNArtArcDrawingBug/blob/master/images/android.png).

**Proof The Bug is Fixed:** [Here is a direct link to Android After pic.](http://i.imgur.com/9dTU2Xn.png) You can see the wedges match the iOS Before screenshot.

**What went wrong:** dgladkov's solution relied on Java's modulus, which in fact, implements modulus in a non-standard way. Modulus should a
Closes #7049

Differential Revision: D3234404

Pulled By: spicyj

fb-gh-sync-id: 4974b818dc49d9d16f2483c49b462c459a8bb479
fbshipit-source-id: 4974b818dc49d9d16f2483c49b462c459a8bb479
  • Loading branch information
MikeOrtiz authored and Facebook Github Bot 9 committed Apr 28, 2016
1 parent d363b1f commit 1af4760
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ protected boolean setupFillPaint(Paint paint, float opacity) {
return false;
}

/**
* Returns the floor modulus of the float arguments. Java modulus will return a negative remainder
* when the divisor is negative. Modulus should always be positive. This mimics the behavior of
* Math.floorMod, introduced in Java 8.
*/
private float modulus(float x, float y) {
float remainder = x % y;
float modulus = remainder;
if (remainder < 0) {
modulus += y;
}
return modulus;
}

/**
* Creates a {@link Path} from an array of instructions constructed by JS
* (see ARTSerializablePath.js). Each instruction starts with a type (see PATH_TYPE_*) followed
Expand Down Expand Up @@ -232,11 +246,18 @@ private Path createPath(float[] data) {
float r = data[i++] * mScale;
float start = (float) Math.toDegrees(data[i++]);
float end = (float) Math.toDegrees(data[i++]);
boolean clockwise = data[i++] == 0f;
if (!clockwise) {
end = 360 - end;
boolean clockwise = data[i++] == 1f;
float sweep = end - start;
if (Math.abs(sweep) > 360) {
sweep = 360;
} else {
sweep = modulus(sweep, 360);
}
if (!clockwise && sweep < 360) {
start = end;
sweep = 360 - sweep;
}
float sweep = start - end;

RectF oval = new RectF(x - r, y - r, x + r, y + r);
path.addArc(oval, start, sweep);
break;
Expand Down

0 comments on commit 1af4760

Please sign in to comment.