wx-canvas.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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._initCanvas(zrender, ctx);
  14. this._initEvent();
  15. }
  16. getContext(contextType) {
  17. if (contextType === '2d') {
  18. return this.ctx;
  19. }
  20. }
  21. // canvasToTempFilePath(opt) {
  22. // if (!opt.canvasId) {
  23. // opt.canvasId = this.canvasId;
  24. // }
  25. // return wx.canvasToTempFilePath(opt, this);
  26. // }
  27. setChart(chart) {
  28. this.chart = chart;
  29. }
  30. addEventListener() {
  31. // noop
  32. }
  33. attachEvent() {
  34. // noop
  35. }
  36. detachEvent() {
  37. // noop
  38. }
  39. _initCanvas(zrender, ctx) {
  40. zrender.util.getContext = function () {
  41. return ctx;
  42. };
  43. zrender.util.$override('measureText', function (text, font) {
  44. ctx.font = font || '12px sans-serif';
  45. return ctx.measureText(text);
  46. });
  47. }
  48. _initStyle(ctx) {
  49. var styles = [
  50. "fillStyle",
  51. "strokeStyle",
  52. "globalAlpha",
  53. "textAlign",
  54. "textBaseAlign",
  55. "shadow",
  56. "lineWidth",
  57. "lineCap",
  58. "lineJoin",
  59. "lineDash",
  60. "miterLimit",
  61. "fontSize",
  62. ];
  63. styles.forEach((style) => {
  64. Object.defineProperty(ctx, style, {
  65. set: (value) => {
  66. if (
  67. (style !== "fillStyle" && style !== "strokeStyle") ||
  68. (value !== "none" && value !== null)
  69. ) {
  70. ctx["set" + style.charAt(0).toUpperCase() + style.slice(1)](value);
  71. }
  72. },
  73. });
  74. });
  75. ctx.createRadialGradient = () => {
  76. return ctx.createCircularGradient(arguments);
  77. };
  78. }
  79. _initEvent() {
  80. this.event = {};
  81. const eventNames = [{
  82. wxName: 'touchStart',
  83. ecName: 'mousedown'
  84. }, {
  85. wxName: 'touchMove',
  86. ecName: 'mousemove'
  87. }, {
  88. wxName: 'touchEnd',
  89. ecName: 'mouseup'
  90. }, {
  91. wxName: 'touchEnd',
  92. ecName: 'click'
  93. }];
  94. eventNames.forEach(name => {
  95. this.event[name.wxName] = e => {
  96. const touch = e.touches[0];
  97. this.chart.getZr().handler.dispatch(name.ecName, {
  98. zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
  99. zrY: name.wxName === 'tap' ? touch.clientY : touch.y,
  100. preventDefault: () => {},
  101. stopImmediatePropagation: () => {},
  102. stopPropagation: () => {}
  103. });
  104. };
  105. });
  106. }
  107. set width(w) {
  108. if (this.canvasNode) this.canvasNode.width = w
  109. }
  110. set height(h) {
  111. if (this.canvasNode) this.canvasNode.height = h
  112. }
  113. get width() {
  114. if (this.canvasNode)
  115. return this.canvasNode.width
  116. return 0
  117. }
  118. get height() {
  119. if (this.canvasNode)
  120. return this.canvasNode.height
  121. return 0
  122. }
  123. }