|
@@ -0,0 +1,87 @@
|
|
1
|
+<template>
|
|
2
|
+ <canvas ref="cvsRef"></canvas>
|
|
3
|
+</template>
|
|
4
|
+
|
|
5
|
+<script setup>
|
|
6
|
+ import { onMounted, ref } from 'vue';
|
|
7
|
+ import indexImage from '@/assets/images/1.jpg';
|
|
8
|
+ import maskImage from '@/assets/cloud-texture.png';
|
|
9
|
+
|
|
10
|
+ // const props = defineProps({
|
|
11
|
+ // img: {
|
|
12
|
+ // type: Object,
|
|
13
|
+ // default: null,
|
|
14
|
+ // }
|
|
15
|
+ // })
|
|
16
|
+
|
|
17
|
+ const emit = defineEmits(['end']);
|
|
18
|
+
|
|
19
|
+ const cvsRef = ref();
|
|
20
|
+ const ctxRef = ref();
|
|
21
|
+ const img = new Image();
|
|
22
|
+ img.src = indexImage;
|
|
23
|
+ img.onload = function() {
|
|
24
|
+ const canv = cvsRef.value;
|
|
25
|
+ canv.width = img.naturalWidth;
|
|
26
|
+ canv.height = img.naturalHeight;
|
|
27
|
+ }
|
|
28
|
+ const imgMask = new Image();
|
|
29
|
+ imgMask.src = maskImage;
|
|
30
|
+
|
|
31
|
+ let i = 0;
|
|
32
|
+ let animationId = 0;
|
|
33
|
+
|
|
34
|
+ const xStart = 60;
|
|
35
|
+ const yStart = 90;
|
|
36
|
+ const speed = 20;
|
|
37
|
+ const draw = () => {
|
|
38
|
+ i += speed;
|
|
39
|
+ const canv = cvsRef.value;
|
|
40
|
+ const maskX = (canv.width - (xStart + i)) / 2;
|
|
41
|
+ const maskY = (canv.height - (yStart + i)) / 2;
|
|
42
|
+ const maskWidth = xStart + i;
|
|
43
|
+ const maskHeight = yStart + i;
|
|
44
|
+
|
|
45
|
+ const ctx = ctxRef.value;
|
|
46
|
+ ctx.clearRect(0, 0, canv.width, canv.height);
|
|
47
|
+ ctx.globalCompositeOperation = "source-over";
|
|
48
|
+
|
|
49
|
+ ctx.drawImage(imgMask, maskX, maskY, maskWidth, maskHeight);
|
|
50
|
+
|
|
51
|
+ ctx.globalCompositeOperation = "source-in";
|
|
52
|
+ ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
|
|
53
|
+ animationId = window.requestAnimationFrame(draw);
|
|
54
|
+
|
|
55
|
+ const { clientWidth, clientHeight } = document.body;
|
|
56
|
+ if ((maskWidth - clientWidth + maskX) > img.naturalWidth && (maskHeight - clientHeight + maskY) > img.naturalHeight) {
|
|
57
|
+ // 动画就结束
|
|
58
|
+ window.cancelAnimationFrame(animationId);
|
|
59
|
+ emit('end');
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ const start = () => {
|
|
64
|
+ i = 0;
|
|
65
|
+ draw();
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ onMounted(() => {
|
|
69
|
+ ctxRef.value = cvsRef.value.getContext("2d");
|
|
70
|
+ })
|
|
71
|
+
|
|
72
|
+ defineExpose({
|
|
73
|
+ start,
|
|
74
|
+ })
|
|
75
|
+
|
|
76
|
+</script>
|
|
77
|
+
|
|
78
|
+<style lang="less" scoped>
|
|
79
|
+canvas {
|
|
80
|
+ position: fixed;
|
|
81
|
+ width: 100vw;
|
|
82
|
+ height: 100vh;
|
|
83
|
+ left: 0;
|
|
84
|
+ top: 0;
|
|
85
|
+ z-index: 20;
|
|
86
|
+}
|
|
87
|
+</style>
|