Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove _fitDesignResolution function call in EVENT_AFTER_UPDATE #15802

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions cocos/2d/framework/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Canvas extends RenderRoot2D {

protected _thisOnCameraResized: () => void;
// fit canvas node to design resolution
protected _fitDesignResolution: (() => void) | undefined;
protected fitDesignResolution_EDITOR: (() => void) | undefined;

private _pos = new Vec3();
private _renderMode = RenderMode.OVERLAY;
Expand All @@ -134,16 +134,33 @@ export class Canvas extends RenderRoot2D {
this._thisOnCameraResized = this._onResizeCamera.bind(this);

if (EDITOR) {
this._fitDesignResolution = (): void => {
this.fitDesignResolution_EDITOR = (): void => {
// TODO: support paddings of locked widget
this.node.getPosition(this._pos);
const nodeSize = view.getDesignResolutionSize();
Vec3.set(_worldPos, nodeSize.width * 0.5, nodeSize.height * 0.5, 0);
const trans = this.node._uiProps.uiTransformComp!;

let scaleX = this.node.scale.x;
moshuying marked this conversation as resolved.
Show resolved Hide resolved
let anchorX = trans.anchorX;
if (scaleX < 0) {
anchorX = 1.0 - anchorX;
scaleX = -scaleX;
}
nodeSize.width = scaleX === 0 ? nodeSize.width : nodeSize.width / scaleX;

let scaleY = this.node.scale.y;
let anchorY = trans.anchorY;
if (scaleY < 0) {
anchorY = 1.0 - anchorY;
scaleY = -scaleY;
}
nodeSize.height = scaleY === 0 ? nodeSize.height : nodeSize.height / scaleY;

Vec3.set(_worldPos, nodeSize.width * anchorX, nodeSize.height * anchorY, 0);

if (!this._pos.equals(_worldPos)) {
this.node.setPosition(_worldPos);
}
const trans = this.node._uiProps.uiTransformComp!;
if (trans.width !== nodeSize.width) {
trans.width = nodeSize.width;
}
Expand All @@ -160,7 +177,7 @@ export class Canvas extends RenderRoot2D {
if (widget) {
widget.updateAlignment();
} else if (EDITOR) {
this._fitDesignResolution!();
this.fitDesignResolution_EDITOR!();
}

if (!EDITOR) {
Expand All @@ -173,9 +190,6 @@ export class Canvas extends RenderRoot2D {
this._onResizeCamera();

if (EDITOR) {
// Constantly align canvas node in edit mode
cclegacy.director.on(cclegacy.Director.EVENT_AFTER_UPDATE, this._fitDesignResolution!, this);

// In Editor can not edit these attrs.
// (Position in Node, contentSize in uiTransform)
// (anchor in uiTransform, but it can edit, this is different from cocos creator)
Expand Down Expand Up @@ -203,11 +217,7 @@ export class Canvas extends RenderRoot2D {
public onDestroy (): void {
super.onDestroy();

if (EDITOR) {
cclegacy.director.off(cclegacy.Director.EVENT_AFTER_UPDATE, this._fitDesignResolution!, this);
} else {
this.node.off(NodeEventType.TRANSFORM_CHANGED, this._thisOnCameraResized);
}
this.node.off(NodeEventType.TRANSFORM_CHANGED, this._thisOnCameraResized);
}

protected _onResizeCamera (): void {
Expand Down
Loading