储能智慧云小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

wx-canvas.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. {
  68. wxName: 'touchStart',
  69. ecName: 'mousedown'
  70. },
  71. {
  72. wxName: 'touchEnd',
  73. ecName: 'mouseup'
  74. }, {
  75. wxName: 'touchEnd',
  76. ecName: 'click'
  77. }];
  78. eventNames.forEach(name => {
  79. this.event[name.wxName] = e => {
  80. const touch = e.touches[0];
  81. this.chart.getZr().handler.dispatch(name.ecName, {
  82. zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
  83. zrY: name.wxName === 'tap' ? touch.clientY : touch.y
  84. });
  85. };
  86. });
  87. }
  88. set width(w) {
  89. if (this.canvasNode) this.canvasNode.width = w
  90. }
  91. set height(h) {
  92. if (this.canvasNode) this.canvasNode.height = h
  93. }
  94. get width() {
  95. if (this.canvasNode)
  96. return this.canvasNode.width
  97. return 0
  98. }
  99. get height() {
  100. if (this.canvasNode)
  101. return this.canvasNode.height
  102. return 0
  103. }
  104. }