apply common stroke processor to elements

apply common stroke processor to elements
This commit is contained in:
Horcrux
2016-01-23 16:50:11 +08:00
parent 1fecd0a015
commit af0995022d
14 changed files with 108 additions and 56 deletions

View File

@@ -8,6 +8,7 @@ import * as Polyline from './examples/Polyline';
import * as Path from './examples/Path'; import * as Path from './examples/Path';
import * as Text from './examples/Text'; import * as Text from './examples/Text';
import * as G from './examples/G'; import * as G from './examples/G';
import * as Stroking from './examples/Stroking';
export { export {
Svg, Svg,
@@ -19,5 +20,6 @@ export {
Polyline, Polyline,
Path, Path,
Text, Text,
Stroking,
G G
}; };

View File

@@ -3,24 +3,58 @@ import React, {
} from 'react-native'; } from 'react-native';
import Svg, { import Svg, {
Line Path,
G
} from 'react-native-art-svg'; } from 'react-native-art-svg';
class LineExample extends Component{ class StrokeExample extends Component{
static title = 'Line'; static title = 'The stroke property defines the color of a line, text or outline of an element';
render() { render() {
return <Svg return <Svg height="80" width="225">
height="100" <G fill="none">
width="100" <Path stroke="red" d="M5 20 l215 0" />
> <Path stroke="black" d="M5 40 l215 0" />
<Line <Path stroke="blue" d="M5 60 l215 0" />
x1="0" </G>
y1="0" </Svg>;
x2="100" }
y2="100" }
stroke="red"
strokeWidth="2" class StrokeWidth extends Component{
/> static title = 'The stroke property defines the color of a line, text or outline of an element';
render() {
return <Svg height="80" width="225">
<G fill="none" stroke="black">
<Path strokeWidth="2" d="M5 20 l215 0" />
<Path strokeWidth="4" d="M5 40 l215 0" />
<Path strokeWidth="6" d="M5 60 l215 0" />
</G>
</Svg>;
}
}
class StrokeLinecap extends Component{
static title = 'The stroke-linecap property defines different types of endings to an open path';
render() {
return <Svg height="80" width="225">
<G fill="none" stroke="black" strokeWidth="6">
<Path strokeLinecap="butt" d="M5 20 l215 0" />
<Path strokeLinecap="round" d="M5 40 l215 0" />
<Path strokeLinecap="square" d="M5 60 l215 0" />
</G>
</Svg>;
}
}
class StrokeDasharray extends Component{
static title = 'The stroke-linecap property defines different types of endings to an open path';
render() {
return <Svg height="80" width="225">
<G fill="none" stroke="black" strokeWidth="4">
<Path strokeDasharray="5,5" d="M5 20 l215 0" />
<Path strokeDasharray="10,10" d="M5 40 l215 0" />
<Path strokeDasharray="20,10,5,5,5,10" d="M5 60 l215 0" />
</G>
</Svg>; </Svg>;
} }
} }
@@ -29,17 +63,14 @@ const icon = <Svg
height="20" height="20"
width="20" width="20"
> >
<Line <G fill="none" stroke="black" strokeWidth="2">
x1="0" <Path strokeDasharray="2,2" d="M0 4 h20" />
y1="0" <Path strokeDasharray="4,4" d="M0 10 h20" />
x2="20" <Path strokeDasharray="4,2,1,1,1,6" d="M0 19 h20" />
y2="20" </G>
stroke="red"
strokeWidth="1"
/>
</Svg>; </Svg>;
const samples = [LineExample]; const samples = [StrokeExample, StrokeWidth, StrokeLinecap, StrokeDasharray];
export { export {
icon, icon,

View File

@@ -85,6 +85,8 @@ const icon = <Svg
fontSize="14" fontSize="14"
fontWeight="bold" fontWeight="bold"
textAnchor="center" textAnchor="center"
fill="none"
stroke="blue"
></Text> ></Text>
</Svg>; </Svg>;

View File

@@ -107,7 +107,7 @@ const styles = StyleSheet.create({
}); });
const names = ['Svg', 'Circle', 'Ellipse', 'G', 'Text', 'Path', 'Polygon', 'Polyline', 'Line', 'Rect']; const names = ['Svg', 'Stroking', 'Path', 'Line', 'Rect', 'Polygon', 'Polyline', 'Circle', 'Ellipse', 'G', 'Text'];
class ArtSvgExample extends Component { class ArtSvgExample extends Component {
constructor() { constructor() {

View File

@@ -3,7 +3,7 @@ import React, {
PropTypes PropTypes
} from 'react-native'; } from 'react-native';
import Ellipse from './Ellipse'; import Ellipse from './Ellipse';
import strokeFilter from '../lib/strokeFilter';
let propType = PropTypes.oneOfType([PropTypes.string, PropTypes.number]); let propType = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
class Circle extends Component{ class Circle extends Component{
static displayName = 'Circle'; static displayName = 'Circle';
@@ -12,9 +12,15 @@ class Circle extends Component{
cy: propType, cy: propType,
r: propType r: propType
}; };
static defaultProps = {
cx: 0,
ct: 0
};
render() { render() {
return <Ellipse return <Ellipse
{...this.props} {...this.props}
{...strokeFilter(this.props)}
r={null} r={null}
rx={this.props.r} rx={this.props.r}
ry={this.props.r} ry={this.props.r}

View File

@@ -31,7 +31,7 @@ class Ellipse extends Component{
return <Shape return <Shape
{...props} {...props}
fill={fillFilter(props)} fill={fillFilter(props)}
stroke={strokeFilter(props)} {...strokeFilter(props)}
strokeCap={null} strokeCap={null}
strokeJoin={null} strokeJoin={null}
cx={null} cx={null}

View File

@@ -31,8 +31,7 @@ class Line extends Component{
x2={null} x2={null}
y2={null} y2={null}
fill={null} fill={null}
stroke={strokeFilter(props)} {...strokeFilter(props)}
strokeCap={props.strokeLinecap || props.strokeCap || 'square'}
d={d} d={d}
/>; />;
} }

View File

@@ -20,16 +20,16 @@ class Path extends Component{
strokeLinecap: PropTypes.oneOf(['butt', 'square', 'round']), strokeLinecap: PropTypes.oneOf(['butt', 'square', 'round']),
strokeCap: PropTypes.oneOf(['butt', 'square', 'round']), strokeCap: PropTypes.oneOf(['butt', 'square', 'round']),
strokeLinejoin: PropTypes.oneOf(['miter', 'bevel', 'round']), strokeLinejoin: PropTypes.oneOf(['miter', 'bevel', 'round']),
strokeJoin: PropTypes.oneOf(['miter', 'bevel', 'round']) strokeJoin: PropTypes.oneOf(['miter', 'bevel', 'round']),
strokeDasharray: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.number)])
}; };
render() { render() {
let {props} = this; let {props} = this;
return <Shape return <Shape
{...props} {...props}
strokeCap={props.strokeLinecap || props.strokeCap || 'square'} {...strokeFilter(props)}
strokeJoin={props.strokeLinejoin || props.strokeJoin || 'miter'}
fill={fillFilter(props)} fill={fillFilter(props)}
stroke={strokeFilter(props)}
/>; />;
} }
} }

View File

@@ -17,6 +17,8 @@ class Polygon extends Component{
let d = 'M' + props.points.trim().replace(/\s+/g, 'L') + 'z'; let d = 'M' + props.points.trim().replace(/\s+/g, 'L') + 'z';
return <Path return <Path
{...props} {...props}
{...strokeFilter(props)}
fill={fillFilter(props)}
points={null} points={null}
d={d} d={d}
/>; />;

View File

@@ -22,6 +22,8 @@ class Polyline extends Component{
let d = 'M' + props.points.trim().replace(/\s+/g, 'L'); let d = 'M' + props.points.trim().replace(/\s+/g, 'L');
return <Path return <Path
{...props} {...props}
{...strokeFilter(props)}
fill={fillFilter(props)}
points={null} points={null}
d={d} d={d}
/>; />;

View File

@@ -91,6 +91,8 @@ class Rect extends Component{
ry={null} ry={null}
width={null} width={null}
height={null} height={null}
{...strokeFilter(props)}
fill={fillFilter(props)}
d={d} d={d}
/>; />;
} }

View File

@@ -8,7 +8,7 @@ import _ from 'lodash';
let { let {
Surface, Surface,
Group Group
} = ART; } = ART;
function extractViewbox({viewbox, width, height, preserveAspectRatio}) { function extractViewbox({viewbox, width, height, preserveAspectRatio}) {
if (!viewbox || !width || !height) { if (!viewbox || !width || !height) {
@@ -77,6 +77,8 @@ class Svg extends Component{
opacity: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), opacity: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
viewbox: PropTypes.string, viewbox: PropTypes.string,
// TODO: complete other values of preserveAspectRatio // TODO: complete other values of preserveAspectRatio
// http://www.justinmccandless.com/demos/viewbox/index.html
// http://tutorials.jenkov.com/svg/svg-viewport-view-box.html
preserveAspectRatio: PropTypes.string // preserveAspectRatio only supports 'none' for now preserveAspectRatio: PropTypes.string // preserveAspectRatio only supports 'none' for now
}; };

View File

@@ -10,6 +10,7 @@ let {
import fillFilter from '../lib/fillFilter'; import fillFilter from '../lib/fillFilter';
import strokeFilter from '../lib/strokeFilter'; import strokeFilter from '../lib/strokeFilter';
import transformFilter from '../lib/transformFilter';
const fontFamily = '"Helvetica Neue", "Helvetica", Arial'; const fontFamily = '"Helvetica Neue", "Helvetica", Arial';
class Text extends Component{ class Text extends Component{
@@ -32,28 +33,18 @@ class Text extends Component{
y = props.dy ? +props.y + (+props.dy) : +props.y; y = props.dy ? +props.y + (+props.dy) : +props.y;
} }
let coords = props.origin ? props.origin.split(',') : [];
let originX = coords.length === 2 ? coords[0] : props.originX;
let originY = coords.length === 2 ? coords[1] :props.originY;
return <ARTText return <ARTText
{...props} {...props}
{...transformFilter(props)}
{...strokeFilter(props)}
fill={fillFilter(props)}
font={{ font={{
fontSize: props.fontSize || 12, fontSize: props.fontSize || 12,
fontFamily: props.fontFamily || fontFamily, fontFamily: props.fontFamily || fontFamily,
fontWeight: props.fontWeight, fontWeight: props.fontWeight,
fontStyle: props.fontStyle fontStyle: props.fontStyle
}} }}
rotation={props.rotation || props.rotate || 0}
scale={props.scale || 1}
originX={originX}
originY={originY}
strokeCap={props.strokeLinecap || props.strokeCap || 'square'}
strokeJoin={props.strokeLinejoin || props.strokeJoin || 'miter'}
alignment={props.textAnchor || props.alignment} alignment={props.textAnchor || props.alignment}
fill={fillFilter(props)}
stroke={strokeFilter(props)}
x={x} x={x}
y={y} y={y}
/>; />;

View File

@@ -1,4 +1,17 @@
import rgba from './rgba'; import rgba from './rgba';
let separator = /\s*,\s*/;
export default function (props) { export default function (props) {
return rgba(props.stroke, props.strokeOpacity); let strokeDasharray = props.strokeDash || props.strokeDasharray;
if (typeof strokeDasharray === 'string') {
strokeDasharray = strokeDasharray.split(separator).map(dash => +dash);
}
return {
stroke: rgba(props.stroke, props.strokeOpacity),
strokeCap:props.strokeLinecap || props.strokeCap || 'square',
strokeJoin:props.strokeLinejoin || props.strokeJoin || 'miter',
strokeDash:strokeDasharray
};
} }