Skip to content

Commit

Permalink
Canvas: Add example of zooming at fixed point (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Dec 27, 2023
1 parent 1846c01 commit 97a5d9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ v0.10.0 (WIP):
From this point ed::EndCreate() and ed::EndDelete() can only be called when
ed::BeginCreate() and ed::BeginDelete() calls were successful.

NEW: Canvas: Add example of zooming at fixed point (#270)

CHANGE: Canvas: Use ImDrawCallback_ImCanvas macro as draw callback sentinel (#256), thanks @nspitko

BUGFIX: Examples: Call ed::EndCreate() and ed::EndDelete() only when ed::BeginCreate() and ed::BeginDelete() returned true
Expand Down
20 changes: 20 additions & 0 deletions examples/canvas-example/canvas-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,27 @@ struct Example:
canvas.SetView(drawStartPoint + ImGui::GetMouseDragDelta(1, 0.0f) * viewScale, viewScale);
}
else if (isDragging)
{
isDragging = false;
}
else if (!isDragging && ImGui::IsItemHovered() && io.MouseWheel)
{
auto mousePos = io.MousePos;

// apply new view scale
auto oldView = canvas.View();
auto newViewScale = viewScale * powf(1.1f, io.MouseWheel);
canvas.SetView(viewOrigin, newViewScale);
auto newView = canvas.View();

// calculate origin offset to keep mouse position fixed
auto screenPosition = canvas.FromLocal(mousePos, oldView);
auto canvasPosition = canvas.ToLocal(screenPosition, newView);
auto originOffset = (canvasPosition - mousePos) * newViewScale;

// apply new view
canvas.SetView(viewOrigin + originOffset, newViewScale);
}

viewRect = canvas.ViewRect();

Expand Down

0 comments on commit 97a5d9e

Please sign in to comment.