简体   繁体   English

缩放到HTML5画布上的特定坐标(Fabric js)

[英]scale to a particular coordinate on HTML5 canvas (Fabric js)

i need to zoom to a particular coordinate(X,Y) on the canvas in fabrics js. 我需要在面料js中的画布上缩放到特定坐标(X,Y)。

I know that there will be 2 functions being called 1. Scaled (x times) 2. Move the image to some x,y location so that the image appears to be at that coordinate. 我知道将有2个函数被调用。1.缩放(x倍)2.将图像移动到x,y的某个位置,以便图像看起来位于该坐标处。

Also i need that once zoomed in , i specify some other coordinate and it should zoom to that new coordinate. 另外,我还需要在放大后指定其他坐标,并且应该将其缩放到该新坐标。 Any tips 有小费吗

You can define yourself a camera. 您可以为自己定义一个摄像机。

You have some absolute coordinate system and you want to project things from that system to the screen. 您有一些绝对坐标系,并且想要将东西从该系统投影到屏幕上。 That is done by the camera object. 这是由相机对象完成的。 The camera has a position somewhere in your absolute coordinate system and it has a zoom factor and a rotation angle. 相机在绝对坐标系中的某个位置,并且具有缩放系数和旋转角度。 The toScreen function takes absolute coordinates and translates them into coordinates on the screen. toScreen函数获取绝对坐标,并将其转换为屏幕上的坐标。 fromScreen does the opposite. fromScreen则相反。 moveBy is a relative move ie it moves in screen coordinates not absolute coordinates. moveBy是相对移动,即它以屏幕坐标而不是绝对坐标移动。 That is useful for example if you have an image and you want it to follow your mouse pointer since the mouse pointer coordinates are screen coordinates. 例如,如果您有图像并且希望它跟随鼠标指针,这将很有用,因为鼠标指针坐标是屏幕坐标。 So you just calculate by how much the pointer moved and then call moveBy on the camera with the negative of those numbers. 因此,您只需计算指针移动了多少,然后用这些数字的负数在相机上调用moveBy。 (negative because if a camera is moved left the image it produces moves right on the screen) Or lets say you click on a point on the screen and you then want the camera to zoom onto that point. (这是负的,因为如果将摄像机向左移动,则它产生的图像将在屏幕上向右移动),或者说您单击屏幕上的某个点,然后希望摄像机将其缩放到该点。 Then you just say camera.moveBy(pointX, pointY) (where pointX/Y is the mouse position relative to the center of the screen ie the position of the camera since in screen coordinates the camera is always at position (0, 0)) and change the zoom factor if needed. 然后,您只需要说说camera.moveBy(pointX, pointY) (其中pointX / Y是相对于屏幕中心的鼠标位置,即相机的位置,因为在屏幕坐标中,相机始终位于位置(0,0))并根据需要更改缩放系数。 btw. 顺便说一句 the "screen" here is of course your canvas. 这里的“屏幕”当然是您的画布。 And pos (0,0) in screen coordinates should be the center of the canvas. 屏幕坐标中的pos(0,0)应该在画布的中心。 So just to emphasize, the canvas is not your absolute coordinate system but the screen onto which the camera projects its image. 因此要强调一点,画布不是您的绝对坐标系,而是相机将其图像投影到的屏幕。

function Camera() {
  this.x = 0;
  this.y = 0;
  this.zoom = 1;
  this.angle = 0;
}

Camera.prototype.moveTo = function(x, y) {
  this.x = x; this.y = y;
};
Camera.prototype.moveBy = function(x, y) {
  var xy = this.fromScreen(x, y);
  this.moveTo(xy.x, xy.y);
};
Camera.prototype.rotateTo = function(angle) {
  while(angle < 0) angle += 360;
  angle %= 360;
  this.angle = angle;
};
Camera.prototype.rotateBy = function(angle) {
  this.rotateTo(this.angle + angle);
};
Camera.prototype.zoomTo = function(zoom) {
  this.zoom = zoom;
};
Camera.prototype.zoomBy = function(zoom) {
  this.zoom *= zoom;
};
Camera.prototype.toScreen = function(x, y) {
  x -= this.x; y -= this.y;
  if(this.angle !== 0) {
    var rad = Math.PI * this.angle / 180;
    rad = 2 * Math.PI - rad;
    var _x = x * Math.cos(rad) - y * Math.sin(rad),
        _y = x * Math.sin(rad) + y * Math.cos(rad);
    x = _x; y = _y;
  }
  return {x: x * this.zoom,
          y: y * this.zoom}
};
Camera.prototype.fromScreen = function(x, y) {
  x /= this.zoom;
  y /= this.zoom;
  if(this.angle !== 0) {
    var rad = Math.PI * this.angle / 180;
    var _x = x * Math.cos(rad) - y * Math.sin(rad),
        _y = x * Math.sin(rad) + y * Math.cos(rad);
    x = _x; y = _y;
  }
  x += this.x; y += this.y;
  return {x: x,
          y: y};
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM