Projekt

Obecné

Profil

Stáhnout (5.43 KB) Statistiky
| Větev: | Tag: | Revize:
1 d0870262 Fantič
import React, { useState, useEffect, useRef } from "react";
2
3 6020775f Fantič
import { Box, Flex, Text, Image, Popover, Pressable, VStack, View, Modal, Overlay } from "native-base"
4 d0870262 Fantič
import { PinchGestureHandler, PanGestureHandler, State } from "react-native-gesture-handler";
5
import Animated, { useAnimatedGestureHandler, useAnimatedStyle, useSharedValue } from "react-native-reanimated";
6
import Svg, { SvgXml, Path } from "react-native-svg";
7
import { FullscreenIcon, ExitfullscreenIcon } from "../general/Icons";
8 6020775f Fantič
import { Button, Dimensions } from "react-native";
9 d0870262 Fantič
10
export const ZoomableImage = (props: { size: number, imageUri: string, fullScreen?: boolean }) => {
11
    const DEFAULT_SCALE = 1
12
    const MIN_SCALE = 0.95
13
14
    const [fullScreenMode, setFullScreenMode] = useState<boolean>(false);
15
    const [viewSize, setViewSize] = useState({ height: 0, width: 0 });
16
17
    const panRef = useRef<React.ReactElement>();
18
    const pinchRef = useRef<React.ReactElement>();
19
20
21
    useEffect(() => {
22
        if (props.fullScreen != undefined) {
23
            setFullScreenMode(props.fullScreen)
24
        }
25
    }, [props.fullScreen])
26
27
    useEffect(() => {
28
        setViewSize({
29
            width: fullScreenMode ?
30
                Dimensions.get('window').width - 20 : // full screen
31
                Dimensions.get('window').width - 20, // not full screen
32
            height: fullScreenMode ?
33
                Dimensions.get('window').height - 122.5 : // full scren
34
                Dimensions.get('window').height - 350, // not full screen
35
        })
36
    }, [fullScreenMode])
37
    const translateX = useSharedValue(0);
38
    const translateY = useSharedValue(0);
39
    const scale = useSharedValue(DEFAULT_SCALE);
40
41
    const onPanEvent = useAnimatedGestureHandler({
42
        // handle one finger -> drag / move
43
        onStart: (event, ctx: any) => {
44
            if (event.numberOfPointers === 1) {
45
                ctx.startX = translateX.value;
46
                ctx.startY = translateY.value;
47
            }
48
        },
49
        onActive: (event, ctx: any) => {
50
            if (event.numberOfPointers === 1) {
51
                translateX.value = ctx.startX + event.translationX;
52
                translateY.value = ctx.startY + event.translationY;
53
54
                console.log("xTranslate " + translateX.value + " yTranslate " + translateY.value)
55
            }
56
        },
57
    });
58
59
    const onGestureEvent = useAnimatedGestureHandler({
60
        onStart: (_, ctx: any) => {
61
            ctx.startScale = scale.value;
62
        },
63
        onActive: (event: any, ctx: any) => {
64
            // handle two fingers -> zoom + -
65
            if (event.numberOfPointers === 2) {
66
                scale.value = ctx.startScale * event.scale;
67
            }
68
        },
69
        onEnd: (event) => {
70
            // handle two fingers -> zoom + -
71
            if (event.numberOfPointers === 2) {
72
                if (scale.value < MIN_SCALE) {
73
                    scale.value = MIN_SCALE;
74
                }
75
                // You may add additional logic for maximum scale if needed
76
77
                console.log("scale")
78
                console.log(scale.value)
79
            }
80
        },
81
    });
82
83
    const animatedStyle = useAnimatedStyle(() => {
84
        return {
85
            transform: [
86
                { translateX: translateX.value },
87
                { translateY: translateY.value },
88
                { scale: scale.value },
89
            ],
90
        };
91
    });
92
93
94 6020775f Fantič
    console.log(viewSize)
95
    console.log(fullScreenMode)
96 d0870262 Fantič
    return (
97 6020775f Fantič
        <View>
98
            <Pressable onPress={() => setFullScreenMode(true)}>
99
                <Image size={viewSize.width} source={{ uri: props.imageUri }} alt="image" resizeMode={"contain"} />
100
            </Pressable>
101
102
            <Modal isOpen={fullScreenMode} onClose={() => setFullScreenMode(false)} size="full">
103
                <Box
104
                    flex={1}
105
                    justifyContent="center"
106
                    alignItems="center"
107
                    backgroundColor="transparent" // Make the background transparent
108
                >
109
                    <Pressable
110
                        position="absolute"
111
                        bottom={20}
112
                        right={0}
113
                        padding={2}
114
                        zIndex={1000}
115
                        onPress={() => setFullScreenMode(false)}
116 d0870262 Fantič
                    >
117 6020775f Fantič
                        {/* Your close button (you can replace this with an icon) */}
118
                        <Box style={{width: 70, height: 30, backgroundColor:"white", borderRadius: 10}} alignItems={"center"} justifyContent="center">
119
                            <Text>Close</Text>
120
                        </Box>
121
                    </Pressable>
122
                    <Image
123
                        height={viewSize.height}
124
                        width={viewSize.width}
125
                        alt="image"
126
                        source={{ uri: props.imageUri }}
127
                        resizeMode={"contain"}
128
                    />
129
                </Box>
130
            </Modal>
131
        </View>
132
        // <Flex direction="row" alignItems="center" justify="flex-end" style={{ height: 50, width: 100, top: fullScreenMode ? 10 : 0, right: fullScreenMode ? 20 : 15, position: "absolute", zIndex: 5 }}>
133
        //     {
134
        //         fullScreenMode &&
135
        //         <Pressable padding={1.5} backgroundColor={"#654B07"} borderRadius={5} marginRight={-2} onPress={() => setFullScreenMode(false)}>
136
        //             <FullscreenIcon color="white" />
137
        //         </Pressable>
138
        //     }
139
140
        // </Flex>
141 d0870262 Fantič
    )
142
143
}