Skip to content

Commit 97a5d9e

Browse files
committed
Canvas: Add example of zooming at fixed point (#270)
1 parent 1846c01 commit 97a5d9e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

docs/CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ v0.10.0 (WIP):
44
From this point ed::EndCreate() and ed::EndDelete() can only be called when
55
ed::BeginCreate() and ed::BeginDelete() calls were successful.
66

7+
NEW: Canvas: Add example of zooming at fixed point (#270)
8+
79
CHANGE: Canvas: Use ImDrawCallback_ImCanvas macro as draw callback sentinel (#256), thanks @nspitko
810

911
BUGFIX: Examples: Call ed::EndCreate() and ed::EndDelete() only when ed::BeginCreate() and ed::BeginDelete() returned true

examples/canvas-example/canvas-example.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,27 @@ struct Example:
188188
canvas.SetView(drawStartPoint + ImGui::GetMouseDragDelta(1, 0.0f) * viewScale, viewScale);
189189
}
190190
else if (isDragging)
191+
{
191192
isDragging = false;
193+
}
194+
else if (!isDragging && ImGui::IsItemHovered() && io.MouseWheel)
195+
{
196+
auto mousePos = io.MousePos;
197+
198+
// apply new view scale
199+
auto oldView = canvas.View();
200+
auto newViewScale = viewScale * powf(1.1f, io.MouseWheel);
201+
canvas.SetView(viewOrigin, newViewScale);
202+
auto newView = canvas.View();
203+
204+
// calculate origin offset to keep mouse position fixed
205+
auto screenPosition = canvas.FromLocal(mousePos, oldView);
206+
auto canvasPosition = canvas.ToLocal(screenPosition, newView);
207+
auto originOffset = (canvasPosition - mousePos) * newViewScale;
208+
209+
// apply new view
210+
canvas.SetView(viewOrigin + originOffset, newViewScale);
211+
}
192212

193213
viewRect = canvas.ViewRect();
194214

0 commit comments

Comments
 (0)