jmgraph 3.1.91 → 3.1.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,7 +45,7 @@ export default class jmGraph extends jmControl {
45
45
 
46
46
  //如果是小程序
47
47
  if(typeof wx != 'undefined' && wx.canIUse && wx.canIUse('canvas')) {
48
- canvas = wx.createSelectorQuery().select('#' + canvas);
48
+ if(typeof canvas === 'string') canvas = wx.createSelectorQuery().select('#' + canvas);
49
49
  this.isWXMiniApp = true;// 微信小程序平台
50
50
  }
51
51
  else {
@@ -67,9 +67,9 @@ export default class jmGraph extends jmControl {
67
67
  else {
68
68
  this.container = canvas.parentElement;
69
69
  }
70
- }
70
+ }
71
+ this.canvas = canvas;
71
72
  if(!this.context) this.context = canvas.getContext('2d');
72
- this.canvas = canvas;
73
73
  this.__init(callback);
74
74
  }
75
75
 
@@ -120,19 +120,28 @@ export default class jmGraph extends jmControl {
120
120
  // 重置canvas大小,并判断高清屏,画图先放大二倍
121
121
  resize(w, h) {
122
122
 
123
- const scale = typeof window != 'undefined' && window.devicePixelRatio > 1? window.devicePixelRatio : 1;
123
+ let scale = typeof window != 'undefined' && window.devicePixelRatio > 1? window.devicePixelRatio : 1;
124
+ if(this.isWXMiniApp) {
125
+ scale = wx.getSystemInfoSync().pixelRatio || 1;
126
+ }
124
127
  if (scale > 1) {
125
128
  this.__normalSize = this.__normalSize || { width: 0, height: 0};
126
129
  w = w || this.__normalSize.width || this.width, h = h || this.__normalSize.height || this.height;
127
130
 
128
131
  if(w) this.__normalSize.width = w;
129
132
  if(h) this.__normalSize.height = h;
130
-
131
- this.canvas.style.width = w + "px";
132
- this.canvas.style.height = h + "px";
133
- this.canvas.height = h * scale;
134
- this.canvas.width = w *scale;
135
- this.context.scale(scale, scale);
133
+
134
+ if(this.canvas.style) {
135
+ this.canvas.style.width = w + "px";
136
+ this.canvas.style.height = h + "px";
137
+ this.canvas.height = h * scale;
138
+ this.canvas.width = w *scale;
139
+ this.context.scale(scale, scale);
140
+ }
141
+ else {
142
+ this.canvas.height = h;
143
+ this.canvas.width = w;
144
+ }
136
145
  this.devicePixelRatio = scale;
137
146
  }
138
147
  }
@@ -85,39 +85,21 @@ export default class jmImage extends jmControl {
85
85
  */
86
86
  draw() {
87
87
  try {
88
- let bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
89
- if(!bounds) bounds = this.parent && this.parent.getAbsoluteBounds?this.parent.getAbsoluteBounds():this.getAbsoluteBounds();
90
- let p = this.getLocation();
91
- p.left += bounds.left;
92
- p.top += bounds.top;
93
88
 
94
- let sp = this.sourcePosition;
95
- let sw = this.sourceWidth;
96
- let sh = this.sourceHeight;
97
89
  let img = this.getImage();
98
-
99
- if(sp || typeof sw != 'undefined' || typeof sh != 'undefined') {
100
- if(typeof sw == 'undefined') sw= p.width || img.width || 0;
101
- if(typeof sh == 'undefined') sh= p.height || img.height || 0;
102
- sp = sp || {x:0, y:0};
103
-
104
- if(p.width && p.height) this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,p.width,p.height);
105
- else if(p.width) {
106
- this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,p.width,sh);
107
- }
108
- else if(p.height) {
109
- this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,sw,p.height);
110
- }
111
- else this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,sw,sh);
112
- }
113
- else if(p) {
114
- if(p.width && p.height) this.context.drawImage(img,p.left,p.top,p.width,p.height);
115
- else if(p.width) this.context.drawImage(img,p.left,p.top,p.width,img.height);
116
- else if(p.height) this.context.drawImage(img,p.left,p.top,img.width,p.height);
117
- else this.context.drawImage(img,p.left,p.top);
90
+ if(this.graph.isWXMiniApp && this.graph.canvas) {
91
+ // 图片对象
92
+ const image = this.graph.canvas.createImage();
93
+ // 图片加载完成回调
94
+ image.onload = () => {
95
+ // 将图片绘制到 canvas 上
96
+ this.drawImg(image);
97
+ }
98
+ // 设置图片src
99
+ image.src = img;
118
100
  }
119
101
  else {
120
- this.context.drawImage(img);
102
+ this.drawImg(img);
121
103
  }
122
104
  }
123
105
  catch(e) {
@@ -125,6 +107,43 @@ export default class jmImage extends jmControl {
125
107
  }
126
108
  }
127
109
 
110
+ // 绘制
111
+ drawImg(img) {
112
+ let bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
113
+ if(!bounds) bounds = this.parent && this.parent.getAbsoluteBounds?this.parent.getAbsoluteBounds():this.getAbsoluteBounds();
114
+ let p = this.getLocation();
115
+ p.left += bounds.left;
116
+ p.top += bounds.top;
117
+
118
+ let sp = this.sourcePosition;
119
+ let sw = this.sourceWidth;
120
+ let sh = this.sourceHeight;
121
+
122
+ if(sp || typeof sw != 'undefined' || typeof sh != 'undefined') {
123
+ if(typeof sw == 'undefined') sw= p.width || img.width || 0;
124
+ if(typeof sh == 'undefined') sh= p.height || img.height || 0;
125
+ sp = sp || {x:0, y:0};
126
+
127
+ if(p.width && p.height) this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,p.width,p.height);
128
+ else if(p.width) {
129
+ this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,p.width,sh);
130
+ }
131
+ else if(p.height) {
132
+ this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,sw,p.height);
133
+ }
134
+ else this.context.drawImage(img,sp.x,sp.y,sw,sh,p.left,p.top,sw,sh);
135
+ }
136
+ else if(p) {
137
+ if(p.width && p.height) this.context.drawImage(img,p.left,p.top,p.width,p.height);
138
+ else if(p.width) this.context.drawImage(img,p.left,p.top,p.width,img.height);
139
+ else if(p.height) this.context.drawImage(img,p.left,p.top,img.width,p.height);
140
+ else this.context.drawImage(img,p.left,p.top);
141
+ }
142
+ else {
143
+ this.context.drawImage(img);
144
+ }
145
+ }
146
+
128
147
  /**
129
148
  * 获取当前控件的边界
130
149
  *
@@ -160,7 +179,7 @@ export default class jmImage extends jmControl {
160
179
  else if(src && src.src) {
161
180
  this.__img = src;
162
181
  }
163
- else if(document && document.createElement) {
182
+ else if(typeof document !== 'undefined' && document.createElement) {
164
183
  this.__img = document.createElement('img');
165
184
  if(src && typeof src == 'string') this.__img.src = src;
166
185
  }