-
zhangwei
2024-08-22 d5eb1ecff5d0236359c744b334cf30fcfdad1132
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号: 9 1  5 10 13 1 3     32   006 193K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-lottie__wrap">
        <!-- #ifdef APP-VUE || H5 -->
        <view ref="lottieRef" id="lottieRef" :prop="options" :change:prop="handler.changeProp" :action="actionVal"
            :change:action="handler.changeAction" :style="{width:width+'rpx',height:height+'rpx'}"></view>
        <!-- #endif -->
        <!-- #ifdef MP -->
        <canvas :style="{width:lottieW+'px',height:lottieH+'px'}" :class="canvasId" type="2d"></canvas>
        <!-- #endif -->
    </view>
</template>
 
<!-- #ifdef APP-VUE || H5 -->
<script module="handler" lang="renderjs">
    import lottie from './fui-lottie-app.js'
 
    export default {
        data() {
            return {
                ani: null,
                prevAction: '',
                isInit: false
            }
        },
        methods: {
            changeProp(vals, old) {
                let appVue3 = false
                // #ifdef APP-VUE
                // #ifdef VUE3
                appVue3 = true;
                // #endif
                // #endif
                if (!this.isInit && !appVue3) return;
                this.init(vals || old)
            },
            changeAction(newVal, oldVal) {
                const action = newVal || oldVal
                if (action === this.prevAction) return;
                this.prevAction = action;
                try {
                    this.ani && this.ani[action]()
                } catch (e) {
                    //TODO handle the exception
                }
            },
            init(vals) {
                if (!vals || (!vals.path && !vals.animationData)) return;
                this.ani && this.ani.destroy();
                this.$nextTick(() => {
                    this.ani = lottie.loadAnimation({
                        // #ifndef H5
                        container: document.getElementById('lottieRef'),
                        // #endif
                        // #ifdef H5
                        container: this.$refs.lottieRef.$el,
                        // #endif
                        renderer: 'svg',
                        loop: vals.loop === undefined ? true : vals.loop,
                        autoplay: vals.autoplay === undefined ? true : vals.autoplay,
                        path: vals.path,
                        // 只能加载本地json,优先级高于path
                        animationData: vals.animationData
                    });
                })
            }
        },
        mounted() {
            setTimeout(() => {
                this.init(this.options)
                this.isInit = true;
            }, 200)
        }
    }
</script>
<!-- #endif -->
 
<script>
    // #ifdef MP
    import lottieMp from './fui-lottie-mp.js'
    // #endif
    export default {
        name: "fui-lottie",
        props: {
            width: {
                type: [Number, String],
                default: 600
            },
            height: {
                type: [Number, String],
                default: 400
            },
            options: {
                type: Object,
                default () {
                    return {
                        path: '',
                        animationData: '',
                        autoplay: true,
                        loop: true
                    }
                }
            },
            //动画操作,可取值 play、pause、stop、destroy
            action: {
                type: String,
                default: 'play'
            }
        },
        watch: {
            // #ifdef MP
            actionVal() {
                this.changeMpAction()
            },
            options() {
                if (!this.mpInitFlag) return;
                this.initMp();
            },
            // #endif
            action(val) {
                this.actionVal = this.getAction(this.action)
            }
        },
        data() {
            const canvasId = `fui_lt_${Math.ceil(Math.random() * 10e5).toString(36)}`
            return {
                canvasId,
                // #ifdef MP
                lottieW: 300,
                lottieH: 200,
                // #endif
                //可取值 play、pause、stop、destroy
                actionVal: ''
            };
        },
        // #ifdef VUE2
        beforeDestroy() {
            this.actionVal = 'destroy'
        },
        // #endif
        // #ifdef VUE3
        beforeUnmount() {
            this.actionVal = 'destroy'
        },
        // #endif
        // #ifdef MP
        created() {
            this.aniMp = null
            this.mpInitFlag = false
            this.lottieW = uni.upx2px(Number(this.width))
            this.lottieH = uni.upx2px(Number(this.height))
        },
        // #endif
        mounted() {
            this.actionVal = this.getAction(this.action)
            // #ifdef MP
            this.$nextTick(() => {
                setTimeout(() => {
                    this.initMp()
                }, 50);
            })
            // #endif
        },
        methods: {
            // #ifdef MP
            changeMpAction() {
                try {
                    this.aniMp && this.aniMp[this.actionVal]()
                } catch (e) {
                    //TODO handle the exception
                }
            },
            initMp() {
                const options = {
                    ...this.options
                }
                if (!options.path && !options.animationData) return;
                this.aniMp && this.aniMp.destroy();
                this.$nextTick(() => {
                    const query = uni.createSelectorQuery()
                        // #ifndef MP-ALIPAY
                        .in(this)
                    // #endif
                    // #ifndef MP-QQ
                    query.selectAll(`.${this.canvasId}`).node(res => {
                        const canvas = res[0].node;
                        const context = canvas.getContext('2d');
                        const dpr = uni.getSystemInfoSync().pixelRatio;
                        canvas.width = this.lottieW * dpr;
                        canvas.height = this.lottieH * dpr;
                        context.scale(dpr, dpr);
                        lottieMp.setup(canvas)
 
                        this.aniMp = lottieMp.loadAnimation({
                            loop: options.loop === undefined ? true : options.loop,
                            autoplay: options.autoplay === undefined ? true : options.autoplay,
                            //只支持网络地址
                            path: options.path,
                            animationData: options.animationData,
                            rendererSettings: {
                                context,
                            },
                        })
                        this.mpInitFlag = true
                    }).exec()
                    // #endif
                });
            },
            // #endif
            getAction(action) {
                const actions = ['play', 'pause', 'stop', 'destroy']
                let val = 'play'
                if (~actions.indexOf(action)) {
                    val = action
                }
                return val
            }
        }
    }
</script>
 
<style scoped>
    /* #ifndef APP-NVUE */
    .fui-lottie__wrap {
        display: inline-flex;
    }
 
    /* #endif */
</style>