Revert "Revert "change circle radius percentage calculation""

This reverts commit f717d8bd93.
This commit is contained in:
Horcrux
2016-04-27 16:49:23 +08:00
parent f717d8bd93
commit 6ad74e3287
8 changed files with 49 additions and 21 deletions

View File

@@ -11,12 +11,12 @@ class CircleExample extends Component{
render() { render() {
return <Svg return <Svg
height="100" height="100"
width="100" width="140"
> >
<Circle <Circle
cx="50" cx="50%"
cy="50" cy="50%"
r="50" r="40%"
fill="pink" fill="pink"
/> />
</Svg>; </Svg>;

View File

@@ -14,10 +14,10 @@ class EllipseExample extends Component{
width="110" width="110"
> >
<Ellipse <Ellipse
cx="55" cx="50%"
cy="55" cy="50%"
rx="50" rx="45%"
ry="30" ry="30%"
stroke="purple" stroke="purple"
strokeWidth="2" strokeWidth="2"
fill="yellow" fill="yellow"

View File

@@ -15,10 +15,10 @@ class LineExample extends Component{
width="100" width="100"
> >
<Line <Line
x1="0" x1="10%"
y1="0" y1="10%"
x2="100" x2="90%"
y2="100" y2="90%"
stroke="red" stroke="red"
strokeWidth="2" strokeWidth="2"
/> />

View File

@@ -14,10 +14,10 @@ class RectExample extends Component{
height="60" height="60"
> >
<Rect <Rect
x="25" x="5%"
y="5" y="5%"
width="150" width="90%"
height="50" height="90%"
fill="rgb(0,0,255)" fill="rgb(0,0,255)"
strokeWidth="3" strokeWidth="3"
stroke="rgb(0,0,0)" stroke="rgb(0,0,0)"

View File

@@ -568,8 +568,6 @@ npm install
#### Thanks: #### Thanks:
* [SVG bounding Algorithm](https://github.com/icons8/svg-path-bounding-box)
* [Circle drawing with svg arc path](http://stackoverflow.com/questions/5737975/circle-drawing-with-svgs-arc-path/10477334#10477334)
* [w3schools.com SVG Tutorial](http://www.w3schools.com/svg/) * [w3schools.com SVG Tutorial](http://www.w3schools.com/svg/)
* [SVG Tutorial](http://tutorials.jenkov.com/svg/index.html) * [SVG Tutorial](http://tutorials.jenkov.com/svg/index.html)
* [MDN](https://developer.mozilla.org/en/docs/Web/SVG) * [MDN](https://developer.mozilla.org/en/docs/Web/SVG)

View File

@@ -14,6 +14,7 @@ import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.util.Log;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
@@ -51,7 +52,18 @@ public class RNSVGShapeShadowNode extends RNSVGPathShadowNode {
// TODO: // TODO:
float cx = getActualProp("cx", width); float cx = getActualProp("cx", width);
float cy = getActualProp("cy", height); float cy = getActualProp("cy", height);
float r = getActualProp("r", width);
float r;
ReadableMap value = mShape.getMap("r");
if (value.getBoolean("percentage")) {
float percent = (float)value.getDouble("value");
float powX = (float)Math.pow((width * percent), 2);
float powY = (float)Math.pow((height*percent), 2);
r = (float)Math.sqrt(powX + powY) / (float)Math.sqrt(2);
} else {
r = (float)value.getDouble("value") * mScale;
}
mPath.addCircle(cx, cy, r, Path.Direction.CW); mPath.addCircle(cx, cy, r, Path.Direction.CW);
break; break;
} }
@@ -120,7 +132,7 @@ public class RNSVGShapeShadowNode extends RNSVGPathShadowNode {
ReadableMap value = mShape.getMap(name); ReadableMap value = mShape.getMap(name);
if (value.getBoolean("percentage")) { if (value.getBoolean("percentage")) {
return (float)value.getDouble("value") * relative * mScale; return (float)value.getDouble("value") * relative;
} else { } else {
return (float)value.getDouble("value") * mScale; return (float)value.getDouble("value") * mScale;
} }

View File

@@ -30,7 +30,20 @@
// draw circle // draw circle
CGFloat cx = [self getActualProp:@"cx" relative:width]; CGFloat cx = [self getActualProp:@"cx" relative:width];
CGFloat cy = [self getActualProp:@"cy" relative:height]; CGFloat cy = [self getActualProp:@"cy" relative:height];
CGFloat r = [self getActualProp:@"r" relative:height];
CGFloat r;
// radius in percentage calculate formula:
// radius = sqrt(pow((width*percent), 2) + pow((height*percent), 2)) / sqrt(2)
NSDictionary *prop = [self.shape objectForKey:@"r"];
CGFloat value = [[prop objectForKey:@"value"] floatValue];
if ([[prop objectForKey:@"percentage"] integerValue] == 1) {
r = sqrt(pow((width * value), 2) + pow((height * value), 2)) / sqrt(2);
} else {
r = value;
}
CGPathAddArc(path, nil, cx, cy, r, -M_PI, M_PI, YES); CGPathAddArc(path, nil, cx, cy, r, -M_PI, M_PI, YES);
break; break;
} }

View File

@@ -1,5 +1,10 @@
let percentReg = /^(\-?\d+(?:\.\d+)?)(%?)$/; let percentReg = /^(\-?\d+(?:\.\d+)?)(%?)$/;
export default function (percent) { export default function (percent) {
let matched = percent.match(percentReg); let matched = percent.match(percentReg);
if (!matched) {
console.warn(`\`${percent}\` is not a valid number or percentage string.`);
return 0;
}
return matched[2] ? matched[1] / 100 : +matched[1]; return matched[2] ? matched[1] / 100 : +matched[1];
} }