java - Zoom Levels with the JavaFX 8 Canvas -
i have following code within javafx 8 application, meant control scale of transform of canvas element added scene:
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 implement transformer ones meant modify // canvas's transform based on input , 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 animationtimer's handle() @override public void update(canvas canvas, affine transform, long now, long delta) { // when scroll up, example, integer +1 added list // (and -1 down). when method called, list cleared. list<integer> pendingscrolls = mouse.getandremovependingscrolls(); if (! pendingscrolls.isempty()) { // runs 'transform.inversetransform(positionofmouserelativetocanvas)' point2d pivot = mouse.gettransformedposition(); (int direction : pendingscrolls) { boolean = direction > 0; // purpose of keep scaling minuscule amount // , check each time if we've went out of bounds of // min_scale or max_scale (int = 0; < 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); } } } } }
the issue transform.appendscale()
, says, appends transform, , floating point precision, zoom levels different every time scroll.
because of reasons, must modify graphicscontext
transform, not canvas directly (in other words, translate, must use transform.settx(...)
instead of canvas.settranslatex(...)
.
the solution set transform's scale, while taking account pivot point (which mouse cursor). so, question how that?
the problem not in float point precision, rather appending scale of 0.9 not decrease scale same amount 1.1 increases it. solution, then, replace scrolling out functionality inverse of scrolling in functionality, so:
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(); (int direction : pendingscrolls) { boolean = direction > 0; if (! && scalelevel == 0 || && scalelevel == max_scale_levels) continue; scalelevel += ? 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; } } } } } }
Comments
Post a Comment