wx-canvas.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. attachEvent() {
  31. // noop
  32. }
  33. detachEvent() {
  34. // noop
  35. }
  36. _initCanvas(zrender, ctx) {
  37. zrender.util.getContext = function () {
  38. return ctx;
  39. };
  40. zrender.util.$override('measureText', function (text, font) {
  41. ctx.font = font || '12px sans-serif';
  42. return ctx.measureText(text);
  43. });
  44. }
  45. _initStyle(ctx) {
  46. var styles = ['fillStyle', 'strokeStyle', 'globalAlpha',
  47. 'textAlign', 'textBaseAlign', 'shadow', 'lineWidth',
  48. 'lineCap', 'lineJoin', 'lineDash', 'miterLimit', 'fontSize'];
  49. styles.forEach(style => {
  50. Object.defineProperty(ctx, style, {
  51. set: value => {
  52. if (style !== 'fillStyle' && style !== 'strokeStyle'
  53. || value !== 'none' && value !== null
  54. ) {
  55. ctx['set' + style.charAt(0).toUpperCase() + style.slice(1)](value);
  56. }
  57. }
  58. });
  59. });
  60. ctx.createRadialGradient = () => {
  61. return ctx.createCircularGradient(arguments);
  62. };
  63. }
  64. _initEvent() {
  65. this.event = {};
  66. const eventNames = [{
  67. wxName: 'touchStart',
  68. ecName: 'mousedown'
  69. }, {
  70. wxName: 'touchMove',
  71. ecName: 'mousemove'
  72. }, {
  73. wxName: 'touchEnd',
  74. ecName: 'mouseup'
  75. }, {
  76. wxName: 'touchEnd',
  77. ecName: 'click'
  78. }];
  79. eventNames.forEach(name => {
  80. this.event[name.wxName] = e => {
  81. const touch = e.touches[0];
  82. this.chart.getZr().handler.dispatch(name.ecName, {
  83. zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
  84. zrY: name.wxName === 'tap' ? touch.clientY : touch.y
  85. });
  86. };
  87. });
  88. }
  89. set width(w) {
  90. if (this.canvasNode) this.canvasNode.width = w
  91. }
  92. set height(h) {
  93. if (this.canvasNode) this.canvasNode.height = h
  94. }
  95. get width() {
  96. if (this.canvasNode)
  97. return this.canvasNode.width
  98. return 0
  99. }
  100. get height() {
  101. if (this.canvasNode)
  102. return this.canvasNode.height
  103. return 0
  104. }
  105. }