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