|
| 1 | +import type { IPath } from "@shopify/react-native-skia"; |
| 2 | +import { |
| 3 | + Line, |
| 4 | + Canvas, |
| 5 | + Circle, |
| 6 | + Fill, |
| 7 | + LinearGradient, |
| 8 | + Paint, |
| 9 | + Path, |
| 10 | + useTouchHandler, |
| 11 | + useValue, |
| 12 | + Text as SkiaText, |
| 13 | + vec, |
| 14 | +} from "@shopify/react-native-skia"; |
| 15 | +import React, { useMemo } from "react"; |
| 16 | +import { StyleSheet, Text, View } from "react-native"; |
| 17 | + |
| 18 | +import { createGraphPath } from "./createGraphPath"; |
| 19 | +import type { GraphProps } from "./types"; |
| 20 | + |
| 21 | +export const Slider: React.FC<GraphProps> = ({ height, width }) => { |
| 22 | + const path = useMemo( |
| 23 | + () => createGraphPath(width, height, 60, false), |
| 24 | + [height, width] |
| 25 | + ); |
| 26 | + |
| 27 | + const progress = useValue( |
| 28 | + getPointAtPositionInPath(width / 2, width, 60, path) |
| 29 | + ); |
| 30 | + |
| 31 | + const touchHandler = useTouchHandler({ |
| 32 | + onActive: ({ x }) => |
| 33 | + (progress.value = getPointAtPositionInPath(x, width, 60, path)), |
| 34 | + }); |
| 35 | + |
| 36 | + return ( |
| 37 | + <View style={{ height, marginBottom: 10 }}> |
| 38 | + <Canvas style={styles.graph} onTouch={touchHandler}> |
| 39 | + <Fill color="black" /> |
| 40 | + <Paint> |
| 41 | + <LinearGradient |
| 42 | + start={vec(0, height * 0.5)} |
| 43 | + end={vec(width * 0.5, height * 0.5)} |
| 44 | + colors={["black", "#DA4167"]} |
| 45 | + /> |
| 46 | + </Paint> |
| 47 | + <Path |
| 48 | + path={path} |
| 49 | + strokeWidth={4} |
| 50 | + style="stroke" |
| 51 | + strokeJoin="round" |
| 52 | + strokeCap="round" |
| 53 | + /> |
| 54 | + <Paint color="#fff" /> |
| 55 | + <Circle c={() => progress.value} r={10} /> |
| 56 | + <Circle color="#DA4167" c={() => progress.value} r={7.5} /> |
| 57 | + <SkiaText |
| 58 | + familyName="Arial" |
| 59 | + size={12} |
| 60 | + x={() => progress.value.x - 24} |
| 61 | + y={() => progress.value.y - 18} |
| 62 | + value={() => "$ " + progress.value.x.toFixed(2)} |
| 63 | + /> |
| 64 | + <Line |
| 65 | + p1={() => vec(progress.value.x, progress.value.y + 14)} |
| 66 | + p2={() => vec(progress.value.x, height)} |
| 67 | + /> |
| 68 | + </Canvas> |
| 69 | + <Text>Touch and drag to move center point</Text> |
| 70 | + </View> |
| 71 | + ); |
| 72 | +}; |
| 73 | + |
| 74 | +const getPointAtPositionInPath = ( |
| 75 | + x: number, |
| 76 | + width: number, |
| 77 | + steps: number, |
| 78 | + path: IPath |
| 79 | +) => { |
| 80 | + const index = Math.max(0, Math.floor(x / (width / steps))); |
| 81 | + const fraction = (x / (width / steps)) % 1; |
| 82 | + const p1 = path.getPoint(index); |
| 83 | + if (index < path.countPoints() - 1) { |
| 84 | + const p2 = path.getPoint(index + 1); |
| 85 | + // Interpolate between p1 and p2 |
| 86 | + return { |
| 87 | + x: p1.x + (p2.x - p1.x) * fraction, |
| 88 | + y: p1.y + (p2.y - p1.y) * fraction, |
| 89 | + }; |
| 90 | + } |
| 91 | + return p1; |
| 92 | +}; |
| 93 | + |
| 94 | +const styles = StyleSheet.create({ |
| 95 | + graph: { |
| 96 | + flex: 1, |
| 97 | + }, |
| 98 | +}); |
0 commit comments