JavaFX 8 的缩放级别 Canvas

Zoom Levels with the JavaFX 8 Canvas

我的 JavaFX 8 应用程序中有以下代码,用于控制添加到场景中的 canvas 元素的变换比例:

import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.transform.Affine;
import me.mazeika.dengine.editor.input.Mouse;

import java.util.List;

// classes that implement Transformer are ones that are meant to simply modify
// the canvas's transform based on some input and nothing else
public class ScrollTransformer implements Transformer
{
    private static final double MIN_SCALE = .1;
    private static final double MAX_SCALE = 5;

    private double currentScale = 1;

    // called every frame from an AnimationTimer's handle()
    @Override
    public void update(Canvas canvas, Affine transform, long now, long delta)
    {
        // when you scroll up, for example, the integer +1 is added to this list
        // (and -1 for down). When this method is called, that list is cleared.
        List<Integer> pendingScrolls = Mouse.getAndRemovePendingScrolls();

        if (! pendingScrolls.isEmpty())
        {
            // runs 'transform.inverseTransform(positionOfMouseRelativeToCanvas)'
            Point2D pivot = Mouse.getTransformedPosition();

            for (int direction : pendingScrolls)
            {
                boolean up = direction > 0;

                // the purpose of this is to keep scaling by a minuscule amount
                // and check each time if we've went out of the bounds of
                // MIN_SCALE or MAX_SCALE
                for (int i = 0; i < 1000; i++)
                {
                    double toAppend = (up ? 1.0001 : .9999);

                    currentScale += toAppend - 1;

                    if (currentScale <= MIN_SCALE)
                    {
                        currentScale = MIN_SCALE;
                        break;
                    }
                    else if (currentScale >= MAX_SCALE)
                    {
                        currentScale = MAX_SCALE;
                        break;
                    }

                    transform.appendScale(toAppend, toAppend, pivot);
                }
            }
        }
    }
}

问题是 transform.appendScale(),就像它说的那样,追加到变换,并且由于浮点精度,每次滚动时缩放级别都会略有不同。

由于某些原因,我必须修改GraphicsContext转换,而不是直接canvas(换句话说,要翻译,我必须使用transform.setTx(...) 而不是 canvas.setTranslateX(...).

解决方案是设置变换的比例,同时考虑轴心点(即鼠标光标) .所以,我的问题是我该怎么做?

问题不一定出在浮点精度上,而是附加 0.9 的小数位数不会减少与 1.1 增加的小数位数相同的量。那么,解决方案就是简单地用滚入功能的 inverse 替换滚出功能,如下所示:

import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.transform.Affine;
import javafx.scene.transform.NonInvertibleTransformException;
import javafx.scene.transform.Scale;
import me.mazeika.dengine.editor.input.Mouse;

import java.util.List;

public class ScrollTransformer implements Transformer
{
    private static final int MAX_SCALE_LEVELS = 80;

    private int scaleLevel = 30;

    @Override
    public void update(Canvas canvas, Affine transform, long now, long delta)
    {
        List<Integer> pendingScrolls = Mouse.getAndRemovePendingScrolls();

        if (! pendingScrolls.isEmpty())
        {
            Point2D pivot = Mouse.getTransformedPosition();

            for (int direction : pendingScrolls)
            {
                boolean up = direction > 0;

                if (! up && scaleLevel == 0 || up && scaleLevel == MAX_SCALE_LEVELS) continue;

                scaleLevel += up ? 1 : -1;

                Scale scale = new Scale(1.1, 1.1, pivot.getX(), pivot.getY());

                if (up)
                {
                    transform.append(scale);
                }
                else
                {
                    try
                    {
                        transform.append(scale.createInverse());
                    }
                    catch (NonInvertibleTransformException e)
                    {
                        e.printStackTrace();
                        return;
                    }
                }
            }
        }
    }
}