wxcanvas.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. export default class WxCanvas {
  2. constructor(ctx, canvasId, isNew, canvasNode) {
  3. this.ctx = ctx;
  4. this.canvasId = canvasId;
  5. this.chart = null;
  6. this.isNew = isNew
  7. if (isNew) {
  8. this.canvasNode = canvasNode;
  9. }
  10. else {
  11. this._initStyle(ctx);
  12. }
  13. this._initEvent();
  14. }
  15. getContext(contextType) {
  16. if (contextType === '2d') {
  17. return this.ctx;
  18. }
  19. }
  20. setChart(chart) {
  21. this.chart = chart;
  22. }
  23. attachEvent() {
  24. // noop
  25. }
  26. detachEvent() {
  27. // noop
  28. }
  29. addEventListener() {
  30. // noop
  31. }
  32. removeEventListener() {
  33. // noop
  34. }
  35. getAttribute () {
  36. }
  37. _initCanvas(zrender, ctx) {
  38. zrender.util.getContext = function () {
  39. return ctx;
  40. };
  41. zrender.util.$override('measureText', function (text, font) {
  42. ctx.font = font || '12px sans-serif';
  43. return ctx.measureText(text);
  44. });
  45. }
  46. _initStyle(ctx) {
  47. ctx.createRadialGradient = () => {
  48. return ctx.createCircularGradient(arguments);
  49. };
  50. }
  51. _initEvent() {
  52. this.event = {};
  53. const eventNames = [{
  54. wxName: 'touchStart',
  55. ecName: 'mousedown'
  56. }, {
  57. wxName: 'touchMove',
  58. ecName: 'mousemove'
  59. }, {
  60. wxName: 'touchEnd',
  61. ecName: 'mouseup'
  62. }, {
  63. wxName: 'touchEnd',
  64. ecName: 'click'
  65. }];
  66. eventNames.forEach(name => {
  67. this.event[name.wxName] = e => {
  68. const touch = e.touches[0];
  69. this.chart.getZr().handler.dispatch(name.ecName, {
  70. zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
  71. zrY: name.wxName === 'tap' ? touch.clientY : touch.y
  72. });
  73. };
  74. });
  75. }
  76. set width(w) {
  77. if (this.canvasNode) this.canvasNode.width = w
  78. }
  79. set height(h) {
  80. if (this.canvasNode) this.canvasNode.height = h
  81. }
  82. get width() {
  83. if (this.canvasNode)
  84. return this.canvasNode.width
  85. return 0
  86. }
  87. get height() {
  88. if (this.canvasNode)
  89. return this.canvasNode.height
  90. return 0
  91. }
  92. }