This tutorial explains how to create wheel of fortune in react native application. In this lesson we’re going to be building a Wheel of Fortune in React Native using react-native-gesture-handler, D3 (d3-shape) and Expo for creating the react-native project.Also we do require below libraries as well.
Expo: https://expo.io/
React Native Gesture Handler: https://github.com/kmagiera/react-native-gesture-handler
Popmotion Popcorn: https://popmotion.io/popcorn
RandomColor: https://randomcolor.lllllllllllllllll.com
Pin SVG: http://clipartist.net/social/clipartist.net/G/places_pin_icon.svg
Source : https://github.com/catalinmiron/react-native-wheel-of-fortune
App.js
See the demo :
This is all about React Native Wheel of Fortune Example. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
Expo: https://expo.io/
React Native Gesture Handler: https://github.com/kmagiera/react-native-gesture-handler
Popmotion Popcorn: https://popmotion.io/popcorn
RandomColor: https://randomcolor.lllllllllllllllll.com
Pin SVG: http://clipartist.net/social/clipartist.net/G/places_pin_icon.svg
Source : https://github.com/catalinmiron/react-native-wheel-of-fortune
React Native Lucky Draw Spinner
Lets see the below complete source code for react native wheel of fortune.App.js
import React from 'react'; import { StyleSheet, View, Text as RNText, Dimensions, Animated } from 'react-native'; import { GestureHandler, Svg } from 'expo'; import * as d3Shape from 'd3-shape'; import color from 'randomcolor'; import { snap } from '@popmotion/popcorn'; const { PanGestureHandler, State } = GestureHandler; const { Path, G, Text, TSpan } = Svg; const { width } = Dimensions.get('screen'); const numberOfSegments = 12; const wheelSize = width * 0.95; const fontSize = 26; const oneTurn = 360; const angleBySegment = oneTurn / numberOfSegments; const angleOffset = angleBySegment / 2; const knobFill = color({ hue: 'purple' }); const makeWheel = () => { const data = Array.from({ length: numberOfSegments }).fill(1); const arcs = d3Shape.pie()(data); const colors = color({ luminosity: 'dark', count: numberOfSegments }); return arcs.map((arc, index) => { const instance = d3Shape .arc() .padAngle(0.01) .outerRadius(width / 2) .innerRadius(20); return { path: instance(arc), color: colors[index], value: Math.round(Math.random() * 10 + 1) * 200, //[200, 2200] centroid: instance.centroid(arc) }; }); }; export default class App extends React.Component { _wheelPaths = makeWheel(); _angle = new Animated.Value(0); angle = 0; state = { enabled: true, finished: false, winner: null }; componentDidMount() { this._angle.addListener(event => { if (this.state.enabled) { this.setState({ enabled: false, finished: false }); } this.angle = event.value; }); } _getWinnerIndex = () => { const deg = Math.abs(Math.round(this.angle % oneTurn)); return Math.floor(deg / angleBySegment); }; _onPan = ({ nativeEvent }) => { if (nativeEvent.state === State.END) { const { velocityY } = nativeEvent; Animated.decay(this._angle, { velocity: velocityY / 1000, deceleration: 0.999, useNativeDriver: true }).start(() => { this._angle.setValue(this.angle % oneTurn); const snapTo = snap(oneTurn / numberOfSegments); Animated.timing(this._angle, { toValue: snapTo(this.angle), duration: 300, useNativeDriver: true }).start(() => { const winnerIndex = this._getWinnerIndex(); this.setState({ enabled: true, finished: true, winner: this._wheelPaths[winnerIndex].value }); }); // do something here; }); } }; render() { return ( <PanGestureHandler onHandlerStateChange={this._onPan} enabled={this.state.enabled} > <View style={styles.container}> {this._renderSvgWheel()} {this.state.finished && this.state.enabled && this._renderWinner()} </View> </PanGestureHandler> ); } _renderKnob = () => { const knobSize = 30; // [0, numberOfSegments] const YOLO = Animated.modulo( Animated.divide( Animated.modulo(Animated.subtract(this._angle, angleOffset), oneTurn), new Animated.Value(angleBySegment) ), 1 ); return ( <Animated.View style={{ width: knobSize, height: knobSize * 2, justifyContent: 'flex-end', zIndex: 1, transform: [ { rotate: YOLO.interpolate({ inputRange: [-1, -0.5, -0.0001, 0.0001, 0.5, 1], outputRange: ['0deg', '0deg', '35deg', '-35deg', '0deg', '0deg'] }) } ] }} > <Svg width={knobSize} height={(knobSize * 100) / 57} viewBox={`0 0 57 100`} style={{ transform: [{ translateY: 8 }] }} > <Path d="M28.034,0C12.552,0,0,12.552,0,28.034S28.034,100,28.034,100s28.034-56.483,28.034-71.966S43.517,0,28.034,0z M28.034,40.477c-6.871,0-12.442-5.572-12.442-12.442c0-6.872,5.571-12.442,12.442-12.442c6.872,0,12.442,5.57,12.442,12.442 C40.477,34.905,34.906,40.477,28.034,40.477z" fill={knobFill} /> </Svg> </Animated.View> ); }; _renderWinner = () => { return ( <RNText style={styles.winnerText}>Winner is: {this.state.winner}</RNText> ); }; _renderSvgWheel = () => { return ( <View style={styles.container}> {this._renderKnob()} <Animated.View style={{ alignItems: 'center', justifyContent: 'center', transform: [ { rotate: this._angle.interpolate({ inputRange: [-oneTurn, 0, oneTurn], outputRange: [`-${oneTurn}deg`, `0deg`, `${oneTurn}deg`] }) } ] }} > <Svg width={wheelSize} height={wheelSize} viewBox={`0 0 ${width} ${width}`} style={{ transform: [{ rotate: `-${angleOffset}deg` }] }} > <G y={width / 2} x={width / 2}> {this._wheelPaths.map((arc, i) => { const [x, y] = arc.centroid; const number = arc.value.toString(); return ( <G key={`arc-${i}`}> <Path d={arc.path} fill={arc.color} /> <G rotation={(i * oneTurn) / numberOfSegments + angleOffset} origin={`${x}, ${y}`} > <Text x={x} y={y - 70} fill="white" textAnchor="middle" fontSize={fontSize} > {Array.from({ length: number.length }).map((_, j) => { return ( <TSpan x={x} dy={fontSize} key={`arc-${i}-slice-${j}`} > {number.charAt(j)} </TSpan> ); })} </Text> </G> </G> ); })} </G> </Svg> </Animated.View> </View> ); }; } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center' }, winnerText: { fontSize: 32, fontFamily: 'Menlo', position: 'absolute', bottom: 10 } });
See the demo :
This is all about React Native Wheel of Fortune Example. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
No comments:
Post a Comment