forked from madsleejensen/react-native-image-sequence
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (81 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React from 'react';
import PropTypes from 'prop-types';
import { View, requireNativeComponent, DeviceEventEmitter } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource'
class ImageSequence extends React.Component {
componentWillMount() {
DeviceEventEmitter.addListener('onLoadStart', (event: Event) => {
if (this.props.onLoadStart) {
this.props.onLoadStart(event)
}
})
DeviceEventEmitter.addListener('onLoadComplete', (event: Event) => {
if (this.props.onLoadComplete) {
this.props.onLoadComplete(event)
}
})
DeviceEventEmitter.addListener('onError', (event: Event) => {
if (this.props.onError) {
this.props.onError(event)
}
})
}
render() {
let normalized = this.props.images.map(resolveAssetSource)
// reorder elements if start-index is different from 0 (beginning)
if (this.props.startFrameIndex !== 0) {
normalized = [...normalized.slice(this.props.startFrameIndex), ...normalized.slice(0, this.props.startFrameIndex)]
}
let props = this.props
if (props.size.width) {
props.size.width = Math.round(props.size.width)
}
if (props.size.height) {
props.size.height = Math.round(props.size.height)
}
return (
<RCTImageSequence
{...this.props}
images={normalized} />
)
}
}
ImageSequence.propTypes = {
startFrameIndex: PropTypes.number,
images: PropTypes.array.isRequired,
sampleSize: PropTypes.number,
framesPerSecond: PropTypes.number,
size: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
}),
autoStart: PropTypes.bool,
oneShot: PropTypes.bool,
}
ImageSequence.defaultProps = {
startFrameIndex: 0,
sampleSize: 1,
framesPerSecond: 24,
autoStart: true,
oneShot: false
}
const RCTImageSequence = requireNativeComponent('RCTImageSequence', {
propTypes: {
...View.propTypes,
images: PropTypes.arrayOf(PropTypes.shape({
uri: PropTypes.string.isRequired
})).isRequired,
sampleSize: PropTypes.number,
framesPerSecond: PropTypes.number,
size: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
}),
autoStart: PropTypes.bool,
oneShot: PropTypes.bool,
onLoadStart: PropTypes.func,
onLoadComplete: PropTypes.func,
onError: PropTypes.func
},
})
export default ImageSequence