缩放/平移后的画布坐标

时间:2016-07-08 15:06:16

标签: javascript html5 canvas

我正在通过从api收到的一些数据构建Canvas,这一切都很好。无论如何,有几天我坚持使用以下代码`canvas.addEventListener('wheel',(event:MouseWheelEvent)=> {         event.preventDefault();

    let coords = Positioning.transformedPoint(
      event.pageX - canvas.offsetLeft,
      event.pageY - canvas.offsetTop
    );

    canvasMethods.clear();
    canvasMethods.translate(coords.x, coords.y);

    if (event.wheelDeltaY > 0) {
      canvasMethods.scale(ZoomDirection.ZOOM_IN);
    } else if (event.wheelDeltaY < 0) {
      canvasMethods.scale(ZoomDirection.ZOOM_OUT);
    }

    canvasMethods.translate(-coords.x, -coords.y);

    this._renderFn();
  }, false);

  canvas.addEventListener('mousedown', (event: MouseEvent) => {
    event.preventDefault();

    this._dragging = true;
    this._dragStart = Positioning.transformedPoint(
      event.clientX - canvas.offsetLeft,
      event.clientY - canvas.offsetTop
    );
  }, false);

  canvas.addEventListener('dblclick', (event: MouseEvent) => {
    let coords = Positioning.transformedPoint(
      event.clientX - canvas.offsetLeft,
      event.clientY - canvas.offsetTop
    );
    this._clickFn(coords);
  });

  canvas.addEventListener('mousemove', (event: MouseEvent) => {
    if (this._dragging) {
      event.preventDefault();

      this._dragEnd = Positioning.transformedPoint(
        event.pageX - canvas.offsetLeft,
        event.pageY - canvas.offsetTop
      );

      let coords = Positioning.transformedPoint(
        event.clientX - canvas.offsetLeft,
        event.clientY - canvas.offsetTop
      );

      canvasMethods.translate(coords.x - this._dragStart.x, coords.y - this._dragStart.y);
      canvasMethods.clear();

      this._renderFn();
      this._dragStart = this._dragEnd;
    }
  }, false);

  canvas.addEventListener('mouseup', (event: MouseEvent) => {
    event.preventDefault();

    this._dragging = false;
    this._dragStart = null;
    this._dragEnd = null;
  })
}`

我在正常尺度上得到正确的坐标,但是一旦我缩放我就会得到一个增量误差(基本上实际点和鼠标光标之间的距离变得越来越大)我无法理解为什么。为了计算矩阵中的坐标,我使用SVG方法作为包装器,方法如下:export class Positioning {     static svg = document.createElementNS('http://www.w3.org/2000/svg','svg');     private static xform = Positioning.svg.createSVGMatrix();

static transformedPoint(x: number, y: number): SVGPoint {
  let coords = Positioning.svg.createSVGPoint();

  coords.x = x;
  coords.y = y;

  return coords.matrixTransform(Positioning.xform.inverse());
}

}`

我知道这与caling有某种关系,但我真的无法弄清楚如何考虑缩放并做正确的操作以获得比例。我也检查了这个答案Zoom Canvas to Mouse Cursor这个非常准确,但实际上他以某种方式对它进行了解释,我无法重新考虑。让其他人遇到同样的问题吗?

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题,我忘了重要的事情,跟踪我的转变。现在我将链接代码对可能遇到同样问题的人有用(如果你不使用Typescript可以避免c的类型)

基本上我使用SVG Matrix方法来追踪画布中发生的事情。

这是通过存储由createSVGMatrix()创建的未转换画布(1,0,0,1,0,0)的基本Matrix并通过本机SVGMatrix方法跟踪转换来获取Canvas转换的类。正在更新原始矩阵。然后,当我需要时,我只需使用变换后的点方法将鼠标的正确x,y坐标按比例缩放到矩阵。

export class Positioning {
static svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
static MatrixTransformationProxy = Positioning.svg.createSVGMatrix();

static transformedPoint(x: number, y: number): SVGPoint {
  let coords = Positioning.svg.createSVGPoint();

  coords.x = x;
  coords.y = y;

  return coords.matrixTransform(Positioning.MatrixTransformationProxy.inverse());
}

static translateProxy(x: number, y: number) {
  Positioning.MatrixTransformationProxy = Positioning.MatrixTransformationProxy.translate(x, y);
}

static scaleUniformProxy(scaleValue: number) {
  Positioning.MatrixTransformationProxy = Positioning.MatrixTransformationProxy.scale(scaleValue);
}

}

我使用我的ProxyMatrix同步我的画布转换,使用包装器调用Canvas方法的canvas方法和相对Positioning方法来更新MatrixTransformationProxy,如下所示

export class CanvasMethods {
static getSharedInstance(): CanvasMethods {
  return sharedInstance;
}

private _canvas: HTMLCanvasElement;

getCanvas(): HTMLCanvasElement {
  return this._canvas;
}

getContext(): CanvasRenderingContext2D {
  return this._canvas.getContext('2d');
}

registerCanvas(canvas: HTMLCanvasElement): void {
  this._canvas = canvas;
}

getCanvasBoundingRect(): BoundingRect {
  return new BoundingRect(0, 0, this._canvas.width, this._canvas.height);
}

clear(): void {
  this.getContext().save();
  this.getContext().setTransform(1, 0, 0, 1, 0, 0);
  this.getContext().clearRect(0, 0, this._canvas.width, this._canvas.height);
  this.getContext().restore();
}

scale(direction: ZoomDirection): void {
  let options = { //TODO get this from constructor options
    scaleValueOut: 0.8,
    scaleValueIn: 1.1
  };

  if (direction === ZoomDirection.ZOOM_OUT) {
    Positioning.scaleUniformProxy(options.scaleValueOut);
    this.getContext().scale(options.scaleValueOut, options.scaleValueOut);
  } else if (direction === ZoomDirection.ZOOM_IN) {
    Positioning.scaleUniformProxy(options.scaleValueIn);
    this.getContext().scale(options.scaleValueIn, options.scaleValueIn);
  }
}

translate(x: number, y: number): void {
  Positioning.translateProxy(x, y);
  this.getContext().translate(x, y);
}

}

const sharedInstance = new CanvasMethods(); }