Repository: sahlhoff/react-native-pulse Branch: master Commit: 3446363ff20e Files: 5 Total size: 8.0 KB Directory structure: gitextract_gt1y39o7/ ├── .gitignore ├── LICENSE ├── README.md ├── package.json └── pulse.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .DS_Store ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2017 Chad Sahlhoff Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # React Native Pulse Animation ![react native pulse](https://raw.githubusercontent.com/sahlhoff/react-native-pulse/master/pulse-gif.gif) ## Installation ``` npm install react-native-pulse --save ``` ## Usage ```js const Pulse = require('react-native-pulse').default; class helloWorld extends Component { render() { return ( ); } } ``` ## Props Component accepts several self-descriptive properties: - **`color`** _(String)_ - Backgroundcolor for pulse. React-native colors supported. Default color is blue. - **`diameter`** _(Number)_ - This is the maximum diameter that a pulse will be. Defaults to 400. - **`duration`** _(Number)_ - Duration in milliseconds this is the delay new pulses will be created. Defaults to 1000. - **`image`** _(Object)_ - Image for center pulse thumbnail. - **`initialDiameter`** _(Number)_ - The diameter new pulses will start with. Defaults to 0. - **`numPulses`** _(Number)_ - This is the number of pulses that will be rendered. Defaults to 3. - **`pulseStyle`** _(Object)_ - Style properties for pulses (borderColor eg.) - **`speed`** _(Number)_ - Speed in milliseconds pulse will redraw. Defaults to 10. - **`style`** _(Object)_ - Style properties for pulse container (positioning eg.) ================================================ FILE: package.json ================================================ { "name": "react-native-pulse", "version": "1.0.7", "description": "React Native Pulse Animation", "main": "pulse.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/sahlhoff/react-native-pulse.git" }, "keywords": [ "react", "react-component", "ios", "react-native", "animation", "ui", "pulse" ], "author": "Chad Sahlhoff", "license": "ISC", "bugs": { "url": "https://github.com/sahlhoff/react-native-pulse/issues" }, "homepage": "https://github.com/sahlhoff/react-native-pulse#readme" } ================================================ FILE: pulse.js ================================================ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { View, Text, Image, StyleSheet, } from 'react-native'; const styles = StyleSheet.create({ container: { position: 'absolute', left: 0, right: 0, alignItems: 'center' }, pulse: { position: 'absolute', flex: 1 } }); export default class Pulse extends Component { static propTypes = { color: PropTypes.string, diameter: PropTypes.number, duration: PropTypes.number, image: PropTypes.object, initialDiameter: PropTypes.number, numPulses: PropTypes.number, pulseStyle: PropTypes.object, speed: PropTypes.number, style: PropTypes.object }; static defaultProps = { color: 'blue', diameter: 400, duration: 1000, image: null, initialDiameter: 0, numPulses: 3, pulseStyle: {}, speed: 10, style: { top: 0, bottom: 0, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' } } constructor(props){ super(props); this.state = { color: this.props.color, duration: this.props.duration, image: this.props.image, maxDiameter: this.props.diameter, numPulses: this.props.numPulses, pulses: [], pulseStyle: this.props.pulseStyle, speed: this.props.speed, started: false, style: this.props.style }; } mounted = true; componentDidMount(){ const {numPulses, duration, speed} = this.state; this.setState({started: true}); let a = 0; while(a < numPulses){ this.createPulseTimer = setTimeout(()=>{ this.createPulse(a); }, a * duration); a++; } this.timer = setInterval(() => { this.updatePulse(); }, speed); } componentWillUnmount(){ this.mounted = false; clearTimeout(this.createPulseTimer); clearInterval(this.timer); } createPulse = (pKey) => { if (this.mounted) { let pulses = this.state.pulses; let pulse = { pulseKey: pulses.length + 1, diameter: this.props.initialDiameter, opacity: .5, centerOffset: ( this.state.maxDiameter - this.props.initialDiameter ) / 2 }; pulses.push(pulse); this.setState({pulses}); } } updatePulse = () => { if (this.mounted) { const pulses = this.state.pulses.map((p, i) => { let maxDiameter = this.state.maxDiameter; let newDiameter = (p.diameter > maxDiameter ? 0 : p.diameter + 2); let centerOffset = ( maxDiameter - newDiameter ) / 2; let opacity = Math.abs( ( newDiameter / this.state.maxDiameter ) - 1 ); let pulse = { pulseKey: i + 1, diameter: newDiameter, opacity: (opacity > .5 ? .5 : opacity), centerOffset: centerOffset }; return pulse; }); this.setState({pulses}); } } render(){ const {color, image, maxDiameter, pulses, pulseStyle, started, style} = this.state; const containerStyle = [styles.container, style]; const pulseWrapperStyle = {width: maxDiameter, height: maxDiameter}; return ( {started && {pulses.map((pulse) => )} {image && } } ) } }