remove useless double stops

This commit is contained in:
Horcrux
2016-04-21 23:06:08 +08:00
parent 67e66aa4de
commit 936a10ddb8
8 changed files with 60 additions and 62 deletions

View File

@@ -9,7 +9,8 @@ import Svg, {
Stop, Stop,
Ellipse, Ellipse,
Circle, Circle,
Text Text,
Rect
} from 'react-native-art-svg'; } from 'react-native-art-svg';
class LinearGradientHorizontal extends Component{ class LinearGradientHorizontal extends Component{
@@ -87,6 +88,11 @@ class RadialGradientExample extends Component{
stopColor="#000" stopColor="#000"
stopOpacity="1" stopOpacity="1"
/> />
<Stop
offset="0.7"
stopColor="#0f0"
stopOpacity="1"
/>
<Stop <Stop
offset="1" offset="1"
stopColor="#83a" stopColor="#83a"
@@ -178,6 +184,32 @@ class FillGradientWithOpacity extends Component{
} }
} }
class FillGradientInRect extends Component{
static title = 'Fill a radial gradient inside a rect';
render() {
return <Svg
height="150"
width="300"
>
<Defs>
<RadialGradient id="grad" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<Stop
offset="0%"
stopColor="#fff"
stopOpacity="1"
/>
<Stop
offset="100%"
stopColor="#00f"
stopOpacity="1"
/>
</RadialGradient>
</Defs>
<Rect x="0" y="0" width="300" height="150" fill="url(#grad)" />
</Svg>;
}
}
const icon = <Svg const icon = <Svg
height="20" height="20"
width="20" width="20"
@@ -199,7 +231,8 @@ const samples = [
RadialGradientExample, RadialGradientExample,
RadialGradientPercent, RadialGradientPercent,
RadialGradientPart, RadialGradientPart,
FillGradientWithOpacity FillGradientWithOpacity,
FillGradientInRect
]; ];
export { export {

View File

@@ -1,40 +1,23 @@
### react-native-art-svg ### react-native-svg
------------------------ ------------------------
`react-native-art-svg` is built to provide a SVG interface to react native on both iOS and Android which is based on *ART* `react-native-svg` is built to provide a SVG interface to react native on both iOS and Android
#### Features #### Features
1. Supports most of SVG elements and properties(Rect, Circle, Line, Polyline, Polygon, G ...). 1. Supports most of SVG elements and properties(Rect, Circle, Line, Polyline, Polygon, G ...).
2. 100% based on ReactNative`s ART library 2. Easy to convert SVG code into ReactNative code.
3. Easy to convert SVG code into ReactNative code.
![example](./screenShoots/art-svg.gif)
#### Install #### Install
`npm install react-native-art-svg` ```
npm install react-native-svg --save
rnpm link react-native-svg
```
##### On iOS we should add import `ART` library into your project
To add ART.xcodeproj find the file located in react-native/Libraries/ART/ART.xcodeproj and just drag it over to the Libraries section in XCode. That should look like so
![Add ART.xcodeproj to Libraries](./screenShoots/addXcodeproj.png)
Next well link the binary.
With the root project selected on the left, select `Build Phases` from the center view. There will be a section called “Link Binary With Libraries”, expand it, press the + and select `libART.a`
Like so
![Link binary](./screenShoots/linkBinary.png)
([Getting react art running on react native](http://browniefed.com/blog/2015/05/03/getting-react-art-running-on-react-native/))
##### On android
react-native\`s `ART` for android is shipped within react-native@0.18.0
#### Usage #### Usage
@@ -57,7 +40,7 @@ import Svg,{
Use, Use,
Defs, Defs,
Stop Stop
} from 'react-native-art-svg'; } from 'react-native-svg';
class SvgExample extends Component { class SvgExample extends Component {
render() { render() {

View File

@@ -204,13 +204,7 @@ public class RNSVGPathShadowNode extends RNSVGVirtualNode {
private static void parseGradientStops(float[] value, int stopsCount, float[] stops, int[] stopsColors, int startColorsPosition) { private static void parseGradientStops(float[] value, int stopsCount, float[] stops, int[] stopsColors, int startColorsPosition) {
int startStops = value.length - stopsCount; int startStops = value.length - stopsCount;
for (int i = 0; i < stopsCount; i++) { for (int i = 0; i < stopsCount; i++) {
int half = stopsCount / 2; stops[i] = value[startStops + i];
// stops keep in a order like this
// 0 1 2 3 4 9 8 7 6 5
stops[i] = value[startStops + (i < half ? i : (half + (stopsCount - i) - 1))];
stopsColors[i] = Color.argb( stopsColors[i] = Color.argb(
(int) (value[startColorsPosition + i * 4 + 3] * 255), (int) (value[startColorsPosition + i * 4 + 3] * 255),
(int) (value[startColorsPosition + i * 4] * 255), (int) (value[startColorsPosition + i * 4] * 255),

View File

@@ -8,7 +8,7 @@ import stopsOpacity from '../lib/stopsOpacity';
import numberProp from '../lib/numberProp'; import numberProp from '../lib/numberProp';
import Gradient from './Gradient'; import Gradient from './Gradient';
import {LINEAR_GRADIENT} from '../lib/extract/extractBrush'; import {LINEAR_GRADIENT} from '../lib/extract/extractBrush';
import {insertColorStopsIntoArray} from '../lib/insertProcessor'; import insertColorStopsIntoArray from '../lib/insertProcessor';
function LinearGradientGenerator(stops, x1, y1, x2, y2) { function LinearGradientGenerator(stops, x1, y1, x2, y2) {
var type = LINEAR_GRADIENT; var type = LINEAR_GRADIENT;

View File

@@ -7,7 +7,7 @@ import stopsOpacity from '../lib/stopsOpacity';
import numberProp from '../lib/numberProp'; import numberProp from '../lib/numberProp';
import Gradient from './Gradient'; import Gradient from './Gradient';
import {RADIAL_GRADIENT} from '../lib/extract/extractBrush'; import {RADIAL_GRADIENT} from '../lib/extract/extractBrush';
import {insertDoubleColorStopsIntoArray} from '../lib/insertProcessor'; import insertColorStopsIntoArray from '../lib/insertProcessor';
function RadialGradientGenerator(stops, fx, fy, rx, ry, cx, cy) { function RadialGradientGenerator(stops, fx, fy, rx, ry, cx, cy) {
@@ -33,7 +33,7 @@ function RadialGradientGenerator(stops, fx, fy, rx, ry, cx, cy) {
// color stops. Ideally this API would become more restrictive so that this // color stops. Ideally this API would become more restrictive so that this
// extra work isn't needed. // extra work isn't needed.
var brushData = [RADIAL_GRADIENT, +fx, +fy, +rx * 2, +ry * 2, +cx, +cy]; var brushData = [RADIAL_GRADIENT, +fx, +fy, +rx * 2, +ry * 2, +cx, +cy];
insertDoubleColorStopsIntoArray(stops, brushData, 7); insertColorStopsIntoArray(stops, brushData, 7, 0.5);
this._brush = brushData; this._brush = brushData;
} }

View File

@@ -9,7 +9,7 @@ function insertColorIntoArray(color, targetArray, atIndex) {
targetArray[atIndex + 3] = c[3]; targetArray[atIndex + 3] = c[3];
} }
function insertColorsIntoArray(stops, targetArray, atIndex, reverse) { function insertColorsIntoArray(stops, targetArray, atIndex) {
let i = 0; let i = 0;
if ('length' in stops) { if ('length' in stops) {
@@ -19,9 +19,6 @@ function insertColorsIntoArray(stops, targetArray, atIndex, reverse) {
} }
} else { } else {
let sorted = _.sortBy(_.map(stops, (value, key) => ({key, value})), 'key'); let sorted = _.sortBy(_.map(stops, (value, key) => ({key, value})), 'key');
if (reverse) {
sorted.reverse();
}
sorted.forEach(({value:stop}) => { sorted.forEach(({value:stop}) => {
insertColorIntoArray(stop, targetArray, atIndex + i * 4); insertColorIntoArray(stop, targetArray, atIndex + i * 4);
@@ -31,25 +28,25 @@ function insertColorsIntoArray(stops, targetArray, atIndex, reverse) {
return atIndex + i * 4; return atIndex + i * 4;
} }
function insertColorStopsIntoArray(stops, targetArray, atIndex) { function insertColorStopsIntoArray(stops, targetArray, atIndex, multi = 1) {
let lastIndex = insertColorsIntoArray(stops, targetArray, atIndex, false); let lastIndex = insertColorsIntoArray(stops, targetArray, atIndex, false);
insertOffsetsIntoArray(stops, targetArray, lastIndex, 1, false); insertOffsetsIntoArray(stops, targetArray, lastIndex, multi, false);
} }
function insertOffsetsIntoArray(stops, targetArray, atIndex, multi, reverse) { function insertOffsetsIntoArray(stops, targetArray, atIndex, multi) {
let offsetNumber; let offsetNumber;
let i = 0; let i = 0;
let arr = []; let arr = [];
if ('length' in stops) { if ('length' in stops) {
while (i < stops.length) { while (i < stops.length) {
offsetNumber = i / (stops.length - 1) * multi; offsetNumber = i / (stops.length - 1) * multi;
targetArray[atIndex + i] = reverse ? 1 - offsetNumber : offsetNumber; targetArray[atIndex + i] = offsetNumber;
i++; i++;
} }
} else { } else {
_.forEach(stops, (stop, offsetString) => { _.forEach(stops, (stop, offsetString) => {
offsetNumber = (+offsetString) * multi; offsetNumber = (+offsetString) * multi;
arr.push(reverse ? 1 - offsetNumber : offsetNumber); arr.push(offsetNumber);
i++; i++;
}); });
arr.sort(); arr.sort();
@@ -58,14 +55,5 @@ function insertOffsetsIntoArray(stops, targetArray, atIndex, multi, reverse) {
return atIndex + i; return atIndex + i;
} }
function insertDoubleColorStopsIntoArray(stops, targetArray, atIndex) {
let lastIndex = insertColorsIntoArray(stops, targetArray, atIndex, false);
lastIndex = insertColorsIntoArray(stops, targetArray, lastIndex, true);
lastIndex = insertOffsetsIntoArray(stops, targetArray, lastIndex, 0.5, false);
insertOffsetsIntoArray(stops, targetArray, lastIndex, 0.5, true);
}
export { export default insertColorStopsIntoArray;
insertDoubleColorStopsIntoArray,
insertColorStopsIntoArray
}

View File

@@ -1,7 +1,7 @@
{ {
"version": "1.1.1", "version": "1.0.0",
"name": "react-native-art-svg", "name": "react-native-svg",
"description": "react native svg library based on `ART`", "description": "react native svg library",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/magicismight/react-native-art-svg" "url": "https://github.com/magicismight/react-native-art-svg"