showDebugDirtyRect.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import type { ZRenderType } from '../zrender';
  2. import type CanvasPainter from '../canvas/Painter';
  3. import type BoundingRect from '../core/BoundingRect';
  4. import { extend } from '../core/util';
  5. class DebugRect {
  6. dom: HTMLDivElement
  7. private _hideTimeout: number
  8. constructor(style: Opts['style']) {
  9. const dom = this.dom = document.createElement('div');
  10. dom.className = 'ec-debug-dirty-rect';
  11. style = extend({}, style);
  12. extend(style, {
  13. backgroundColor: 'rgba(0, 0, 255, 0.2)',
  14. border: '1px solid #00f'
  15. });
  16. dom.style.cssText = `
  17. position: absolute;
  18. opacity: 0;
  19. transition: opacity 0.5s linear;
  20. pointer-events: none;
  21. `;
  22. for (let key in style) {
  23. if (style.hasOwnProperty(key)) {
  24. (dom.style as any)[key] = (style as any)[key];
  25. }
  26. }
  27. }
  28. update(rect: BoundingRect) {
  29. const domStyle = this.dom.style;
  30. domStyle.width = rect.width + 'px';
  31. domStyle.height = rect.height + 'px';
  32. domStyle.left = rect.x + 'px';
  33. domStyle.top = rect.y + 'px';
  34. }
  35. hide() {
  36. this.dom.style.opacity = '0';
  37. }
  38. show(autoHideDelay?: number) {
  39. clearTimeout(this._hideTimeout);
  40. this.dom.style.opacity = '1';
  41. // Auto hide after 2 second
  42. this._hideTimeout = setTimeout(() => {
  43. this.hide();
  44. }, autoHideDelay || 1000) as unknown as number;
  45. }
  46. }
  47. interface Opts {
  48. style?: {
  49. backgroundColor?: string
  50. color?: string
  51. }
  52. autoHideDelay?: number
  53. }
  54. export default function showDebugDirtyRect(zr: ZRenderType, opts?: Opts) {
  55. opts = opts || {};
  56. const painter = zr.painter as CanvasPainter;
  57. if (!painter.getLayers) {
  58. throw new Error('Debug dirty rect can only been used on canvas renderer.');
  59. }
  60. if (painter.isSingleCanvas()) {
  61. throw new Error('Debug dirty rect can only been used on zrender inited with container.');
  62. }
  63. const debugViewRoot = document.createElement('div');
  64. debugViewRoot.style.cssText = `
  65. position:absolute;
  66. left:0;
  67. top:0;
  68. right:0;
  69. bottom:0;
  70. pointer-events:none;
  71. `;
  72. debugViewRoot.className = 'ec-debug-dirty-rect-container';
  73. const debugRects: DebugRect[] = [];
  74. const dom = zr.dom;
  75. dom.appendChild(debugViewRoot);
  76. const computedStyle = getComputedStyle(dom);
  77. if (computedStyle.position === 'static') {
  78. dom.style.position = 'relative';
  79. }
  80. zr.on('rendered', function () {
  81. if (painter.getLayers) {
  82. let idx = 0;
  83. painter.eachBuiltinLayer((layer) => {
  84. if (!layer.debugGetPaintRects) {
  85. return;
  86. }
  87. const paintRects = layer.debugGetPaintRects();
  88. for (let i = 0; i < paintRects.length; i++) {
  89. if (!paintRects[i].width || !paintRects[i].height) {
  90. continue;
  91. }
  92. if (!debugRects[idx]) {
  93. debugRects[idx] = new DebugRect(opts.style);
  94. debugViewRoot.appendChild(debugRects[idx].dom);
  95. }
  96. debugRects[idx].show(opts.autoHideDelay);
  97. debugRects[idx].update(paintRects[i]);
  98. idx++;
  99. }
  100. });
  101. for (let i = idx; i < debugRects.length; i++) {
  102. debugRects[i].hide();
  103. }
  104. }
  105. });
  106. }