canvas.js 6.7 KB

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