React Native ART

React Native ART

類似于 h5 的 svg

非官方文檔哥力,栗子有一些錯(cuò)誤

React Native ART介紹

打印 ART 發(fā)現(xiàn)如下內(nèi)容:

【ART】 
Surface:? Surface()
Group:? Group()
Shape:? Shape()
ClippingRectangle:? ClippingRectangle()
Text:? Text()
Pattern:? Pattern(url, width, height, left, top)
Path:? (a, b, c, d, e, f, g, h)
Transform:? (a, b, c, d, e, f, g, h)
LinearGradient:? LinearGradient(stops, x1, y1, x2, y2)
RadialGradient:? RadialGradient(stops, fx, fy, rx, ry, cx, cy)

【ART.Path】
constructor: ? (a, b, c, d, e, f, g, h)
initialize: ? initialize(path)
reset: ? reset()
close: ? close()
line: ? line(x, y)
lineTo: ? lineTo(x, y)
move: ? move(x, y)
moveTo: ? moveTo(x, y)
arc: ? arc(x, y, rx, ry, outer, counterClockwise, rotation)
arcTo: ? arcTo(x, y, rx, ry, outer, counterClockwise, rotation)
counterArc: ? counterArc(x, y, rx, ry, outer)
counterArcTo: ? counterArcTo(x, y, rx, ry, outer)
curve: ? curve(c1x, c1y, c2x, c2y, ex, ey)
curveTo: ? curveTo(c1x, c1y, c2x, c2y, ex, ey)
onArc: ? onArc(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation)
onBezierCurve: ? onBezierCurve(sx, sy, p1x, p1y, p2x, p2y, x, y)
onClose: ? onClose()
onLine: ? onLine(sx, sy, x, y)
onMove: ? onMove(sx, sy, x, y)
onReset: ? onReset()
push: ? push()
toJSON: ? toJSON()
_arcToBezier: ? onArc(sx, sy, ex, ey, cx, cy, rx, ry, sa, ea, ccw, rotation)

【ART.Transform 】
constructor: ? (a, b, c, d, e, f, g, h)
initialize: ? Transform(xx, yx, xy, yy, x, y)
inversePoint: ? inversePoint(x, y)
move: ? move(x, y)
moveTo: ? moveTo(x, y)
point: ? point(x, y)
resizeTo: ? resizeTo(width, height)
rotate: ? rotate(deg, x, y)
rotateTo: ? rotateTo(deg, x, y)
scale: ? scale(x, y)
scaleTo: ? scaleTo(x, y)
transform: ? transform(xx, yx, xy, yy, x, y)
transformTo: ? Transform(xx, yx, xy, yy, x, y)
translate: ? translate(x, y)

Surface

ART內(nèi)容的父層啥寇,其中不能包含非ART標(biāo)簽(否則直接閃退…)扒接,需要指定寬高。

Group

分組叶圃,類似于 View,可指定內(nèi)容在畫布繪制的起點(diǎn)。

Shape

ART 繪制的核心建钥,d屬性指定繪制路徑

Path

line: ? line(x, y)
lineTo: ? lineTo(x, y)
move: ? move(x, y)
moveTo: ? moveTo(x, y)
arc: ? arc(x, y, rx, ry, outer, counterClockwise, rotation)
arcTo: ? arcTo(x, y, rx, ry, outer, counterClockwise, rotation)
counterArc: ? counterArc(x, y, rx, ry, outer)
counterArcTo: ? counterArcTo(x, y, rx, ry, outer)
curve: ? curve(c1x, c1y, c2x, c2y, ex, ey)
curveTo: ? curveTo(c1x, c1y, c2x, c2y, ex, ey)
  • reset 重置當(dāng)前的路徑,類似于 canvas 的 beginPath()
  • close 用線段閉合當(dāng)前點(diǎn)到當(dāng)前子段的第一個(gè)點(diǎn)(之前如果線不連續(xù)則最開始的一段不在閉合區(qū)域內(nèi))
  • move/moveTo 從一點(diǎn)移動(dòng)到另一點(diǎn)
  • line/lineTo 從一點(diǎn)畫線到另一點(diǎn)
  • arc/arcTo 從一點(diǎn)通過順時(shí)針畫曲線到另一點(diǎn)
  • counterArc/counterArcTo 從一點(diǎn)通過逆時(shí)針畫曲線到另一點(diǎn)
  • 帶to的都是絕對(duì)位置虐沥,不帶to的都是相對(duì)位置熊经。

LinearGradient

LinearGradient(stops, x1, y1, x2, y2)
stops 停的位置與顏色,(x1,y1) 起點(diǎn) (x2,y2) 終點(diǎn)

RadialGradient

RadialGradient(stops, fx, fy, rx, ry, cx, cy)

噗哈哈哈欲险,徑向漸變目前有問題啊...

一片黑...

都是一片黑...

Transform

move(deltaX[,deltaY])
moveTo(x[,y])
scale(scaleX[,scaleY])
rotate(deg[,transformOriginX,transformOriginY])
rotateTo(deg[,transformOriginX,transformOriginY])
transform(scaleX, skewX, skewY, scaleY, translateX, translateY)

ClippingRectangle

截取一塊區(qū)域用來繪圖镐依。

【實(shí)例】

import React, { Component } from 'react';

import {
    ScrollView,
    StyleSheet,
    ART
} from 'react-native';

export default class App extends Component {
    constructor(props) {
        super(props)
        this.state = {

        }
    }

    render() {

        const path = new ART.Path();
        path.moveTo(10, 10);
        path.lineTo(20, 10);
        path.reset(); // Reset the current path. Just like beginPath in canvasRenderingContext2d.
        path.moveTo(50, 50);
        path.lineTo(50, 100);
        path.lineTo(100, 100);
        path.close();

        return (
            <ScrollView style={styles.container}>
                <ART.Surface width={400} height={1500} visible={false} style={{ backgroundColor: '#eee' }}>
                    {/* 第一組 Shape 線段 moveTo lineTo*/}
                    <ART.Group x={10} y={10}>
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={5} // 描邊的寬度
                            fill="#0000ff" // 填充的顏色
                            // strokeDash={[10, 10, 20, 20, 30, 30]} // 長(zhǎng)度 line-gap-line-gap-... repeat
                            strokeCap="round" // cap style of path end.  oneOf(["butt", "round"(default), "square"])
                            strokeJoin="bevel" // path join point style.  oneOf(["miter", "round"(default), "bevel"])
                            d={new ART.Path().moveTo(10, 10).move(10, 10).lineTo(40, 10).moveTo(40, 40).lineTo(40, 80).lineTo(80, 80).close()} //
                        />
                    </ART.Group>
                    {/* 第二組 Text 文字 */}
                    <ART.Group x={100} y={10}>
                        <ART.Text
                            strokeWidth={5}  // 描邊寬度
                            stroke="#00ff00" // 描邊顏色
                            fill="#000000" // 文字顏色
                            alignment="center"
                            font={{
                                fontFamily: 'Helvetica, Neue Helvetica, Arial',
                                fontSize: 23,
                                fontWeight: "bold",
                                fontStyle: "italic",
                            }}
                        // transform={ART.Transform().translate(150, 150).rotate(30, 100, 100).scale(3, 2)}
                        >zdy
                        </ART.Text>
                    </ART.Group>
                    {/* 第三組 Shape 畫圓 arcTo */}
                    <ART.Group x={0} y={100} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            d={new ART.Path().moveTo(50, 50).arcTo(150, 50, 50).arcTo(50, 50, 50)} // 從一個(gè)點(diǎn)(50,50) 通過畫圓(半徑50)的方式到達(dá)另一個(gè)點(diǎn) (150,50) 再通過畫圓(半徑50)的方式到達(dá)起點(diǎn)(50,50)完成畫圓
                        />
                    </ART.Group>
                    {/* 第四組 Shape 順時(shí)針弧線 畫扇形 arcTo */}
                    <ART.Group x={0} y={200} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            fill="#00ff00"
                            d={new ART.Path().moveTo(50, 50).lineTo(50, 0).arcTo(100, 50, 50).lineTo(50, 50)} // (50, 50)->線段(50,0)->弧線(100, 50, 50)->線段(50, 50)
                        />
                    </ART.Group>
                    {/* 第五組 Shape 逆時(shí)針弧線 counterArcTo */}
                    <ART.Group x={100} y={200} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            fill="#00ff00"
                            d={new ART.Path().moveTo(50, 50).lineTo(50, 0).counterArcTo(100, 50, 50).lineTo(50, 50)} // (50, 50)->線段(50,0)->弧線(100, 50, 50)->線段(50, 50)
                        />
                    </ART.Group>
                    {/* 第六組 Shape 曲線 curve */}
                    <ART.Group x={0} y={300} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            fill="#00ff00"
                            // If now we are at (10, 10), it draw a cubic bezier curve from (10, 10) to (22, 42) and use (10, 20) as first control point and (30, 40) the second one
                            d={new ART.Path().moveTo(10, 10).curve(10, 20, 30, 40, 12, 32)}
                        />
                    </ART.Group>
                    {/* 第七組 Shape 曲線 curveTo */}
                    <ART.Group x={100} y={300} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            fill="#00ff00"
                            // if now we are at (10, 10), it draw a cubic bezier curve from (10, 10) to (12, 32) and use (10, 20) as first control point and (30, 40) the second one
                            d={new ART.Path().moveTo(10, 10).curveTo(10, 20, 30, 40, 12, 32)}
                        />
                    </ART.Group>
                    {/* 第八組 Shape reset */}
                    <ART.Group x={200} y={300} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            fill="#00ff00"
                            d={path}
                        />
                    </ART.Group>
                    {/* 第九組 Shape reset */}
                    <ART.Group x={0} y={400} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            d={new ART.Path().moveTo(100, 0).lineTo(300, 0).lineTo(300, 100).lineTo(100, 100).close()}
                            fill={
                                new ART.LinearGradient({
                                    "0": "#2ba",
                                    ".5": "#f90",
                                    "0.7": "#aa4422",
                                    "1": "rgba(255,255,255,0.5)"
                                }, 100, 50, 300, 50)
                            }
                        />
                    </ART.Group>
                    {/* 第十組 Shape reset */}
                    <ART.Group x={0} y={500} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            // d={new ART.Path().moveTo(100, 50).arcTo(200, 50, 50).arcTo(100, 50, 50)}
                            d={new ART.Path().moveTo(100, 0).lineTo(300, 0).lineTo(300, 100).lineTo(100, 100).close()}
                            fill={
                                new ART.RadialGradient({
                                    "0": "#2ba",
                                    "1": "#f90",
                                }, 300, 200, 400, 400, 200, 200)
                            }
                        />
                    </ART.Group>
                    {/* 第十一組 Shape Pattern 失敗了 */}
                    {/* <ART.Group x={0} y={600} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            // d={new ART.Path().moveTo(100, 50).arcTo(200, 50, 50).arcTo(100, 50, 50)}
                            d={new ART.Path().moveTo(100, 0).lineTo(300, 0).lineTo(300, 100).lineTo(100, 100).close()}
                            transform={new ART.Transform().scaleTo(0.5, 0.5).move(50, 0).rotate(90, 100, 0).transform(2, 0, 1, 1, 0, 0)}
                        />
                    </ART.Group> */}
                    {/* 第十二組 Shape ClippingRectangle */}
                    <ART.Group x={0} y={1000} >
                        <ART.ClippingRectangle
                            width={20} // 寬度
                            height={20} // 高度
                            x={0} // 橫向起始點(diǎn)
                            y={0} // 縱向起始點(diǎn)
                        >
                            <ART.Shape d={new ART.Path().moveTo(0, 0).lineTo(200, 200)} stroke="black" strokeWidth={10} />
                        </ART.ClippingRectangle>
                        <ART.ClippingRectangle
                            width={20}
                            height={20}
                            x={100}
                            y={100}
                        >
                            <ART.Shape d={new ART.Path().moveTo(0, 0).lineTo(200, 200)} stroke="black" strokeWidth={10} />
                        </ART.ClippingRectangle>
                    </ART.Group>
                    {/* 第十三組 Shape svg 值 */}
                    <ART.Group x={0} y={1200} >
                        <ART.Shape
                            stroke="#ff0000" // 描邊的顏色
                            strokeWidth={2} // 描邊的寬度
                            // d={new ART.Path().moveTo(100, 50).arcTo(200, 50, 50).arcTo(100, 50, 50)}
                            d={"M123,.16c36.58-.71,70.44,1,102,6,13.42,2.14,28.67-4.46,32,7,2.61,9-13.92,32.74-19,36l-12,3C209.92,67,186.19,143,180,171.16c-3.19,14.43-9.42,36.06-2,48,44.27,9.9,76-40,101-39l1,2c2.34,29.21-109.13,89.76-144,79-27.46-69.43,43.56-162.42,63-211v-1c-59.61-2.63-140.8-15.75-174,21-.38,28.16,17.27,27,31,40,1.35,44.31-40.85,41.54-53,9-16.52-44.23,40-94.3,67-106C85.79,6.3,105.21,7.17,123,.16Z"}
                        />
                    </ART.Group>
                </ART.Surface>
            </ScrollView>
        );
    }
    componentDidMount() {
        // console.log(ART)
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
    }

});
效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市天试,隨后出現(xiàn)的幾起案子槐壳,更是在濱河造成了極大的恐慌,老刑警劉巖喜每,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異婿斥,居然都是意外死亡智末,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門来农,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人崇堰,你說我怎么就攤上這事沃于。” “怎么了海诲?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵繁莹,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我特幔,道長(zhǎng)咨演,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任蚯斯,我火速辦了婚禮薄风,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拍嵌。我一直安慰自己遭赂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布横辆。 她就那樣靜靜地躺著撇他,像睡著了一般。 火紅的嫁衣襯著肌膚如雪狈蚤。 梳的紋絲不亂的頭發(fā)上困肩,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音脆侮,去河邊找鬼锌畸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛靖避,可吹牛的內(nèi)容都是我干的潭枣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼筋蓖,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼卸耘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起粘咖,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蚣抗,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翰铡,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡钝域,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锭魔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片例证。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖迷捧,靈堂內(nèi)的尸體忽然破棺而出织咧,到底是詐尸還是另有隱情,我是刑警寧澤漠秋,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布笙蒙,位于F島的核電站,受9級(jí)特大地震影響庆锦,放射性物質(zhì)發(fā)生泄漏捅位。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一搂抒、第九天 我趴在偏房一處隱蔽的房頂上張望艇搀。 院中可真熱鬧,春花似錦求晶、人聲如沸焰雕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽淀散。三九已至,卻和暖如春蚜锨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慢蜓。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工亚再, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人晨抡。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓氛悬,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親耘柱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子如捅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355