base.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { mix } from '../util/common';
  2. import MatrixUtil from '../graphic/util/matrix';
  3. import Vector2 from '../graphic/util/vector2';
  4. const defaultMatrix = [ 1, 0, 0, 1, 0, 0 ];
  5. class Base {
  6. _initDefaultCfg() {}
  7. constructor(cfg) {
  8. this._initDefaultCfg();
  9. mix(this, cfg);
  10. let start;
  11. let end;
  12. if (this.plot) {
  13. start = this.plot.bl;
  14. end = this.plot.tr;
  15. this.start = start;
  16. this.end = end;
  17. } else {
  18. start = this.start;
  19. end = this.end;
  20. }
  21. this.init(start, end);
  22. }
  23. _scale(s1, s2) {
  24. const matrix = this.matrix;
  25. const center = this.center;
  26. MatrixUtil.translate(matrix, matrix, [ center.x, center.y ]);
  27. MatrixUtil.scale(matrix, matrix, [ s1, s2 ]);
  28. MatrixUtil.translate(matrix, matrix, [ -center.x, -center.y ]);
  29. }
  30. init(start, end) {
  31. this.matrix = [].concat(defaultMatrix);
  32. // 设置中心点
  33. this.center = {
  34. x: ((end.x - start.x) / 2) + start.x,
  35. y: (end.y - start.y) / 2 + start.y
  36. };
  37. if (this.scale) {
  38. this._scale(this.scale[0], this.scale[1]);
  39. }
  40. }
  41. convertPoint(point) {
  42. const { x, y } = this._convertPoint(point);
  43. if (!MatrixUtil.isChanged(this.matrix)) {
  44. return { x, y };
  45. }
  46. const vector = [ x, y ];
  47. Vector2.transformMat2d(vector, vector, this.matrix);
  48. return {
  49. x: vector[0],
  50. y: vector[1]
  51. };
  52. }
  53. invertPoint(point) {
  54. return this._invertPoint(point);
  55. }
  56. _convertPoint(point) {
  57. return point;
  58. }
  59. _invertPoint(point) {
  60. return point;
  61. }
  62. reset(plot) {
  63. this.plot = plot;
  64. const { bl, tr } = plot;
  65. this.start = bl;
  66. this.end = tr;
  67. this.init(bl, tr);
  68. }
  69. }
  70. export default Base;