1.0.12 • Published 7 years ago

react-native-sf-sprite v1.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

react-native-sf-sprite

精灵控件,支持移动、放大、旋转、渐变动画,可以拖动

绘制图层,支持绘制线段、连续线段实现画笔功能

show

安装

  • npm install react-native-sf-sprite

IOS

Add the ART.xcodeproj (found in node_modules/react-native/Libraries/ART) to the Libraries group and add libART.a to Link Binary With Libraries under Build Phases.

Props

SFSprite

parametertyperequireddescriptiondefault
tagnumberno标签0
sourceuri/stringyes图片资源
widthnumberyes图片宽度
heightnumberyes图高度
visiblebooleanno是否显示true
canDragbooleanno是否可以拖拽移动false
rotatenumberno旋转度(0-360)0
opacitynumberno透明度(0-1)1
xnumberno坐标x0
ynumberno坐标y0
anchorPointXnumberno锚点x(0-1)0.5
anchorPointYnumberno锚点y(0-1)0.5
scaleXnumberno放大倍数x1
scaleYnumberno放大倍数y1
resizeModelstringno图片显示模式'cover', 'contain', 'stretch', 'repeat', 'center''stretch'
showHitBoxboolno是否显示精灵碰撞框false
showHitBoxColorstringno碰撞框颜色'white'
onPressfuncno点击事件(tag)null
onPressInfuncno按下事件(tag)null
onPressOutfuncno抬起并不在精灵范围内事件(tag)null
onMoveBeginfuncno开始移动事件({touchX,touchY,tag})null
onMovefuncno移动事件({touchX,touchY,tag)null
onMoveEndfuncno结束移动事件({touchX,touchY,tag)null

SFDrawLayer

parametertyperequireddescriptiondefault
xnumberno画布x0
ynumberno画布y0
widthnumberno画布宽度屏幕宽度
heightnumberno画布高度屏幕高度

Methods

SFSprite

MethodsParamsParam TypesdescriptionExample
reset重置精灵
setEnablevalueboolfalse:不可触发事件
setVisiblevaluebool设置隐藏
setPos{x:,y:}dic设置位置
setPosX{x:}dic设置位置x
setPosY{y:}dic设置位置y
setScale{x:,y:}设置放大缩小
setScaleX{x:}dic设置放大缩小x
setScaleY{y:}dic设置放大缩小y
setRotate{angle:}dic设置旋转角度(0-360)
setOpacity{opacity:}dic设置透明度(0-1)
setAnchorPoint{x:,y:}dic设置锚点(0-1)
setAnchorPointX{x:}dic设置锚点x
setAnchorPointY{y:}dic设置锚点y
getVisible获取是否显示
getSize获取精灵宽高
getPos获取精灵位置
getPosX获取位置x
getPosY获取位置y
getPosRealX获取实际位置x(由于锚点不一样,实际位置与显示位置不一样)
getPosRealY获取实际位置y(由于锚点不一样,实际位置与显示位置不一样)
getPosReal获取实际位置(由于锚点不一样,实际位置与显示位置不一样)
getScale获取放大缩小
getScaleX获取缩放x
getScaleY获取缩放y
getRotate获取角度
getOpacity获取透明度
getAnchorPoint获取锚点
getAnchorPointX获取锚点x
getAnchorPointY获取锚点y
setHitLinespointAry,rarray,number设置碰撞线段,碰撞半径。设置后碰撞后不可以超过线段

SFSprite-Animated (所有动画都需要调用run相应的函数去执行才能生效)

MethodsParamsParam TypesdescriptionExample
moveTo{x:x, y:y, dur, easing:easing}移动到指定位置
moveToX{x:x, dur:dur, easing:easing}移动到指定位置x
moveToY{y:y, dur:dur, easing:easing}移动到指定位置y
rotateTo{angle:angle, dur:dur, easing:easing}旋转
fadeIn{dur:dur, easing:easing}淡入
fadeOut{dur:dur, easing:easing}淡出
scalexTo{scaleX:scaleX, dur:dur, from:from, easing:easing}缩放到
scaleyTo{scaleY:scaleY, dur:dur, from:from, easing:easing}缩放到
scaleTo{scaleX:scaleX, scaleY:scaleY, dur:dur, from:from, easing:easing}缩放到
parallelaniArrayarray组合平行动画
sequenceaniArrayarray组合顺序动画
runani, finishobj,func执行单一动画
runParallelaniAry, finish同时执行多个动画
runSequenceaniAry, finish, loop = 1顺序执行多个动画,可重复
stop停止动画
runRotateLoopdur, finish, loop = 1number,func,number执行无限旋转动画

SFDrawLayer

MethodsParamsParam TypesdescriptionExample
drawLinep1, p2, color, size{x:,y:},{x:,y:},string,number画线段
drawPathBegincolor, sizestring,number绘制连续线段起始,每次重新绘制都需要调用
drawPathp{x:,y:}绘制连续线段
getLineKBp1,p2{x:,y:},{x:,y:}获取线性函数k,b系数
getPointByLineAndCirclep1, p2, center, r{x:,y:},{x:,y:},{x:,y:},number获取圆与直线的2个交点
setVisiblevaluebool设置是否显示
setLineVisiblevaluebool设置线段是否显示
setPathVisiblevaluebool设置连续线段是否显示

例子

import React, {Component} from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    TouchableWithoutFeedback,
    ScrollView,
    findNodeHandle,
    Dimensions,
    Image
} from 'react-native';
import {SFSprite,SFDrawLayer} from "react-native-sf-sprite"
var width = Dimensions.get('window').width;
var height = Dimensions.get('window').height;

# 动画例子
var obj = this.sp;
var dur = 300;
obj.runSequence([//顺序执行动画
        obj.moveTo({x:width/2-200,y:100,dur:dur}),
        obj.moveTo({x:width/2-100,y:100,dur:dur}),
    ],(tag,finished)=>{//顺序执行结束回调
        obj.runParallel([//同时执行动画
            obj.moveTo({x:width/2,y:50,dur:dur}),
            obj.scaleTo({scaleX:0.5,scaleY:0.5,dur:dur})
        ],(tag,finished)=>{同时执行动画回调
            obj.setScale({x:0.5,y:0.5})
            //旋转动画
            obj.runRotateLoop(dur,()=>{
                obj.run(obj.scaleTo({scaleX:1,scaleY:1,dur:dur}),()=>{
                    obj.setScale({x:1.5,y:1.5});
                    obj.run(obj.moveTo({x:width/2,y:height/2,dur:dur}),()=>{
                        obj.runRotateLoop(300,()=>{},0)
                    })

                })

            },2);
        })

    },1)

//精灵绘制
<SFSprite
    ref={(ref)=> {this.sp = ref}}
    width={100}
    height={100}
    x={width/2}
    y={height/2}
    scaleX={1}
    scaleY={1}
    showHitBox={true}
    canDrag={true}
    onMove={this.onMove}
    onMoveBegin={this.onMoveBegin}
    anchorPointX={0.5}
    anchorPointY={0.5}
    onPress={()=>console.log('onPress')}
    onPressIn={()=>console.log('onPressIn')}
    onPressOut={()=>console.log('onPressOut')}
    source={require('./1.png')}/>


# 绘制例子

<SFDrawLayer ref={(ref)=>{this.draw = ref}}/>

//绘制线段
this.draw.drawLine({x:0,y:0},{x:width,y:height},'black',5);

//绘制连续线段
//在开始移动函数里调用
onMoveBegin = (data) => {
    this.draw.drawPathBegin('white',10);
}
//在移动函数里调用
onMove = (data) => {
    if (this.sp.checkLineIn({px1:0,py1:0,px2:width,py2:100})){//碰撞检测
        console.log('碰到线段')
    }
    //绘制连续线段
    this.draw.drawPath({x:data.touchX,y:data.touchY})
}
1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago