Skip to content

TileEngine sx/sy setter doesn't account for scaling #412

@snowfrogdev

Description

@snowfrogdev

I tried changing the sx value on my TileEngine and I noticed that it wasn't working. I thought that was weird as the current value was 0 and I was just trying to increment it by 1. It would remain at 0.

A look at the setter made me understand the problem.

  set sx(value) {
    let max = Math.max(0, this.mapwidth - getCanvas().width);
    this._sx = clamp(0, max, value);
  }

  set sy(value) {
    let max = Math.max(0, this.mapheight - getCanvas().height);
    this._sy = clamp(0, max, value);
  }

It is comparing the mapwidth with the canvas' width. In my particular case the mapwidth value was 1024 and the canvas width was 1920. Which at face value seems right. That's like saying that the map width is contained within the canvas. But the thing is, it wasn't. For one very simple reason, I'm scaling using context.scale(scaleFactorX, scaleFacorY).

I would think that scaling like this is a pretty common thing to do. Is it not?

Anyway, I'm thinking that it would be useful to account for scaling in the sx and sy setters to make it possible for people who are doing it to use the TileEngine.

Modifying those setters like this should do the trick:

  set sx(value) {
    let scaleFactor = getContext().getTransform().a;
    let max = Math.max(0, this.mapwidth * scaleFactor - getCanvas().width);
    this._sx = clamp(0, max, value);
  }

  set sy(value) {
    let scaleFactor = getContext().getTransform().d;
    let max = Math.max(0, this.mapheight * scaleFactor - getCanvas().height);
    this._sy = clamp(0, max, value);
  }

I'd be happy to make a PR if you think this is a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug fixes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions