简体   繁体   English

在Paper.js中适合页面缩放后,光栅图像偏离中心

[英]Raster image is off-center after fit-to-page zooming in Paper.js

I'm working on a Paper.js application that puts a raster (an image) in the view . 我正在开发一个在view中放置raster (图像)的Paper.js应用程序。 Then it zooms to fit the image so that all of it is visible at one time. 然后它缩放以适合图像,以便一次可见所有图像。 It's mostly working, but the image ends up offset, like this: 它主要是工作,但图像最终偏移,如下所示:

抵消

When it should look more like this: 它应该看起来更像这样:

在此输入图像描述

Here's the code that makes the view , adds the image, and makes the call to zoom to fit: 以下是制作view的代码,添加图像并调用缩放以适应:

// Set up HTMLImage
var image = new Image(this.props.image.width, this.props.image.height);
image.src = 'data:image/png;base64,' + this.props.image.imageData;

//var canvas = React.findDOMNode(this.refs.canvas); // React 0.13 +
var canvas = this.refs.canvas.getDOMNode();

// Scale width based on scaled height; canvas height has been set to the height of the document (this.props.height)
var scalingFactor = canvas.height/image.height;
canvas.width = image.width * scalingFactor;

// Initialize Paper.js on the canvas
paper.setup(canvas);

// Add image to Paper.js canvas
var raster = new paper.Raster(image, new paper.Point(0,0));

// Fit image to page so whole thing is displayed
var delta = scalingFactor < 1 ? -1 : 1; // Arbitrary delta (for determining zoom in/out) based on scaling factor
var returnedValues = panAndZoom.changeZoom(paper.view.zoom, delta, paper.view.center, paper.view.center, scalingFactor);
paper.view.zoom = returnedValues[0];

And here is the panAndZoom.changeZoom method: 这是panAndZoom.changeZoom方法:

SimplePanAndZoom.prototype.changeZoom = function(oldZoom, delta, centerPoint, offsetPoint, zoomFactor) {
    var newZoom = oldZoom;
    if (delta < 0) {
        newZoom = oldZoom * zoomFactor;
    }
    if (delta > 0) {
        newZoom = oldZoom / zoomFactor;
    }

    var a = null;
    if(!centerPoint.equals(offsetPoint)) {
        var scalingFactor = oldZoom / newZoom;
        var difference = offsetPoint.subtract(centerPoint);
        a = offsetPoint.subtract(difference.multiply(scalingFactor)).subtract(centerPoint);
    }

    return [newZoom, a];
};

Any idea why it zooms to fit but loses the centering? 任何想法为什么它缩放适合但失去居中?

TL;DR TL; DR

Use either, but not both: 使用其中之一,但不是两者:

  • After the zoom: 缩放后:

     paper.view.setCenter(0,0); 
  • When pasting the image: 粘贴图像时:

     var raster = new paper.Raster(image, new paper.Point(canvas.width/2, canvas.height/2)); 

The long answer 答案很长

As a disclaimer, I must point out I have no knowledge of paper.js and their documentation looks seemingly terrible. 作为免责声明,我必须指出我不了解paper.js,他们的文档看起来似乎很糟糕。 This answer is born from fiddling. 这个答案来自于摆弄。

I more or less replicated your code and after some tinkering, I managed to fix the issue by using this: 我或多或少地复制了你的代码,经过一些修补,我设法通过使用这个解决了这个问题:

paper.view.zoom = returnedValues[0];
paper.view.setCenter(0,0);

If you wonder why I'm not pointing at the documentation for setCenter , it's because I couldn't find any. 如果你想知道我为什么不指向setCenter的文档,那是因为我找不到任何文档。 I discovered that method by inspecting paper.view.center . 我通过检查paper.view.center发现了这个方法。

Although I wasn't satisfied as I could not understand why this would work. 虽然我不满意,因为我无法理解为什么这会起作用。 As such, I kept looking at your code and noticed this: 因此,我一直在查看您的代码并注意到这一点:

// Add image to Paper.js canvas
var raster = new paper.Raster(image, new paper.Point(0,0));

The documentation for raster tells us the following: raster的文档告诉我们以下内容:

Raster 光栅

Creates a new raster item from the passed argument, and places it in the active layer. 从传递的参数创建一个新的栅格项,并将其放在活动层中。 object can either be a DOM Image, a Canvas, or a string describing the URL to load the image from, or the ID of a DOM element to get the image from (either a DOM Image or a Canvas). object可以是DOM Image,Canvas,也可以是描述从中加载图像的URL的字符串,或者是DOM元素的ID以从中获取图像(DOM图像或Canvas)。

Parameters: 参数:

source : HTMLImageElement / HTMLCanvasElement / String — the source of the raster — optional source :HTMLImageElement / HTMLCanvasElement / String - 栅格的来源 - 可选

position : Point — the center position at which the raster item is placed — optional position :Point - 放置栅格项目的中心位置 - 可选

My understanding is that the image is pasted on the canvas with its center at position 0,0 , also known as the top left corner . 我的理解是图像粘贴在画布上,其中心位于0,0位置,也称为左上角 Then the content of the canvas is resized, but based on its own center position. 然后调整画布的内容大小,但基于其自己的中心位置。 This pulls the image closer to the center of the canvas, but there is still a discrepancy. 这会使图像更接近画布的中心,但仍然存在差异。

Setting the center to 0,0 simply synchronizes the center points of both the image and the canvas. 将中心设置为0,0只需同步图像和画布的中心点。

There is also an alternative way, which is to paste the image at the current center of the canvas, which seems more proper: 还有一种替代方法,即将图像粘贴到画布的当前中心,这看起来更合适:

// Add image to Paper.js canvas
var raster = new paper.Raster(image, new paper.Point(canvas.width/2, canvas.height/2));

Here is the rudimentary JSFiddle to experiment. 这是实验的基本JSFiddle

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

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