react-native-art-繪圖入門

在React Native中ART是個很是重要的庫,它讓很是酷炫的繪圖及動畫變成了可能。可是多是知道的人真的很少致使文檔及少中文更少。不少都是把英文的參數列表翻譯過來,也沒有案例。因而決定出這樣一份入門文檔及可能遇到的坑,但願可以幫助到你們。本文的示例工程https://github.com/xu-duqing/React-Native-ART-Samplenode

<!--more-->react

添加依賴

Android默認就包含ART庫,IOS須要單獨添加依賴庫。git

  1. 右鍵點擊項目 -> ‘Add Files to ProjectName -> 選擇 node_modules/react-native/React/Libraries/ART/ART.xcodeproj’github

  2. 將 libART.a 添加到 Linked Frameworks and Librariesreact-native

基礎組件

ART暴露的組件有7個,這篇用到的有五個。先介紹即將用到的四個組件,以後在介紹另外三個。xcode

  • Surface - 一個矩形可渲染的區域,是其餘元素的容器!字體

  • Group - 可容納多個形狀、文本和其餘的分組動畫

  • Shape - 形狀定義,可填充this

  • Text - 文本形狀定義翻譯

props

  • Surface

    • width : 渲染區域的寬

    • height : 定義渲染區域的高

  • Shape

    • d : 定義繪製路徑

    • stroke : 描邊顏色

    • strokeWidth : 描邊寬度

    • strokeDash : 定義虛線

    • fill : 填充顏色

  • Text

    • funt : 字體樣式,定義字體、大小、是否加粗 如: bold 35px Heiti SC

  • Path

    • moveTo(x,y) : 移動到座標(x,y)

    • lineTo(x,y) : 連線到(x,y)

    • arc() : 繪製弧線

    • close() : 封閉空間

繪製直線

瞭解Path的moveTo和LineTo的使用,注意Surface的高度和寬度,多數沒有繪製出想要的都是由於渲染區域定義問題

示例
import React from 'react'
import {
    View,
    ART
} from 'react-native'

export default class Line extends React.Component{

    render(){

        const path = ART.Path();
        path.moveTo(1,1); //將起始點移動到(1,1) 默認(0,0)
        path.lineTo(300,1); //連線到目標點(300,1)

        return(
            <View style={this.props.style}>
                <ART.Surface width={300} height={2}>
                    <ART.Shape d={path} stroke="#000000" strokeWidth={1} />
                </ART.Surface>
            </View>
        )
    }
}

繪製虛線

瞭解strokeDash的參數,

[10,5] : 表示繪10像素實線在繪5像素空白,如此循環
[10,5,20,5] : 表示繪10像素實線在繪製5像素空白在繪20像素實線及5像素空白

示例
import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape, Path} = ART;

export default class DashLine extends React.Component{

    render(){

        const path = Path()
            .moveTo(1,1)
            .lineTo(300,1);

        return(
            <View style={this.props.style}>
                <Surface width={300} height={2}>
                    <Shape d={path} stroke="#000000" strokeWidth={2} strokeDash={[10,5]}/>
                </Surface>
            </View>
        )
    }
}

繪製矩形

瞭解close()的使用,close的意思是建立一個密閉的路徑。首先經過lineTo繪製三條邊,在使用close連接第四條邊。fill作顏色填充

示例
import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape, Path} = ART;

export default class Rect extends React.Component{

    render(){

        const path = new Path()
            .moveTo(1,1)
            .lineTo(1,99)
            .lineTo(99,99)
            .lineTo(99,1)
            .close();

        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Shape d={path} stroke="#000000" fill="#892265" strokeWidth={1} />
                </Surface>
            </View>
        )
    }
}

繪圓

瞭解arc(x,y,radius)的使用, 終點座標距離起點座標的相對距離

示例
import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape, Path} = ART;

export default class Circle extends React.Component{

    render(){

        const path = new Path()
            .moveTo(50,1)
            .arc(0,99,25)
            .arc(0,-99,25)
            .close();


        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Shape d={path} stroke="#000000" strokeWidth={1}/>
                </Surface>
            </View>
        )
    }
}

繪製文字

瞭解funt屬性的使用,規則是「粗細 字號 字體」

注意: 字體應該是支持path屬性的,應該是實現bug並無不生效。 Android經過修改源碼是能夠解決的,IOS沒看源碼。

示例
import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Text, Path} = ART;

export default class ArtText extends  React.Component{


    render(){

        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Text strokeWidth={1} stroke="#000" font="bold 35px Heiti SC" path={new Path().moveTo(40,40).lineTo(99,10)} >Swipe</Text>
                </Surface>
            </View>
        )
    }

}

繪製扇形

這裏使用的是react-art中封裝的一個組件地址,內部仍是使用arc作路徑繪製,感興趣的同窗能夠閱讀一下代碼

示例
import React from 'react'
import {
    View,
    ART
} from  'react-native'

const {Surface} = ART;
import Wedge from './Wedge'

export default class Fan extends  React.Component{

    render(){

        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Wedge
                     outerRadius={50}
                     startAngle={0}
                     endAngle={60}
                     originX={50}
                     originY={50}
                     fill="blue"/>

                </Surface>
            </View>
        )
    }
}

圖層疊加

瞭解Group的使用

示例
"use strict";

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape,Text, Path,Group} = ART;

export default class GroupTest extends React.Component{

    render(){

        const pathRect = new Path()
            .moveTo(1,1)
            .lineTo(1,99)
            .lineTo(99,99)
            .lineTo(99,1)
            .close();

        const pathCircle = new Path()
            .moveTo(50,1)
            .arc(0,99,25)
            .arc(0,-99,25)
            .close();

        const pathText = new Path()
            .moveTo(40,5)
            .lineTo(40,99);


        return(
            <View>
                <Surface width={100} height={100}>
                    <Group>
                        <Shape d={pathRect} stroke="#000000" fill="#000000" strokeWidth={1}/>
                        <Shape d={pathCircle} stroke="#FFFFFF" fill="#FFFFFF" strokeWidth={1}/>
                        <Text strokeWidth={1} strokeDash={[2,1,2,1]} stroke="#000" font="bold 30px Heiti SC" path={pathText} >Swipe</Text>
                    </Group>
                </Surface>
            </View>
        )
    }
}

做者 大光 更多文章 | Github

相關文章
相關標籤/搜索