In the application below, you can click anywhere in the scene to centre the "view" over that point.
import QtQuick 2.7
import QtQuick.Controls 2.2
ApplicationWindow {
width: 640
height: 480
visible: true
property real zoom: 1
property point offset
property point scenePosToCentreOn
Item {
id: sceneView
anchors.fill: parent
MouseArea {
anchors.fill: parent
onClicked: {
scenePosToCentreOn = scene.mapFromItem(sceneView, mouse.x, mouse.y)
offset = Qt.point(scenePosToCentreOn.x - width / 2,
scenePosToCentreOn.y - height / 2);
}
onWheel: {
var zoomFactor = Math.pow(1.4, wheel.angleDelta.y / 120.0);
var newZoom = Math.min(8.0, Math.max(0.25, zoom * zoomFactor));
zoom = newZoom;
}
}
Item {
id: scene
implicitWidth: backgroundImage.implicitWidth
implicitHeight: backgroundImage.implicitHeight
transform: [
Translate {
x: -offset.x
y: -offset.y
},
Scale {
xScale: zoom
yScale: zoom
}
]
Image {
id: backgroundImage
source: "http://cdn.akamai.steamstatic.com/steam/apps/393010/ss_29cf93db42617dd08ceb0a0bf0a4b62ad12a1cfc.1920x1080.jpg?t=1459456906"
}
Rectangle {
x: scenePosToCentreOn.x - width / 2
y: scenePosToCentreOn.y - height / 2
width: 8
height: width
radius: width / 2
color: "#fff"
}
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "darkorange"
border.width: 4
Label {
text: "Scene"
}
}
}
Label {
text: zoom.toFixed(2)
font.pixelSize: Qt.application.font.pixelSize * 2
color: "salmon"
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
}
I'd like to be able to zoom in and out on the point that is selected (scenePosToCentreOn
). It currently works when zoom
is 1
, but seems to have its origin at the top left of the screen for any other zoom
value. I suspect I'm missing something from the list of transforms, but I can't figure it out.