|
1 | | -<template> |
2 | | - <canvas |
3 | | - v-if="canvasId" |
4 | | - class="ec-canvas" |
5 | | - :id="canvasId" |
6 | | - :canvasId="canvasId" |
7 | | - @touchstart="touchStart" |
8 | | - @touchmove="touchMove" |
9 | | - @touchend="touchEnd" |
10 | | - @error="error"> |
11 | | - </canvas> |
12 | | -</template> |
13 | | - |
14 | | -<script> |
15 | | -import WxCanvas from './wx-canvas'; |
16 | | -
|
17 | | -export default { |
18 | | - props: { |
19 | | - onInit: { |
20 | | - required: true, |
21 | | - type: Function, |
22 | | - default: null, |
23 | | - }, |
24 | | - canvasId: { |
25 | | - type: String, |
26 | | - default: 'ec-canvas', |
27 | | - }, |
28 | | - lazyLoad: { |
29 | | - type: Boolean, |
30 | | - default: false, |
31 | | - }, |
32 | | - disableTouch: { |
33 | | - type: Boolean, |
34 | | - default: false, |
35 | | - }, |
36 | | - throttleTouch: { |
37 | | - type: Boolean, |
38 | | - default: false, |
39 | | - }, |
40 | | - }, |
41 | | - // #ifdef H5 |
42 | | - mounted() { |
43 | | - if (!this.lazyLoad) this.init(); |
44 | | - }, |
45 | | - // #endif |
46 | | - // #ifndef H5 |
47 | | - onReady() { |
48 | | - if (!this.lazyLoad) this.init(); |
49 | | - }, |
50 | | - // #endif |
51 | | - methods: { |
52 | | - init() { |
53 | | - if (!this.onInit) { |
54 | | - console.warn('请传入 onInit 函数进行初始化'); |
55 | | - return; |
56 | | - } |
57 | | -
|
58 | | - const { canvasId } = this; |
59 | | - this.ctx = wx.createCanvasContext(canvasId, this); |
60 | | -
|
61 | | - const canvas = new WxCanvas(this.ctx, canvasId); |
62 | | -
|
63 | | - const query = wx.createSelectorQuery().in(this); |
64 | | - query.select(`#${canvasId}`).boundingClientRect((res) => { |
65 | | - if (!res) { |
66 | | - setTimeout(() => this.init(), 50); |
67 | | - return; |
68 | | - } |
69 | | - this.chart = this.onInit(canvas, res.width, res.height); |
70 | | - }).exec(); |
71 | | - }, |
72 | | - canvasToTempFilePath(opt) { |
73 | | - const { canvasId } = this; |
74 | | - this.ctx.draw(true, () => { |
75 | | - wx.canvasToTempFilePath({ |
76 | | - canvasId, |
77 | | - ...opt, |
78 | | - }); |
79 | | - }); |
80 | | - }, |
81 | | - touchStart(e) { |
82 | | - const { disableTouch, chart } = this; |
83 | | - if (disableTouch || !chart || !e.mp.touches.length) return; |
84 | | - const touch = e.mp.touches[0]; |
85 | | - chart._zr.handler.dispatch('mousedown', { |
86 | | - zrX: touch.x, |
87 | | - zrY: touch.y, |
88 | | - }); |
89 | | - chart._zr.handler.dispatch('mousemove', { |
90 | | - zrX: touch.x, |
91 | | - zrY: touch.y, |
92 | | - }); |
93 | | - }, |
94 | | - touchMove(e) { |
95 | | - const { |
96 | | - disableTouch, throttleTouch, chart, lastMoveTime, |
97 | | - } = this; |
98 | | - if (disableTouch || !chart || !e.mp.touches.length) return; |
99 | | -
|
100 | | - if (throttleTouch) { |
101 | | - const currMoveTime = Date.now(); |
102 | | - if (currMoveTime - lastMoveTime < 240) return; |
103 | | - this.lastMoveTime = currMoveTime; |
104 | | - } |
105 | | -
|
106 | | - const touch = e.mp.touches[0]; |
107 | | - chart._zr.handler.dispatch('mousemove', { |
108 | | - zrX: touch.x, |
109 | | - zrY: touch.y, |
110 | | - }); |
111 | | - }, |
112 | | - touchEnd(e) { |
113 | | - const { disableTouch, chart } = this; |
114 | | - if (disableTouch || !chart) return; |
115 | | - const touch = e.mp.changedTouches ? e.mp.changedTouches[0] : {}; |
116 | | - chart._zr.handler.dispatch('mouseup', { |
117 | | - zrX: touch.x, |
118 | | - zrY: touch.y, |
119 | | - }); |
120 | | - chart._zr.handler.dispatch('click', { |
121 | | - zrX: touch.x, |
122 | | - zrY: touch.y, |
123 | | - }); |
124 | | - }, |
125 | | - }, |
126 | | -}; |
127 | | -</script> |
128 | | - |
129 | | -<style scoped> |
130 | | -.ec-canvas { |
131 | | - width: 100%; |
132 | | - height: 100%; |
133 | | - flex: 1; |
134 | | -} |
| 1 | +<template> |
| 2 | + <canvas v-if="canvasId" class="ec-canvas" :id="canvasId" :canvasId="canvasId" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error"></canvas> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script> |
| 6 | +import WxCanvas from './wx-canvas'; |
| 7 | +
|
| 8 | +export default { |
| 9 | + props: { |
| 10 | + // onInit: { |
| 11 | + // required: true, |
| 12 | + // type: Function, |
| 13 | + // default: null, |
| 14 | + // }, |
| 15 | + canvasId: { |
| 16 | + type: String, |
| 17 | + default: 'ec-canvas' |
| 18 | + }, |
| 19 | + lazyLoad: { |
| 20 | + type: Boolean, |
| 21 | + default: false |
| 22 | + }, |
| 23 | + disableTouch: { |
| 24 | + type: Boolean, |
| 25 | + default: false |
| 26 | + }, |
| 27 | + throttleTouch: { |
| 28 | + type: Boolean, |
| 29 | + default: false |
| 30 | + } |
| 31 | + }, |
| 32 | + // #ifdef H5 |
| 33 | + mounted() { |
| 34 | + if (!this.lazyLoad) this.init(); |
| 35 | + }, |
| 36 | + // #endif |
| 37 | + // #ifndef H5 |
| 38 | + onReady() { |
| 39 | + if (!this.lazyLoad) this.init(); |
| 40 | + }, |
| 41 | + // #endif |
| 42 | + methods: { |
| 43 | + init() { |
| 44 | + // if (!this.onInit) { |
| 45 | + // console.warn('请传入 onInit 函数进行初始化'); |
| 46 | + // return; |
| 47 | + // } |
| 48 | + const { canvasId } = this; |
| 49 | + this.ctx = wx.createCanvasContext(canvasId, this); |
| 50 | +
|
| 51 | + const canvas = new WxCanvas(this.ctx, canvasId); |
| 52 | +
|
| 53 | + const query = wx.createSelectorQuery().in(this); |
| 54 | + query |
| 55 | + .select(`#${canvasId}`) |
| 56 | + .boundingClientRect(res => { |
| 57 | + if (!res) { |
| 58 | + setTimeout(() => this.init(), 50); |
| 59 | + return; |
| 60 | + } |
| 61 | + this.$emit('onInit', { |
| 62 | + canvas, |
| 63 | + width: res.width, |
| 64 | + height: res.height, |
| 65 | + fire: chart => { |
| 66 | + console.log(chart); |
| 67 | + this.chart = chart; |
| 68 | + } |
| 69 | + }); |
| 70 | + // this.chart = this.onInit(canvas, res.width, res.height); |
| 71 | + }) |
| 72 | + .exec(); |
| 73 | + }, |
| 74 | + canvasToTempFilePath(opt) { |
| 75 | + const { canvasId } = this; |
| 76 | + this.ctx.draw(true, () => { |
| 77 | + wx.canvasToTempFilePath({ |
| 78 | + canvasId, |
| 79 | + ...opt |
| 80 | + }); |
| 81 | + }); |
| 82 | + }, |
| 83 | + touchStart(e) { |
| 84 | + const { disableTouch, chart } = this; |
| 85 | + if (disableTouch || !chart || !e.mp.touches.length) return; |
| 86 | + const touch = e.mp.touches[0]; |
| 87 | + chart._zr.handler.dispatch('mousedown', { |
| 88 | + zrX: touch.x, |
| 89 | + zrY: touch.y |
| 90 | + }); |
| 91 | + chart._zr.handler.dispatch('mousemove', { |
| 92 | + zrX: touch.x, |
| 93 | + zrY: touch.y |
| 94 | + }); |
| 95 | + }, |
| 96 | + touchMove(e) { |
| 97 | + const { disableTouch, throttleTouch, chart, lastMoveTime } = this; |
| 98 | + if (disableTouch || !chart || !e.mp.touches.length) return; |
| 99 | +
|
| 100 | + if (throttleTouch) { |
| 101 | + const currMoveTime = Date.now(); |
| 102 | + if (currMoveTime - lastMoveTime < 240) return; |
| 103 | + this.lastMoveTime = currMoveTime; |
| 104 | + } |
| 105 | +
|
| 106 | + const touch = e.mp.touches[0]; |
| 107 | + chart._zr.handler.dispatch('mousemove', { |
| 108 | + zrX: touch.x, |
| 109 | + zrY: touch.y |
| 110 | + }); |
| 111 | + }, |
| 112 | + touchEnd(e) { |
| 113 | + const { disableTouch, chart } = this; |
| 114 | + if (disableTouch || !chart) return; |
| 115 | + const touch = e.mp.changedTouches ? e.mp.changedTouches[0] : {}; |
| 116 | + chart._zr.handler.dispatch('mouseup', { |
| 117 | + zrX: touch.x, |
| 118 | + zrY: touch.y |
| 119 | + }); |
| 120 | + chart._zr.handler.dispatch('click', { |
| 121 | + zrX: touch.x, |
| 122 | + zrY: touch.y |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | +}; |
| 127 | +</script> |
| 128 | + |
| 129 | +<style scoped> |
| 130 | +.ec-canvas { |
| 131 | + width: 100%; |
| 132 | + height: 100%; |
| 133 | + flex: 1; |
| 134 | +} |
135 | 135 | </style> |
0 commit comments