@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
72 lines (67 loc) • 4.96 kB
JavaScript
import { AABBBounds } from "@visactor/vutils";
import { UpdateTag } from "../../common/enums";
import { Generator } from "../../common/generator";
const globalBounds = new AABBBounds;
export class DirtyBoundsPlugin {
constructor() {
this.name = "DirtyBoundsPlugin", this.activeEvent = "onRegister", this._uid = Generator.GenAutoIncrementId(),
this.key = this.name + this._uid, this.dirtyBoundsHooksRegistered = !1, this.handlePaintOnlyUpdate = graphic => {
var _a;
const stage = this.pluginService.stage;
if (!stage || stage !== graphic.stage || !stage.renderCount) return;
const owner = null !== (_a = graphic.glyphHost) && void 0 !== _a ? _a : graphic;
if (!(owner._updateTag & UpdateTag.UPDATE_PAINT) || owner._updateTag & UpdateTag.UPDATE_BOUNDS) return;
const ownerBounds = this.ensurePaintDirtyBoundsCache(owner);
ownerBounds && !ownerBounds.empty() && stage.dirty(ownerBounds), owner.clearUpdatePaintTag();
const shadowRoot = owner.shadowRoot;
if (!shadowRoot) return;
const shadowBounds = this.ensurePaintDirtyBoundsCache(shadowRoot);
shadowBounds && !shadowBounds.empty() && stage.dirty(shadowBounds), shadowRoot.clearUpdatePaintTag();
};
}
ensurePaintDirtyBoundsCache(graphic) {
var _a;
const owner = graphic;
return owner._AABBBounds.empty() && owner.doUpdateAABBBounds("imprecise" === (null === (_a = owner.attribute) || void 0 === _a ? void 0 : _a.boundsMode)),
owner._globalAABBBounds && !owner._globalAABBBounds.empty() || owner.tryUpdateGlobalAABBBounds(),
owner._globalAABBBounds;
}
getRemoveDirtyBounds(graphic) {
var _a;
const cachedBounds = (null !== (_a = graphic.glyphHost) && void 0 !== _a ? _a : graphic)._globalAABBBounds;
if (cachedBounds && !cachedBounds.empty()) return cachedBounds;
}
registerDirtyBoundsHooks(stage) {
this.dirtyBoundsHooksRegistered || (stage.graphicService.hooks.onAttributeUpdate.tap(this.key, this.handlePaintOnlyUpdate),
stage.graphicService.hooks.beforeUpdateAABBBounds.tap(this.key, ((graphic, stage, willUpdate, bounds) => {
graphic.glyphHost && (graphic = graphic.glyphHost), stage && stage === this.pluginService.stage && stage.renderCount && (graphic.isContainer && !graphic.shouldSelfChangeUpdateAABBBounds() || willUpdate && (globalBounds.setValue(bounds.x1, bounds.y1, bounds.x2, bounds.y2),
stage.dirty(globalBounds, graphic.parent && graphic.parent.globalTransMatrix)));
})), stage.graphicService.hooks.afterUpdateAABBBounds.tap(this.key, ((graphic, stage, bounds, params, selfChange) => {
stage && stage === this.pluginService.stage && stage.renderCount && (graphic.isContainer && !selfChange || stage.dirty(params.globalAABBBounds));
})), stage.graphicService.hooks.clearAABBBounds.tap(this.key, ((graphic, stage, bounds) => {
stage && stage === this.pluginService.stage && stage.renderCount && stage && stage.dirty(bounds);
})), stage.graphicService.hooks.onRemove.tap(this.key, (graphic => {
const stage = graphic.stage;
if (!stage || stage !== this.pluginService.stage || !stage.renderCount) return;
const bounds = this.getRemoveDirtyBounds(graphic);
bounds && !bounds.empty() && stage.dirty(bounds);
})), this.dirtyBoundsHooksRegistered = !0);
}
activate(context) {
this.pluginService = context, context.stage.hooks.afterRender.tap(this.key, (stage => {
stage && stage === this.pluginService.stage && (stage.dirtyBounds.clear(), this.registerDirtyBoundsHooks(stage));
}));
const stage = this.pluginService.stage;
stage && stage.renderCount && this.registerDirtyBoundsHooks(stage);
}
deactivate(context) {
const stage = this.pluginService.stage;
stage && (stage.graphicService.hooks.onAttributeUpdate.taps = stage.graphicService.hooks.onAttributeUpdate.taps.filter((item => item.name !== this.key)),
this.dirtyBoundsHooksRegistered = !1, stage.graphicService.hooks.beforeUpdateAABBBounds.taps = stage.graphicService.hooks.beforeUpdateAABBBounds.taps.filter((item => item.name !== this.key)),
stage.graphicService.hooks.afterUpdateAABBBounds.taps = stage.graphicService.hooks.afterUpdateAABBBounds.taps.filter((item => item.name !== this.key)),
stage.graphicService.hooks.clearAABBBounds.taps = stage.graphicService.hooks.clearAABBBounds.taps.filter((item => item.name !== this.key)),
stage.hooks.afterRender.taps = stage.hooks.afterRender.taps.filter((item => item.name !== this.key)),
stage.graphicService.hooks.onRemove.taps = stage.graphicService.hooks.onRemove.taps.filter((item => item.name !== this.key)));
}
}
//# sourceMappingURL=dirty-bounds-plugin.js.map