|
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import { Canvas, Picture, Skia } from "@shopify/react-native-skia"; |
| 3 | +import { |
| 4 | + useDerivedValue, |
| 5 | + useSharedValue, |
| 6 | + withRepeat, |
| 7 | + withTiming, |
| 8 | +} from "react-native-reanimated"; |
| 9 | + |
| 10 | +const size = 256; |
| 11 | +const n = 20; |
| 12 | + |
| 13 | +const paint = Skia.Paint(); |
| 14 | + |
| 15 | +export const Pictures = () => { |
| 16 | + const progress = useSharedValue(0); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + progress.value = withRepeat(withTiming(1, { duration: 3000 }), -1, true); |
| 20 | + }, [progress]); |
| 21 | + |
| 22 | + const picture = useDerivedValue(() => { |
| 23 | + "worklet"; |
| 24 | + const recorder = Skia.PictureRecorder(); |
| 25 | + const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, size, size)); |
| 26 | + const angle = progress.value * Math.PI * 2; |
| 27 | + for (let i = 0; i < n; i++) { |
| 28 | + const alpha = Math.pow((i + 1) / n, 5) * 200; |
| 29 | + const r = ((i + 1) / n) * (size / 4); |
| 30 | + const offset = ((i + 1) / n) * (size / 4); |
| 31 | + const cx = size / 2 + Math.cos(angle + (i * Math.PI * 2) / n) * offset; |
| 32 | + const cy = size / 2 + Math.sin(angle + (i * Math.PI * 2) / n) * offset; |
| 33 | + paint.setColor(Skia.Color(`rgba(0, 122, 255, ${alpha / 255})`)); |
| 34 | + canvas.drawCircle(cx, cy, r, paint); |
| 35 | + } |
| 36 | + return recorder.finishRecordingAsPicture(); |
| 37 | + }); |
| 38 | + |
| 39 | + return ( |
| 40 | + <Canvas style={{ flex: 1 }}> |
| 41 | + <Picture picture={picture} /> |
| 42 | + </Canvas> |
| 43 | + ); |
| 44 | +}; |
0 commit comments