Junjie
2024-03-02 1fee72f5089e6ec92e8a0116185bac42972d1bce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as React from 'react'
import * as PIXI from 'pixi.js';
import { FormattedMessage, useIntl, useModel } from '@umijs/max';
import { Layout, Button, Flex, } from 'antd';
const { Header, Content } = Layout;
import './index.css'
import { createStyles } from 'antd-style';
import Edit from './components/edit'
 
const useStyles = createStyles(({ token }) => {
    let dark = token.colorBgBase === '#000';
    return {
        layout: {
            overflow: 'hidden',
        },
        header: {
            height: 64,
            paddingInline: 48,
            lineHeight: '64px',
            padding: 0,
        },
        flex: {
            width: '100%',
            height: '100%',
            padding: '0 30px',
        },
        content: {
            backgroundColor: '#F8FAFB',
            height: 'calc(100vh - 120px)'
        }
    };
});
 
const Map = () => {
    const { initialState, setInitialState } = useModel('@@initialState');
    const { styles } = useStyles();
    const mapRef = React.useRef();
    const contentRef = React.useRef();
 
    const [editModalVisible, setEditModalVisible] = React.useState(false);
 
    const [app, setApp] = React.useState(() => {
        return new PIXI.Application({
            background: '#F8FAFB',
            antialias: true,
        })
    })
 
    const [windowSize, setWindowSize] = React.useState({
        width: window.innerWidth,
        height: window.innerHeight,
    });
 
    React.useEffect(() => {
        const handleResize = () => {
            setWindowSize({
                width: window.innerWidth,
                height: window.innerHeight,
            });
        };
        window.addEventListener('resize', handleResize);
 
        setTimeout(() => {
            // app init
            app.stage.eventMode = 'auto';
            app.stage.hitArea = app.screen;
            globalThis.__PIXI_APP__ = app;
            mapRef.current.appendChild(app.view);
 
            // mapContainer ------------------------------
            const mapContainer = new PIXI.Container();
            mapContainer.name = "mapContainer";
            mapContainer.data = {};
            app.stage.addChild(mapContainer);
 
            const texture = PIXI.Texture.from('https://pixijs.com/assets/bunny.png');
            const bunny = new PIXI.Sprite(texture);
            bunny.anchor.set(0.5);
            bunny.x = app.screen.width / 2;
            bunny.y = app.screen.height / 2;
 
            mapContainer.addChild(bunny);
 
            app.ticker.add((delta) => {
                bunny.rotation -= 0.01 * delta;
            });
 
            return () => {
                app.destroy(true, true);
            }
        }, 300)
 
    }, []);
 
    React.useEffect(() => {
        const width = contentRef.current.offsetWidth;
        const height = contentRef.current.offsetHeight;
        app.renderer.resize(width, height);
    }, [windowSize]);
 
    const editHandle = () => {
        setEditModalVisible(true);
    }
 
    return (
        <>
            <Layout className={styles.layout}>
                <Header className={styles.header}>
                    <Flex className={styles.flex} gap={'large'} justify={'flex-end'} align={'center'}>
                        <Button onClick={editHandle} size={'large'}><FormattedMessage id='map.edit' defaultMessage='编辑地图' /></Button>
                    </Flex>
                </Header>
                <Content ref={contentRef} className={styles.content}>
                    <div ref={mapRef} style={{ position: "relative" }} />
                </Content>
            </Layout>
 
            <Edit
                open={editModalVisible}
                onCancel={
                    () => {
                        setEditModalVisible(false);
                    }
                }
                refCurr={mapRef.current}
            />
        </>
    )
}
 
export default Map;