canvas.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <!-- 新的:接口对其了H5 -->
  3. <canvas v-if="isUseNewCanvas" type="2d" class="ec-canvas" canvas-id="{canvasId " bindinit="init" bindtouchstart="ec.disableTouch ? '' : 'touchStart'" bindtouchmove=" ec.disableTouch ? '' : 'touchMove' " bindtouchend="ec.disableTouch ? '' : 'touchEnd' "></canvas>
  4. <!-- 旧的 -->
  5. <canvas v-else class="ec-canvas" canvas-id=" canvasId " bindinit="init" bindtouchstart=" ec.disableTouch ? '' : 'touchStart' " bindtouchmove=" ec.disableTouch ? '' : 'touchMove' " bindtouchend=" ec.disableTouch ? '' : 'touchEnd' "></canvas>
  6. </template>
  7. <script>
  8. import WxCanvas from './wx-canvas';
  9. import * as echarts from './echarts.min';
  10. let ctx;
  11. function compareVersion(v1, v2) {
  12. v1 = v1.split('.')
  13. v2 = v2.split('.')
  14. const len = Math.max(v1.length, v2.length)
  15. while (v1.length < len) {
  16. v1.push('0')
  17. }
  18. while (v2.length < len) {
  19. v2.push('0')
  20. }
  21. for (let i = 0; i < len; i++) {
  22. const num1 = parseInt(v1[i])
  23. const num2 = parseInt(v2[i])
  24. if (num1 > num2) {
  25. return 1
  26. } else if (num1 < num2) {
  27. return -1
  28. }
  29. }
  30. return 0
  31. }
  32. Component({
  33. properties: {
  34. canvasId: {
  35. type: String,
  36. value: 'ec-canvas'
  37. },
  38. ec: {
  39. type: Object
  40. },
  41. forceUseOldCanvas: {
  42. type: Boolean,
  43. value: false
  44. }
  45. },
  46. data: {
  47. isUseNewCanvas: false
  48. },
  49. ready: function () {
  50. if (!this.data.ec) {
  51. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
  52. + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  53. return;
  54. }
  55. if (!this.data.ec.lazyLoad) {
  56. this.init();
  57. }
  58. },
  59. methods: {
  60. init: function (callback) {
  61. const version = uni.getSystemInfoSync().SDKVersion
  62. const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
  63. const forceUseOldCanvas = this.data.forceUseOldCanvas;
  64. const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
  65. this.setData({ isUseNewCanvas });
  66. if (forceUseOldCanvas && canUseNewCanvas) {
  67. console.warn('开发者强制使用旧canvas,建议关闭');
  68. }
  69. if (isUseNewCanvas) {
  70. // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
  71. // 2.9.0 可以使用 <canvas type="2d"></canvas>
  72. this.initByNewWay(callback);
  73. } else {
  74. const isValid = compareVersion(version, '1.9.91') >= 0
  75. if (!isValid) {
  76. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  77. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  78. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  79. return;
  80. } else {
  81. console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
  82. this.initByOldWay(callback);
  83. }
  84. }
  85. },
  86. initByOldWay(callback) {
  87. // 1.9.91 <= version < 2.9.0:原来的方式初始化
  88. ctx = uni.createCanvasContext(this.data.canvasId, this);
  89. const canvas = new WxCanvas(ctx, this.data.canvasId, false);
  90. echarts.setCanvasCreator(() => {
  91. return canvas;
  92. });
  93. // const canvasDpr = uni.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
  94. const canvasDpr = 1
  95. var query = uni.createSelectorQuery().in(this);
  96. query.select('.ec-canvas').boundingClientRect(res => {
  97. if (typeof callback === 'function') {
  98. this.chart = callback(canvas, res.width, res.height, canvasDpr);
  99. }
  100. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  101. this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
  102. }
  103. else {
  104. this.triggerEvent('init', {
  105. canvas: canvas,
  106. width: res.width,
  107. height: res.height,
  108. canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
  109. });
  110. }
  111. }).exec();
  112. },
  113. initByNewWay(callback) {
  114. // version >= 2.9.0:使用新的方式初始化
  115. const query = uni.createSelectorQuery().in(this)
  116. query
  117. .select('.ec-canvas')
  118. .fields({ node: true, size: true })
  119. .exec(res => {
  120. const canvasNode = res[0].node
  121. this.canvasNode = canvasNode
  122. const canvasDpr = uni.getSystemInfoSync().pixelRatio
  123. const canvasWidth = res[0].width
  124. const canvasHeight = res[0].height
  125. const ctx = canvasNode.getContext('2d')
  126. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
  127. echarts.setCanvasCreator(() => {
  128. return canvas
  129. })
  130. if (typeof callback === 'function') {
  131. this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
  132. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  133. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
  134. } else {
  135. this.triggerEvent('init', {
  136. canvas: canvas,
  137. width: canvasWidth,
  138. height: canvasHeight,
  139. dpr: canvasDpr
  140. })
  141. }
  142. })
  143. },
  144. canvasToTempFilePath(opt) {
  145. if (this.data.isUseNewCanvas) {
  146. // 新版
  147. const query = uni.createSelectorQuery().in(this)
  148. query
  149. .select('.ec-canvas')
  150. .fields({ node: true, size: true })
  151. .exec(res => {
  152. const canvasNode = res[0].node
  153. opt.canvas = canvasNode
  154. uni.canvasToTempFilePath(opt)
  155. })
  156. } else {
  157. // 旧的
  158. if (!opt.canvasId) {
  159. opt.canvasId = this.data.canvasId;
  160. }
  161. ctx.draw(true, () => {
  162. uni.canvasToTempFilePath(opt, this);
  163. });
  164. }
  165. },
  166. touchStart(e) {
  167. if (this.chart && e.touches.length > 0) {
  168. var touch = e.touches[0];
  169. var handler = this.chart.getZr().handler;
  170. handler.dispatch('mousedown', {
  171. zrX: touch.x,
  172. zrY: touch.y
  173. });
  174. handler.dispatch('mousemove', {
  175. zrX: touch.x,
  176. zrY: touch.y
  177. });
  178. handler.processGesture(wrapTouch(e), 'start');
  179. }
  180. },
  181. touchMove(e) {
  182. if (this.chart && e.touches.length > 0) {
  183. var touch = e.touches[0];
  184. var handler = this.chart.getZr().handler;
  185. handler.dispatch('mousemove', {
  186. zrX: touch.x,
  187. zrY: touch.y
  188. });
  189. handler.processGesture(wrapTouch(e), 'change');
  190. }
  191. },
  192. touchEnd(e) {
  193. if (this.chart) {
  194. const touch = e.changedTouches ? e.changedTouches[0] : {};
  195. var handler = this.chart.getZr().handler;
  196. handler.dispatch('mouseup', {
  197. zrX: touch.x,
  198. zrY: touch.y
  199. });
  200. handler.dispatch('click', {
  201. zrX: touch.x,
  202. zrY: touch.y
  203. });
  204. handler.processGesture(wrapTouch(e), 'end');
  205. }
  206. }
  207. }
  208. });
  209. function wrapTouch(event) {
  210. for (let i = 0; i < event.touches.length; ++i) {
  211. const touch = event.touches[i];
  212. touch.offsetX = touch.x;
  213. touch.offsetY = touch.y;
  214. }
  215. return event;
  216. }
  217. </script>
  218. <style>
  219. </style>