f2.vue
2.24 KB
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
<template>
<canvas
:id="`f2-canvas-${$id}`"
:canvas-id="`f2-canvas-${$id}`"
class="f2-canvas"
:width="canvasWidth"
:height="canvasHeight"
@touch-start="touchStart"
@touch-move="touchMove"
@touch-end="touchEnd"
/>
</template>
<script>
import F2 from '@antv/f2';
import { my as my$1 } from '@antv/f2-context';
function wrapEvent(e) {
if (!e) return;
if (!e.preventDefault) {
e.preventDefault = function() {};
}
return e;
}
export default {
props: {
init: {
type: Function,
default: () => {}
},
width: Number,
height: Number,
},
data: function() {
return {
canvasEl: null,
canvasWidth: this.width,
canvasHeight: this.height,
};
},
mounted: function() {
const id = `f2-canvas-${this.$id}`;
const myCtx = uni.createCanvasContext(id, this);
const context = my$1(myCtx);
const query = uni.createSelectorQuery().in(this);
query
.select(`#${id}`)
.boundingClientRect()
.exec(res => {
// 获取画布实际宽高, 用props的宽高做失败兜底
const { width, height } = res && res[0] ? res[0] : this;
if (!width || !height) {
return;
}
const pixelRatio = uni.getSystemInfoSync().pixelRatio;
const ref = this;
this.canvasWidth = width * pixelRatio;
this.canvasHeight = height * pixelRatio;
this.$nextTick(() => {
// 高清解决方案
const chart = this.init(F2, { context, width, height, pixelRatio }, ref);
if (chart) {
this.canvasEl = chart.get('el');
}
});
});
},
methods: {
touchStart: function(e) {
const canvasEl = this.canvasEl;
if (!canvasEl) {
return;
}
canvasEl.dispatchEvent('touchstart', wrapEvent(e));
},
touchMove: function(e) {
const canvasEl = this.canvasEl;
if (!canvasEl) {
return;
}
canvasEl.dispatchEvent('touchmove', wrapEvent(e));
},
touchEnd: function(e) {
const canvasEl = this.canvasEl;
if (!canvasEl) {
return;
}
canvasEl.dispatchEvent('touchend', wrapEvent(e));
}
}
}
</script>
<style>
.f2-canvas {
width: 100%;
height: 100%;
}
</style>