|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { StyleSheet, View, Dimensions } from 'react-native'; |
| 3 | +import { PanGestureHandler, State } from 'react-native-gesture-handler'; |
| 4 | +import Animated from 'react-native-reanimated'; |
| 5 | + |
| 6 | +// setInterval(() => { |
| 7 | +// let iters = 1e8, |
| 8 | +// sum = 0; |
| 9 | +// while (iters-- > 0) sum += iters; |
| 10 | +// }, 300); |
| 11 | + |
| 12 | +const { |
| 13 | + set, |
| 14 | + cond, |
| 15 | + eq, |
| 16 | + add, |
| 17 | + multiply, |
| 18 | + lessThan, |
| 19 | + abs, |
| 20 | + modulo, |
| 21 | + round, |
| 22 | + interpolate, |
| 23 | + divide, |
| 24 | + sub, |
| 25 | + color, |
| 26 | + Value, |
| 27 | + event, |
| 28 | +} = Animated; |
| 29 | + |
| 30 | +const PICKER_WIDTH = Dimensions.get('window').width; |
| 31 | +const PICKER_HEIGHT = Dimensions.get('window').height; |
| 32 | + |
| 33 | +function match(condsAndResPairs, offset = 0) { |
| 34 | + if (condsAndResPairs.length - offset === 1) { |
| 35 | + return condsAndResPairs[offset]; |
| 36 | + } else if (condsAndResPairs.length - offset === 0) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + return cond( |
| 40 | + condsAndResPairs[offset], |
| 41 | + condsAndResPairs[offset + 1], |
| 42 | + match(condsAndResPairs, offset + 2) |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +function colorHSV(h /* 0 - 360 */, s /* 0 - 1 */, v /* 0 - 1 */) { |
| 47 | + // Converts color from HSV format into RGB |
| 48 | + // Formula explained here: https://www.rapidtables.com/convert/color/hsv-to-rgb.html |
| 49 | + const c = multiply(v, s); |
| 50 | + const hh = divide(h, 60); |
| 51 | + const x = multiply(c, sub(1, abs(sub(modulo(hh, 2), 1)))); |
| 52 | + |
| 53 | + const m = sub(v, c); |
| 54 | + |
| 55 | + const colorRGB = (r, g, b) => |
| 56 | + color( |
| 57 | + round(multiply(255, add(r, m))), |
| 58 | + round(multiply(255, add(g, m))), |
| 59 | + round(multiply(255, add(b, m))) |
| 60 | + ); |
| 61 | + |
| 62 | + return match([ |
| 63 | + lessThan(h, 60), |
| 64 | + colorRGB(c, x, 0), |
| 65 | + lessThan(h, 120), |
| 66 | + colorRGB(x, c, 0), |
| 67 | + lessThan(h, 180), |
| 68 | + colorRGB(0, c, x), |
| 69 | + lessThan(h, 240), |
| 70 | + colorRGB(0, x, c), |
| 71 | + lessThan(h, 300), |
| 72 | + colorRGB(x, 0, c), |
| 73 | + colorRGB(c, 0, x) /* else */, |
| 74 | + ]); |
| 75 | +} |
| 76 | + |
| 77 | +export default class Example extends Component { |
| 78 | + static navigationOptions = { |
| 79 | + title: 'Colors Example', |
| 80 | + }; |
| 81 | + constructor(props) { |
| 82 | + super(props); |
| 83 | + |
| 84 | + const dragX = new Value(0); |
| 85 | + const dragY = new Value(0); |
| 86 | + const state = new Value(-1); |
| 87 | + |
| 88 | + this._onGestureEvent = event([ |
| 89 | + { |
| 90 | + nativeEvent: { translationX: dragX, translationY: dragY, state: state }, |
| 91 | + }, |
| 92 | + ]); |
| 93 | + |
| 94 | + const offsetX = new Value(PICKER_WIDTH / 2); |
| 95 | + this._transX = cond( |
| 96 | + eq(state, State.ACTIVE), |
| 97 | + add(offsetX, dragX), |
| 98 | + set(offsetX, add(offsetX, dragX)) |
| 99 | + ); |
| 100 | + |
| 101 | + const offsetY = new Value(200); |
| 102 | + this._transY = cond( |
| 103 | + eq(state, State.ACTIVE), |
| 104 | + add(offsetY, dragY), |
| 105 | + set(offsetY, add(offsetY, dragY)) |
| 106 | + ); |
| 107 | + |
| 108 | + const h = interpolate(this._transX, { |
| 109 | + inputRange: [0, PICKER_WIDTH], |
| 110 | + outputRange: [0, 360], |
| 111 | + extrapolate: 'clamp', |
| 112 | + }); |
| 113 | + const s = interpolate(this._transY, { |
| 114 | + inputRange: [0, PICKER_HEIGHT], |
| 115 | + outputRange: [1, 0], |
| 116 | + extrapolate: 'clamp', |
| 117 | + }); |
| 118 | + const v = 1; |
| 119 | + this._color = colorHSV(h, s, v); |
| 120 | + } |
| 121 | + render() { |
| 122 | + return ( |
| 123 | + <View style={styles.container}> |
| 124 | + <PanGestureHandler |
| 125 | + maxPointers={1} |
| 126 | + onGestureEvent={this._onGestureEvent} |
| 127 | + onHandlerStateChange={this._onGestureEvent}> |
| 128 | + <Animated.View |
| 129 | + style={[ |
| 130 | + styles.box, |
| 131 | + { |
| 132 | + backgroundColor: this._color, |
| 133 | + transform: [ |
| 134 | + { translateX: this._transX, translateY: this._transY }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + ]} |
| 138 | + /> |
| 139 | + </PanGestureHandler> |
| 140 | + </View> |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +const CIRCLE_SIZE = 70; |
| 146 | + |
| 147 | +const styles = StyleSheet.create({ |
| 148 | + container: { |
| 149 | + flex: 1, |
| 150 | + backgroundColor: '#F5FCFF', |
| 151 | + }, |
| 152 | + box: { |
| 153 | + width: CIRCLE_SIZE, |
| 154 | + height: CIRCLE_SIZE, |
| 155 | + marginLeft: -CIRCLE_SIZE / 2, |
| 156 | + marginTop: -CIRCLE_SIZE / 2, |
| 157 | + borderRadius: CIRCLE_SIZE / 2, |
| 158 | + borderColor: 'black', |
| 159 | + borderWidth: 1, |
| 160 | + }, |
| 161 | +}); |
0 commit comments