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

Setting an anchor doesn't affect tagged text #268

Open
wmike1987 opened this issue Jun 30, 2022 · 7 comments
Open

Setting an anchor doesn't affect tagged text #268

wmike1987 opened this issue Jun 30, 2022 · 7 comments

Comments

@wmike1987
Copy link

Example from the demo:

            const basicText = `Let's make some
                  <ml>multiline</ml>
                  and <ms>multistyle</ms> text for
                  <pixi>Pixi.js!</pixi>`;

            const basicStyle = {
              default: {
                fontSize: "24px",
                fill: "#cccccc",
                align: "center",
              },
              ml: {
                fontStyle: "italic",
                fill: "#ff8888",
                fontSize: "40px",
              },
              ms: {
                fontWeight: "bold",
                fill: "#4488ff",
                fontSize: "40px",
              },
              pixi: {
                fontSize: "64px",
                fill: "#efefef",
              },
            };

            const basic = new TaggedText(basicText, basicStyle, {});

            //Added this line to the demo
            basic.anchor = {x: 0.5, y: 0.5};

Setting the anchor has no effect on the tagged-text placement.

@mimshwright
Copy link
Owner

mimshwright commented Jun 30, 2022 via email

@wmike1987
Copy link
Author

Here's a sample pixi playground with the effect of an anchor: https://www.pixiplayground.com/#/edit/EUqEbIdDK0gfM18V0_bLr

In my particular use-case, I'd like the tagged text to be centered at the position I give it (for other sprites and regular pixi Text, I use an anchor of 0.5 to achieve this).

@mimshwright
Copy link
Owner

mimshwright commented Jul 1, 2022 via email

@mimshwright
Copy link
Owner

Apologies. I am totally wrong. TaggedText extends Sprite not Container. I wrote that while I was away from my computer and should have checked first. So I gues that eliminates #1 and maybe #3.

I think next step is to create some tests or some way to figure out what the current behaviour is and why it's not working and what the behaviour should be.

@neymanushka
Copy link

any fixes or recipes how to make anchor work?)

@nz771202
Copy link

nz771202 commented Feb 13, 2024

The pivot of the container could be used for this. Basically just get the local bounds of the container, and multiply its width/height by the anchor to get the pivot. For example, this seems to work (in TS, overriding the TaggedText's anchor callback, so you can set the anchor as normal):

export class TaggedTextMod extends TaggedText {
    constructor(...args: ConstructorParameters<typeof TaggedText>) {
        super(...args);
        const origCb = this.anchor.cb;
        this.anchor.cb = function() {
            origCb.call(this);
            const bounds = this.getLocalBounds();
            this.pivot.set(bounds.width * this.anchor.x, bounds.height * this.anchor.y);
        };
    }
}

The drawback is that it doesn't update when the contents change. Maybe you could do the pivot update inside updateTransform, e.g.:

class TaggedTextMod extends TaggedText {
    private exitFlag = false; // prevent infinite recursive updateTransform calls

    public override updateTransform() {
        if (this.exitFlag) return;
        this.exitFlag = true;
        const bounds = this.getLocalBounds();
        this.exitFlag = false;
        this.pivot.set(bounds.width * this.anchor.x / this.scale.x, bounds.height * this.anchor.y / this.scale.y);
        super.updateTransform();
    }
}

@geraldzm
Copy link

@nz771202 thanks to your comment, I was able to get it working by doing this.

class TaggedTextMod extends TaggedText {
  constructor() {
    // any anchor you want
    this.anchor.set(0.5);
  }
  private _updateLock = false;

  public override updateTransform() {
    if (this._updateLock) return super.updateTransform();

    this._updateLock = true;

    // Store original properties
    const originalAnchor = this.anchor.clone();
    const originalPivot = this.pivot.clone();

    // Calculate and set the pivot
    const bounds = this.getLocalBounds();
    this.pivot.set(bounds.width * this.anchor.x, bounds.height * this.anchor.y);

    // Perform the transform
    super.updateTransform();

    // Restore original properties
    this.anchor.copyFrom(originalAnchor);
    this.pivot.copyFrom(originalPivot);

    // Adjust position to maintain visual position
    const dx = (this.anchor.x - originalAnchor.x) * this.width;
    const dy = (this.anchor.y - originalAnchor.y) * this.height;
    this.position.x +=
      dx * Math.cos(this.rotation) + dy * Math.sin(this.rotation);
    this.position.y +=
      -dx * Math.sin(this.rotation) + dy * Math.cos(this.rotation);

    this._updateLock = false;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants