|
| 1 | +import React, { useMemo } from "react"; |
| 2 | +import { Dimensions, ScrollView, Text, View } from "react-native"; |
| 3 | +import { Canvas, Circle, Group, useClock } from "@shopify/react-native-skia"; |
| 4 | +import type { SharedValue } from "react-native-reanimated"; |
| 5 | +import { useDerivedValue } from "react-native-reanimated"; |
| 6 | + |
| 7 | +const { width } = Dimensions.get("window"); |
| 8 | +const CANVAS_HEIGHT = 350; |
| 9 | +const CENTER = { x: width / 2, y: CANVAS_HEIGHT / 2 }; |
| 10 | +const NUM_POINTS = 150; |
| 11 | +const SPHERE_RADIUS = width * 0.35; |
| 12 | + |
| 13 | +// Fibonacci Sphere Algorithm to distribute points evenly |
| 14 | +const getSpherePoints = (n: number) => { |
| 15 | + const points = []; |
| 16 | + const phi = Math.PI * (3 - Math.sqrt(5)); // golden angle |
| 17 | + |
| 18 | + for (let i = 0; i < n; i++) { |
| 19 | + const y = 1 - (i / (n - 1)) * 2; // y goes from 1 to -1 |
| 20 | + const radius = Math.sqrt(1 - y * y); // radius at y |
| 21 | + |
| 22 | + const theta = phi * i; // golden angle increment |
| 23 | + |
| 24 | + const x = Math.cos(theta) * radius; |
| 25 | + const z = Math.sin(theta) * radius; |
| 26 | + |
| 27 | + points.push({ x, y, z }); |
| 28 | + } |
| 29 | + return points; |
| 30 | +}; |
| 31 | + |
| 32 | +const POINTS = getSpherePoints(NUM_POINTS).sort(() => Math.random() - 0.5); |
| 33 | + |
| 34 | +interface Point3D { |
| 35 | + x: number; |
| 36 | + y: number; |
| 37 | + z: number; |
| 38 | +} |
| 39 | + |
| 40 | +const SpherePoint = ({ |
| 41 | + point, |
| 42 | + rotation, |
| 43 | + enableZIndex, |
| 44 | +}: { |
| 45 | + point: Point3D; |
| 46 | + rotation: { x: SharedValue<number>; y: SharedValue<number> }; |
| 47 | + enableZIndex: boolean; |
| 48 | +}) => { |
| 49 | + const derived = useDerivedValue(() => { |
| 50 | + // Rotate around Y axis |
| 51 | + const cosY = Math.cos(rotation.y.value); |
| 52 | + const sinY = Math.sin(rotation.y.value); |
| 53 | + |
| 54 | + // Rotate around X axis |
| 55 | + const cosX = Math.cos(rotation.x.value); |
| 56 | + const sinX = Math.sin(rotation.x.value); |
| 57 | + |
| 58 | + let { x } = point; |
| 59 | + let { y } = point; |
| 60 | + let { z } = point; |
| 61 | + |
| 62 | + // Apply Y rotation |
| 63 | + const x1 = x * cosY - z * sinY; |
| 64 | + const z1 = z * cosY + x * sinY; |
| 65 | + x = x1; |
| 66 | + z = z1; |
| 67 | + |
| 68 | + // Apply X rotation |
| 69 | + const y2 = y * cosX - z * sinX; |
| 70 | + const z2 = z * cosX + y * sinX; |
| 71 | + y = y2; |
| 72 | + z = z2; |
| 73 | + |
| 74 | + // Project to 2D |
| 75 | + const scale = (z + 2) / 3; // Simple perspective projection |
| 76 | + const screenX = CENTER.x + x * SPHERE_RADIUS; |
| 77 | + const screenY = CENTER.y + y * SPHERE_RADIUS; |
| 78 | + |
| 79 | + // Map z (-1 to 1) to zIndex |
| 80 | + // We want points with higher z (closer to camera) to have higher zIndex |
| 81 | + const zIndex = Math.round(z * 1000); |
| 82 | + |
| 83 | + return { |
| 84 | + cx: screenX, |
| 85 | + cy: screenY, |
| 86 | + r: 20 * scale, |
| 87 | + zIndex: enableZIndex ? zIndex : 0, |
| 88 | + color: `hsl(${((x + 1) / 2) * 360}, 80%, ${((z + 2) / 3) * 60}%)`, |
| 89 | + }; |
| 90 | + }); |
| 91 | + |
| 92 | + const cx = useDerivedValue(() => derived.value.cx); |
| 93 | + const cy = useDerivedValue(() => derived.value.cy); |
| 94 | + const r = useDerivedValue(() => derived.value.r); |
| 95 | + const color = useDerivedValue(() => derived.value.color); |
| 96 | + const zIndex = useDerivedValue(() => derived.value.zIndex); |
| 97 | + |
| 98 | + return ( |
| 99 | + <Group zIndex={zIndex}> |
| 100 | + <Circle cx={cx} cy={cy} r={r} color={color} /> |
| 101 | + <Circle |
| 102 | + cx={cx} |
| 103 | + cy={cy} |
| 104 | + r={r} |
| 105 | + color="black" |
| 106 | + style="stroke" |
| 107 | + strokeWidth={1} |
| 108 | + /> |
| 109 | + </Group> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +const Sphere = ({ enableZIndex }: { enableZIndex: boolean }) => { |
| 114 | + const clock = useClock(); |
| 115 | + |
| 116 | + const rotationY = useDerivedValue(() => { |
| 117 | + return (clock.value / 4000) * Math.PI * 2; |
| 118 | + }); |
| 119 | + |
| 120 | + const rotationX = useDerivedValue(() => { |
| 121 | + return (clock.value / 6000) * Math.PI * 2; |
| 122 | + }); |
| 123 | + |
| 124 | + const rotationValues = useMemo( |
| 125 | + () => ({ x: rotationX, y: rotationY }), |
| 126 | + [rotationX, rotationY] |
| 127 | + ); |
| 128 | + |
| 129 | + return ( |
| 130 | + <Group> |
| 131 | + {POINTS.map((point, i) => ( |
| 132 | + <SpherePoint |
| 133 | + key={i} |
| 134 | + point={point} |
| 135 | + rotation={rotationValues} |
| 136 | + enableZIndex={enableZIndex} |
| 137 | + /> |
| 138 | + ))} |
| 139 | + </Group> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export const ZIndexExample = () => { |
| 144 | + return ( |
| 145 | + <ScrollView style={{ flex: 1, backgroundColor: "#111" }}> |
| 146 | + <View style={{ padding: 20, paddingBottom: 0 }}> |
| 147 | + <Text |
| 148 | + style={{ |
| 149 | + color: "white", |
| 150 | + fontSize: 24, |
| 151 | + fontWeight: "bold", |
| 152 | + marginBottom: 10, |
| 153 | + }} |
| 154 | + > |
| 155 | + With zIndex |
| 156 | + </Text> |
| 157 | + </View> |
| 158 | + <Canvas style={{ width, height: CANVAS_HEIGHT }}> |
| 159 | + <Sphere enableZIndex={true} /> |
| 160 | + </Canvas> |
| 161 | + |
| 162 | + <View style={{ padding: 20, paddingBottom: 0 }}> |
| 163 | + <Text |
| 164 | + style={{ |
| 165 | + color: "white", |
| 166 | + fontSize: 24, |
| 167 | + fontWeight: "bold", |
| 168 | + marginBottom: 10, |
| 169 | + }} |
| 170 | + > |
| 171 | + Without zIndex |
| 172 | + </Text> |
| 173 | + </View> |
| 174 | + <Canvas style={{ width, height: CANVAS_HEIGHT }}> |
| 175 | + <Sphere enableZIndex={false} /> |
| 176 | + </Canvas> |
| 177 | + </ScrollView> |
| 178 | + ); |
| 179 | +}; |
0 commit comments