canvas.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { __extends } from "tslib";
  2. import { detect } from 'detect-browser';
  3. import Container from './container';
  4. import { isBrowser, isNil, isString } from '../util/util';
  5. import Timeline from '../animate/timeline';
  6. import EventController from '../event/event-contoller';
  7. var PX_SUFFIX = 'px';
  8. var browser = detect();
  9. var isFirefox = browser && browser.name === 'firefox';
  10. var Canvas = /** @class */ (function (_super) {
  11. __extends(Canvas, _super);
  12. function Canvas(cfg) {
  13. var _this = _super.call(this, cfg) || this;
  14. _this.initContainer();
  15. _this.initDom();
  16. _this.initEvents();
  17. _this.initTimeline();
  18. return _this;
  19. }
  20. Canvas.prototype.getDefaultCfg = function () {
  21. var cfg = _super.prototype.getDefaultCfg.call(this);
  22. // set default cursor style for canvas
  23. cfg['cursor'] = 'default';
  24. // CSS transform 目前尚未经过长时间验证,为了避免影响上层业务,默认关闭,上层按需开启
  25. cfg['supportCSSTransform'] = false;
  26. return cfg;
  27. };
  28. /**
  29. * @protected
  30. * 初始化容器
  31. */
  32. Canvas.prototype.initContainer = function () {
  33. var container = this.get('container');
  34. if (isString(container)) {
  35. container = document.getElementById(container);
  36. this.set('container', container);
  37. }
  38. };
  39. /**
  40. * @protected
  41. * 初始化 DOM
  42. */
  43. Canvas.prototype.initDom = function () {
  44. var el = this.createDom();
  45. this.set('el', el);
  46. // 附加到容器
  47. var container = this.get('container');
  48. container.appendChild(el);
  49. // 设置初始宽度
  50. this.setDOMSize(this.get('width'), this.get('height'));
  51. };
  52. /**
  53. * @protected
  54. * 初始化绑定的事件
  55. */
  56. Canvas.prototype.initEvents = function () {
  57. var eventController = new EventController({
  58. canvas: this,
  59. });
  60. eventController.init();
  61. this.set('eventController', eventController);
  62. };
  63. /**
  64. * @protected
  65. * 初始化时间轴
  66. */
  67. Canvas.prototype.initTimeline = function () {
  68. var timeline = new Timeline(this);
  69. this.set('timeline', timeline);
  70. };
  71. /**
  72. * @protected
  73. * 修改画布对应的 DOM 的大小
  74. * @param {number} width 宽度
  75. * @param {number} height 高度
  76. */
  77. Canvas.prototype.setDOMSize = function (width, height) {
  78. var el = this.get('el');
  79. if (isBrowser) {
  80. el.style.width = width + PX_SUFFIX;
  81. el.style.height = height + PX_SUFFIX;
  82. }
  83. };
  84. // 实现接口
  85. Canvas.prototype.changeSize = function (width, height) {
  86. this.setDOMSize(width, height);
  87. this.set('width', width);
  88. this.set('height', height);
  89. this.onCanvasChange('changeSize');
  90. };
  91. /**
  92. * 获取当前的渲染引擎
  93. * @return {Renderer} 返回当前的渲染引擎
  94. */
  95. Canvas.prototype.getRenderer = function () {
  96. return this.get('renderer');
  97. };
  98. /**
  99. * 获取画布的 cursor 样式
  100. * @return {Cursor}
  101. */
  102. Canvas.prototype.getCursor = function () {
  103. return this.get('cursor');
  104. };
  105. /**
  106. * 设置画布的 cursor 样式
  107. * @param {Cursor} cursor cursor 样式
  108. */
  109. Canvas.prototype.setCursor = function (cursor) {
  110. this.set('cursor', cursor);
  111. var el = this.get('el');
  112. if (isBrowser && el) {
  113. // 直接设置样式,不等待鼠标移动时再设置
  114. el.style.cursor = cursor;
  115. }
  116. };
  117. // 实现接口
  118. Canvas.prototype.getPointByEvent = function (ev) {
  119. var supportCSSTransform = this.get('supportCSSTransform');
  120. if (supportCSSTransform) {
  121. // For Firefox <= 38
  122. if (isFirefox && !isNil(ev.layerX) && ev.layerX !== ev.offsetX) {
  123. return {
  124. x: ev.layerX,
  125. y: ev.layerY,
  126. };
  127. }
  128. if (!isNil(ev.offsetX)) {
  129. // For IE6+, Firefox >= 39, Chrome, Safari, Opera
  130. return {
  131. x: ev.offsetX,
  132. y: ev.offsetY,
  133. };
  134. }
  135. }
  136. // should calculate by self for other cases, like Safari in ios
  137. // TODO: support CSS transform
  138. var _a = this.getClientByEvent(ev), clientX = _a.x, clientY = _a.y;
  139. return this.getPointByClient(clientX, clientY);
  140. };
  141. // 获取 touch 事件的 clientX 和 clientY 需要单独处理
  142. Canvas.prototype.getClientByEvent = function (ev) {
  143. var clientInfo = ev;
  144. if (ev.touches) {
  145. if (ev.type === 'touchend') {
  146. clientInfo = ev.changedTouches[0];
  147. }
  148. else {
  149. clientInfo = ev.touches[0];
  150. }
  151. }
  152. return {
  153. x: clientInfo.clientX,
  154. y: clientInfo.clientY,
  155. };
  156. };
  157. // 实现接口
  158. Canvas.prototype.getPointByClient = function (clientX, clientY) {
  159. var el = this.get('el');
  160. var bbox = el.getBoundingClientRect();
  161. return {
  162. x: clientX - bbox.left,
  163. y: clientY - bbox.top,
  164. };
  165. };
  166. // 实现接口
  167. Canvas.prototype.getClientByPoint = function (x, y) {
  168. var el = this.get('el');
  169. var bbox = el.getBoundingClientRect();
  170. return {
  171. x: x + bbox.left,
  172. y: y + bbox.top,
  173. };
  174. };
  175. // 实现接口
  176. Canvas.prototype.draw = function () { };
  177. /**
  178. * @protected
  179. * 销毁 DOM 容器
  180. */
  181. Canvas.prototype.removeDom = function () {
  182. var el = this.get('el');
  183. el.parentNode.removeChild(el);
  184. };
  185. /**
  186. * @protected
  187. * 清理所有的事件
  188. */
  189. Canvas.prototype.clearEvents = function () {
  190. var eventController = this.get('eventController');
  191. eventController.destroy();
  192. };
  193. Canvas.prototype.isCanvas = function () {
  194. return true;
  195. };
  196. Canvas.prototype.getParent = function () {
  197. return null;
  198. };
  199. Canvas.prototype.destroy = function () {
  200. var timeline = this.get('timeline');
  201. if (this.get('destroyed')) {
  202. return;
  203. }
  204. this.clear();
  205. // 同初始化时相反顺序调用
  206. if (timeline) {
  207. // 画布销毁时自动停止动画
  208. timeline.stop();
  209. }
  210. this.clearEvents();
  211. this.removeDom();
  212. _super.prototype.destroy.call(this);
  213. };
  214. return Canvas;
  215. }(Container));
  216. export default Canvas;
  217. //# sourceMappingURL=canvas.js.map