@antv/g 3.3.5 → 3.3.6

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.
package/lib/canvas.js CHANGED
@@ -193,22 +193,17 @@ Util.augment(Canvas, {
193
193
  }, false);
194
194
  },
195
195
  _scale: function _scale() {
196
- if (this._cfg.renderType !== 'svg') {
197
- var pixelRatio = this.get('pixelRatio');
198
- this.scale(pixelRatio, pixelRatio);
199
- }
196
+ var pixelRatio = this.get('pixelRatio');
197
+ this.scale(pixelRatio, pixelRatio);
200
198
  },
201
199
  _setGlobalParam: function _setGlobalParam() {
202
- var renderType = this.get('renderer') || 'canvas';
200
+ var pixelRatio = this.get('pixelRatio');
203
201
 
204
- if (renderType === 'svg') {
205
- this.set('pixelRatio', 1);
206
- } else if (!this.get('pixelRatio')) {
202
+ if (!pixelRatio) {
207
203
  this.set('pixelRatio', Util.getRatio());
208
204
  }
209
205
 
210
- this._cfg.renderType = renderType;
211
- var renderer = renderers[renderType];
206
+ var renderer = renderers[this.get('renderer') || 'canvas'];
212
207
  this._cfg.renderer = renderer;
213
208
  this._cfg.canvas = this;
214
209
  var timeline = new Timeline(this);
@@ -303,9 +298,6 @@ Util.augment(Canvas, {
303
298
 
304
299
  return Canvas.superclass.getShape.call(this, x, y);
305
300
  },
306
- getRenderer: function getRenderer() {
307
- return this._cfg.renderType;
308
- },
309
301
  _drawSync: function _drawSync() {
310
302
  this._cfg.painter.drawSync(this);
311
303
  },
@@ -0,0 +1,32 @@
1
+ var Util = require('../util/index');
2
+
3
+ var Event = require('../event');
4
+
5
+ var EventEmitter = require('./event-emitter');
6
+
7
+ var PROPOGATE_EVENTS = ['click', 'mousedown', 'mouseup', 'dblclick', 'contextmenu', 'mouseout', 'mouseover', 'mousemove', 'dragstart', 'drag', 'dragend', 'dragenter', 'dragleave', 'drop'];
8
+
9
+ var AdvancedEE = function AdvancedEE() {};
10
+
11
+ Util.augment(AdvancedEE, EventEmitter, {
12
+ emit: function emit(evt, e) {
13
+ var args = arguments;
14
+ EventEmitter.prototype.emit.apply(this, args);
15
+
16
+ if (args.length >= 2 && args[1] instanceof Event && args[1].propagationStopped) {
17
+ return;
18
+ }
19
+
20
+ if (PROPOGATE_EVENTS.indexOf(evt) >= 0 && e.target === this
21
+ /* prevent bubbling multiple times */
22
+ ) {
23
+ var shape = this._cfg.parent;
24
+
25
+ while (shape && !shape.get('destroyed')) {
26
+ shape.emit.apply(shape, args);
27
+ shape = shape._cfg.parent;
28
+ }
29
+ }
30
+ }
31
+ });
32
+ module.exports = AdvancedEE;
@@ -0,0 +1,116 @@
1
+ var Util = require('../util/common');
2
+
3
+ var slice = Array.prototype.slice;
4
+
5
+ function indexOfCallback(events, callback) {
6
+ var i = events.length;
7
+
8
+ while (i--) {
9
+ if (events[i].callback === callback) {
10
+ return i;
11
+ }
12
+ }
13
+
14
+ return -1;
15
+ }
16
+
17
+ var EventEmitter = function EventEmitter() {};
18
+
19
+ Util.augment(EventEmitter, {
20
+ on: function on(evt, callback, one) {
21
+ var self = this;
22
+
23
+ if (!Util.isFunction(callback)) {
24
+ throw new TypeError('listener should be a function');
25
+ }
26
+
27
+ if (!self._cfg._events) {
28
+ self._cfg._events = {};
29
+ }
30
+
31
+ if (!self._cfg._events[evt]) {
32
+ self._cfg._events[evt] = [];
33
+ }
34
+
35
+ self._cfg._events[evt].push({
36
+ callback: callback,
37
+ one: one
38
+ });
39
+
40
+ return this;
41
+ },
42
+ one: function one(evt, callback) {
43
+ this.on(evt, callback, true);
44
+ return this;
45
+ },
46
+ emit: function emit(evt) {
47
+ if (this.get('destroyed')) {
48
+ return;
49
+ }
50
+
51
+ if (!this._cfg._events || Util.isEmpty(this._cfg._events)) {
52
+ return;
53
+ }
54
+
55
+ var events = this._cfg._events[evt];
56
+
57
+ if (Util.isEmpty(events)) {
58
+ return;
59
+ }
60
+
61
+ var args = arguments;
62
+ var arg = slice.call(args, 1);
63
+ var length = events.length;
64
+
65
+ for (var i = 0; i < length;) {
66
+ events[i].callback.apply(this, arg);
67
+
68
+ if (events[i].one) {
69
+ events.splice(i, 1);
70
+ length--;
71
+ } else {
72
+ i++;
73
+ }
74
+ }
75
+ },
76
+ trigger: function trigger() {
77
+ this.emit.apply(this, arguments);
78
+ },
79
+ off: function off(evt, callback) {
80
+ var events = this._cfg._events;
81
+
82
+ if (!events || Util.isEmpty(events)) {
83
+ return;
84
+ }
85
+
86
+ if (arguments.length === 0) {
87
+ this._cfg._events = {};
88
+ return this;
89
+ }
90
+
91
+ if (events[evt]) {
92
+ var index = indexOfCallback(events[evt], callback);
93
+
94
+ if (index >= 0) {
95
+ events[evt].splice(index, 1);
96
+ }
97
+
98
+ if (events[evt].length === 0) {
99
+ delete events[evt];
100
+ }
101
+ }
102
+ },
103
+ removeEvent: function removeEvent(evt) {
104
+ if (typeof evt === 'undefined') {
105
+ this._cfg._events = {};
106
+ } else {
107
+ delete this._cfg._events[evt];
108
+ }
109
+
110
+ return this;
111
+ },
112
+ _getEvents: function _getEvents() {
113
+ return this._cfg._events || {};
114
+ }
115
+ });
116
+ module.exports = EventEmitter;
@@ -0,0 +1,149 @@
1
+ var Util = require('../util/index');
2
+
3
+ var Event = require('../event');
4
+
5
+ var EVENTS = ['click', 'mousedown', 'mouseup', 'dblclick', 'contextmenu', 'mouseenter', 'mouseout', 'mouseover', 'mousemove', 'mouseleave'];
6
+ var preShape = null;
7
+ var mousedown = null;
8
+ var dragging = null;
9
+ module.exports = {
10
+ registerEvent: function registerEvent() {
11
+ var self = this;
12
+ var el = this.get('el');
13
+ Util.each(EVENTS, function (evt) {
14
+ el.addEventListener(evt, function (e) {
15
+ self._triggerEvent(evt, e);
16
+ }, false);
17
+ }); // special cases
18
+
19
+ el.addEventListener('touchstart', function (e) {
20
+ if (!Util.isEmpty(e.touches)) {
21
+ self._triggerEvent('touchstart', e.touches[0]);
22
+ }
23
+ }, false);
24
+ el.addEventListener('touchmove', function (e) {
25
+ if (!Util.isEmpty(e.touches)) {
26
+ self._triggerEvent('touchmove', e.touches[0]);
27
+ }
28
+ }, false);
29
+ el.addEventListener('touchend', function (e) {
30
+ if (!Util.isEmpty(e.changedTouches)) {
31
+ self._triggerEvent('touchend', e.changedTouches[0]);
32
+ }
33
+ }, false);
34
+ },
35
+ _getEmitter: function _getEmitter(element, event) {
36
+ if (element) {
37
+ if (Util.isEmpty(element._getEvents())) {
38
+ var parent = element.get('parent');
39
+
40
+ if (parent && !event.propagationStopped) {
41
+ return this._getEmitter(parent, event);
42
+ }
43
+ } else {
44
+ return element;
45
+ }
46
+ }
47
+ },
48
+ _getEventObj: function _getEventObj(type, e, point, target) {
49
+ var event = new Event(type, e, true, true);
50
+ event.x = point.x;
51
+ event.y = point.y;
52
+ event.clientX = e.clientX;
53
+ event.clientY = e.clientY;
54
+ event.currentTarget = target;
55
+ event.target = target;
56
+ return event;
57
+ },
58
+ _triggerEvent: function _triggerEvent(type, e) {
59
+ var point = this.getPointByClient(e.clientX, e.clientY);
60
+ var shape = this.getShape(point.x, point.y, e);
61
+ var el = this.get('el'); // svg原生事件取不到dragover, dragout, drop等事件的对象。这边需要走数学拾取。
62
+
63
+ if (dragging && this.getRenderer() === 'svg') {
64
+ shape = this.getShape(point.x, point.y);
65
+ }
66
+
67
+ if (type === 'mousemove') {
68
+ if (preShape && preShape !== shape) {
69
+ this._emitEvent('mouseout', e, point, preShape);
70
+
71
+ this._emitEvent('mouseleave', e, point, preShape);
72
+
73
+ if (dragging) {
74
+ this._emitEvent('dragleave', e, point, preShape);
75
+ }
76
+
77
+ if (!preShape.destroyed && !preShape.removed) {
78
+ el.style.cursor = preShape.attr('cursor') || 'default';
79
+ }
80
+ } // 拖拽过程中不会触发mousemove事件
81
+
82
+
83
+ if (dragging) {
84
+ this._emitEvent('drag', e, point, dragging);
85
+ }
86
+
87
+ if (shape) {
88
+ if (!dragging) {
89
+ if (mousedown === shape) {
90
+ dragging = shape;
91
+ mousedown = null;
92
+
93
+ this._emitEvent('dragstart', e, point, shape);
94
+ } else {
95
+ this._emitEvent('mousemove', e, point, shape);
96
+ }
97
+ }
98
+
99
+ if (preShape !== shape) {
100
+ this._emitEvent('mouseenter', e, point, shape);
101
+
102
+ this._emitEvent('mouseover', e, point, shape);
103
+
104
+ if (dragging) {
105
+ this._emitEvent('dragenter', e, point, shape);
106
+ }
107
+ }
108
+ } else {
109
+ var canvasmousemove = this._getEventObj('mousemove', e, point, this);
110
+
111
+ this.emit('mousemove', canvasmousemove);
112
+ }
113
+
114
+ preShape = shape;
115
+ } else {
116
+ this._emitEvent(type, e, point, shape || this);
117
+
118
+ if (!dragging && type === 'mousedown') {
119
+ mousedown = shape;
120
+ }
121
+
122
+ if (type === 'mouseup') {
123
+ mousedown = null;
124
+
125
+ if (dragging) {
126
+ dragging._cfg.capture = true;
127
+
128
+ this._emitEvent('dragend', e, point, dragging);
129
+
130
+ dragging = null;
131
+
132
+ this._emitEvent('drop', e, point, shape || this);
133
+ }
134
+ }
135
+ }
136
+
137
+ if (shape && !shape.destroyed) {
138
+ el.style.cursor = shape.attr('cursor') || 'default';
139
+ }
140
+ },
141
+ _emitEvent: function _emitEvent(type, evt, point, shape) {
142
+ var event = this._getEventObj(type, evt, point, shape);
143
+
144
+ var emitShape = this._getEmitter(shape, evt);
145
+
146
+ emitShape && emitShape.emit(type, event);
147
+ return emitShape;
148
+ }
149
+ };
@@ -0,0 +1,185 @@
1
+ var Util = require('../../util/index');
2
+
3
+ var Event = require('../../event');
4
+
5
+ var EVENTS = ['mousedown', 'mouseup', 'dblclick', 'mouseenter', 'mouseout', 'mouseover', 'mousemove', 'mouseleave'];
6
+ var CLICK_OFFSET = 40;
7
+ var LEFT_BTN_CODE = 0;
8
+ var preShape = null;
9
+ var mousedownShape = null;
10
+ var mousedownOffset = {};
11
+ var dragging = null;
12
+ var dragTimer = null;
13
+ module.exports = {
14
+ registerEvent: function registerEvent() {
15
+ var self = this;
16
+ var el = this.get('el');
17
+ Util.each(EVENTS, function (evt) {
18
+ el.addEventListener(evt, function (e) {
19
+ self._triggerEvent(evt, e);
20
+ }, false);
21
+ }); // special cases
22
+
23
+ el.addEventListener('touchstart', function (e) {
24
+ if (!Util.isEmpty(e.touches)) {
25
+ self._triggerEvent('touchstart', e.touches[0]);
26
+ }
27
+ }, false);
28
+ el.addEventListener('touchmove', function (e) {
29
+ if (!Util.isEmpty(e.touches)) {
30
+ self._triggerEvent('touchmove', e.touches[0]);
31
+ }
32
+ }, false);
33
+ el.addEventListener('touchend', function (e) {
34
+ if (!Util.isEmpty(e.changedTouches)) {
35
+ self._triggerEvent('touchend', e.changedTouches[0]);
36
+ }
37
+ }, false);
38
+ el.addEventListener('contextmenu', function (e) {
39
+ self._triggerEvent('contextmenu', e);
40
+
41
+ e.preventDefault();
42
+ }, false);
43
+ },
44
+ _getEmitter: function _getEmitter(element, event) {
45
+ if (element) {
46
+ if (Util.isEmpty(element._getEvents())) {
47
+ var parent = element.get('parent');
48
+
49
+ if (parent && !event.propagationStopped) {
50
+ return this._getEmitter(parent, event);
51
+ }
52
+ } else {
53
+ return element;
54
+ }
55
+ }
56
+ },
57
+ _getEventObj: function _getEventObj(type, e, point, target) {
58
+ var event = new Event(type, e, true, true); // 事件的x,y应该是基于画布左上角的,与canvas的matrix无关
59
+
60
+ event.x = point.x;
61
+ event.y = point.y;
62
+ event.clientX = e.clientX;
63
+ event.clientY = e.clientY;
64
+ event.currentTarget = target;
65
+ event.target = target;
66
+ return event;
67
+ },
68
+ _triggerEvent: function _triggerEvent(type, e) {
69
+ var self = this;
70
+ var point = self.getPointByClient(e.clientX, e.clientY);
71
+ var shape = self.getShape(point.x, point.y, e);
72
+ var el = self.get('el'); // svg原生事件取不到dragover, dragout, drop等事件的对象。这边需要走数学拾取。
73
+
74
+ if (dragging && self.getRenderer() === 'svg') {
75
+ shape = self.getShape(point.x, point.y);
76
+ }
77
+
78
+ if (type === 'mousemove') {
79
+ if (preShape && preShape !== shape) {
80
+ self._emitEvent('mouseout', e, point, preShape);
81
+
82
+ self._emitEvent('mouseleave', e, point, preShape);
83
+
84
+ if (dragging) {
85
+ self._emitEvent('dragleave', e, point, preShape);
86
+ } // 当 mouseleave 触发时,如果拾取到其他 shape 的 mouseenter ,鼠标样式会正常
87
+ // 当鼠标移出 shape 但是移动到画布上时,没有shape,就不恢复样式。这里判断一下,直接重置
88
+
89
+
90
+ if (!shape || shape.destroyed) {
91
+ el.style.cursor = 'default';
92
+ }
93
+ }
94
+
95
+ if (dragging) {
96
+ self._emitEvent('drag', e, point, dragging);
97
+ /**
98
+ * H5原生事件中drag同时不会触发mousemove
99
+ * 但是在上层具有嵌套关系的item事件计算中需要用到drag时的mousemove
100
+ * 才能计算dragenter etc.
101
+ */
102
+
103
+
104
+ self._emitEvent('mousemove', e, point, shape);
105
+ }
106
+
107
+ if (shape) {
108
+ if (!dragging) {
109
+ if (mousedownShape === shape) {
110
+ dragging = shape;
111
+ mousedownShape = null;
112
+
113
+ this._emitEvent('dragstart', e, point, shape);
114
+ } else {
115
+ self._emitEvent('mousemove', e, point, shape);
116
+ }
117
+ }
118
+
119
+ if (preShape !== shape) {
120
+ self._emitEvent('mouseenter', e, point, shape);
121
+
122
+ self._emitEvent('mouseover', e, point, shape);
123
+
124
+ if (dragging) {
125
+ self._emitEvent('dragenter', e, point, shape);
126
+ }
127
+ }
128
+ } else {
129
+ var canvasmousemove = self._getEventObj('mousemove', e, point, self);
130
+
131
+ self.emit('mousemove', canvasmousemove);
132
+ }
133
+
134
+ preShape = shape;
135
+ } else {
136
+ this._emitEvent(type, e, point, shape || this); // e.button === 0 保证按下左键,防止点击右键触发click
137
+
138
+
139
+ if (!dragging && type === 'mousedown' && e.button === LEFT_BTN_CODE) {
140
+ mousedownShape = shape;
141
+ mousedownOffset = {
142
+ x: e.clientX,
143
+ y: e.clientY
144
+ };
145
+ }
146
+
147
+ if (type === 'mouseup' && e.button === LEFT_BTN_CODE) {
148
+ var dx = mousedownOffset.x - e.clientX;
149
+ var dy = mousedownOffset.y - e.clientY;
150
+ var dist = dx * dx + dy * dy;
151
+
152
+ if (dist < CLICK_OFFSET) {
153
+ clearTimeout(dragTimer);
154
+ dragTimer = null;
155
+
156
+ this._emitEvent('click', e, point, mousedownShape || this);
157
+ }
158
+
159
+ if (dragging) {
160
+ dragging._cfg.capture = true;
161
+
162
+ this._emitEvent('dragend', e, point, dragging);
163
+
164
+ dragging = null;
165
+
166
+ this._emitEvent('drop', e, point, shape || this);
167
+ }
168
+
169
+ mousedownShape = null;
170
+ }
171
+ }
172
+
173
+ if (shape && !shape.get('destroyed')) {
174
+ el.style.cursor = shape.attr('cursor') || 'default';
175
+ }
176
+ },
177
+ _emitEvent: function _emitEvent(type, evt, point, shape) {
178
+ var event = this._getEventObj(type, evt, point, shape);
179
+
180
+ var emitShape = this._getEmitter(shape, evt);
181
+
182
+ emitShape && !emitShape.get('destroyed') && emitShape.emit(type, event);
183
+ return emitShape;
184
+ }
185
+ };
package/lib/index.js CHANGED
@@ -19,5 +19,5 @@ module.exports = {
19
19
  PathUtil: require('./util/path'),
20
20
  Event: require('./event'),
21
21
  // version, etc.
22
- version: '3.3.5'
22
+ version: '3.3.6'
23
23
  };
@@ -5,7 +5,8 @@ var regexDot = /[^\s\,]+/ig;
5
5
  var regexLG = /^l\s*\(\s*([\d.]+)\s*\)\s*(.*)/i;
6
6
  var regexRG = /^r\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)\s*(.*)/i;
7
7
  var regexPR = /^p\s*\(\s*([axyn])\s*\)\s*(.*)/i;
8
- var regexColorStop = /[\d.]+:(#[^\s]+|[^\)]+\))/ig; // const numColorCache = {};
8
+ var regexColorStop = /[\d.]+:(#[^\s]+|[^\)]+\))/ig;
9
+ var numColorCache = {};
9
10
 
10
11
  function addStop(steps, gradient) {
11
12
  var arr = steps.match(regexColorStop);
@@ -197,20 +198,22 @@ module.exports = {
197
198
 
198
199
  return color;
199
200
  }
200
- }
201
- /* ,
202
- numberToColor(num) {
201
+ },
202
+ numberToColor: function numberToColor(num) {
203
203
  // 增加缓存
204
- let color = numColorCache[num];
204
+ var color = numColorCache[num];
205
+
205
206
  if (!color) {
206
- let str = num.toString(16);
207
- for (let i = str.length; i < 6; i++) {
207
+ var str = num.toString(16);
208
+
209
+ for (var i = str.length; i < 6; i++) {
208
210
  str = '0' + str;
209
211
  }
212
+
210
213
  color = '#' + str;
211
214
  numColorCache[num] = color;
212
215
  }
213
- return color;
214
- }*/
215
216
 
217
+ return color;
218
+ }
216
219
  };