background.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.BackgroundManager = void 0;
  10. const x6_common_1 = require("@antv/x6-common");
  11. const x6_geometry_1 = require("@antv/x6-geometry");
  12. const registry_1 = require("../registry");
  13. const base_1 = require("./base");
  14. class BackgroundManager extends base_1.Base {
  15. get elem() {
  16. return this.view.background;
  17. }
  18. init() {
  19. this.startListening();
  20. if (this.options.background) {
  21. this.draw(this.options.background);
  22. }
  23. }
  24. startListening() {
  25. this.graph.on('scale', this.update, this);
  26. this.graph.on('translate', this.update, this);
  27. }
  28. stopListening() {
  29. this.graph.off('scale', this.update, this);
  30. this.graph.off('translate', this.update, this);
  31. }
  32. updateBackgroundImage(options = {}) {
  33. let backgroundSize = options.size || 'auto auto';
  34. let backgroundPosition = options.position || 'center';
  35. const scale = this.graph.transform.getScale();
  36. const ts = this.graph.translate();
  37. // backgroundPosition
  38. if (typeof backgroundPosition === 'object') {
  39. const x = ts.tx + scale.sx * (backgroundPosition.x || 0);
  40. const y = ts.ty + scale.sy * (backgroundPosition.y || 0);
  41. backgroundPosition = `${x}px ${y}px`;
  42. }
  43. // backgroundSize
  44. if (typeof backgroundSize === 'object') {
  45. backgroundSize = x6_geometry_1.Rectangle.fromSize(backgroundSize).scale(scale.sx, scale.sy);
  46. backgroundSize = `${backgroundSize.width}px ${backgroundSize.height}px`;
  47. }
  48. this.elem.style.backgroundSize = backgroundSize;
  49. this.elem.style.backgroundPosition = backgroundPosition;
  50. }
  51. drawBackgroundImage(img, options = {}) {
  52. if (!(img instanceof HTMLImageElement)) {
  53. this.elem.style.backgroundImage = '';
  54. return;
  55. }
  56. // draw multiple times to show the last image
  57. const cache = this.optionsCache;
  58. if (cache && cache.image !== options.image) {
  59. return;
  60. }
  61. let uri;
  62. const opacity = options.opacity;
  63. const backgroundSize = options.size;
  64. let backgroundRepeat = options.repeat || 'no-repeat';
  65. const pattern = registry_1.Background.registry.get(backgroundRepeat);
  66. if (typeof pattern === 'function') {
  67. const quality = options.quality || 1;
  68. img.width *= quality;
  69. img.height *= quality;
  70. const canvas = pattern(img, options);
  71. if (!(canvas instanceof HTMLCanvasElement)) {
  72. throw new Error('Background pattern must return an HTML Canvas instance');
  73. }
  74. uri = canvas.toDataURL('image/png');
  75. // `repeat` was changed in pattern function
  76. if (options.repeat && backgroundRepeat !== options.repeat) {
  77. backgroundRepeat = options.repeat;
  78. }
  79. else {
  80. backgroundRepeat = 'repeat';
  81. }
  82. if (typeof backgroundSize === 'object') {
  83. // recalculate the tile size if an object passed in
  84. backgroundSize.width *= canvas.width / img.width;
  85. backgroundSize.height *= canvas.height / img.height;
  86. }
  87. else if (backgroundSize === undefined) {
  88. // calcule the tile size if no provided
  89. options.size = {
  90. width: canvas.width / quality,
  91. height: canvas.height / quality,
  92. };
  93. }
  94. }
  95. else {
  96. uri = img.src;
  97. if (backgroundSize === undefined) {
  98. options.size = {
  99. width: img.width,
  100. height: img.height,
  101. };
  102. }
  103. }
  104. if (cache != null &&
  105. typeof options.size === 'object' &&
  106. options.image === cache.image &&
  107. options.repeat === cache.repeat &&
  108. options.quality ===
  109. cache.quality) {
  110. cache.size = x6_common_1.ObjectExt.clone(options.size);
  111. }
  112. const style = this.elem.style;
  113. style.backgroundImage = `url(${uri})`;
  114. style.backgroundRepeat = backgroundRepeat;
  115. style.opacity = opacity == null || opacity >= 1 ? '' : `${opacity}`;
  116. this.updateBackgroundImage(options);
  117. }
  118. updateBackgroundColor(color) {
  119. this.elem.style.backgroundColor = color || '';
  120. }
  121. updateBackgroundOptions(options) {
  122. this.graph.options.background = options;
  123. }
  124. update() {
  125. if (this.optionsCache) {
  126. this.updateBackgroundImage(this.optionsCache);
  127. }
  128. }
  129. draw(options) {
  130. const opts = options || {};
  131. this.updateBackgroundOptions(options);
  132. this.updateBackgroundColor(opts.color);
  133. if (opts.image) {
  134. this.optionsCache = x6_common_1.ObjectExt.clone(opts);
  135. const img = document.createElement('img');
  136. img.onload = () => this.drawBackgroundImage(img, options);
  137. img.setAttribute('crossorigin', 'anonymous');
  138. img.src = opts.image;
  139. }
  140. else {
  141. this.drawBackgroundImage(null);
  142. this.optionsCache = null;
  143. }
  144. }
  145. clear() {
  146. this.draw();
  147. }
  148. dispose() {
  149. this.clear();
  150. this.stopListening();
  151. }
  152. }
  153. __decorate([
  154. base_1.Base.dispose()
  155. ], BackgroundManager.prototype, "dispose", null);
  156. exports.BackgroundManager = BackgroundManager;
  157. //# sourceMappingURL=background.js.map