Table of Contents

Class RenderContext

Namespace
FlowGraph.Avalonia.Rendering
Assembly
FlowGraph.Avalonia.dll

Shared rendering context containing settings, viewport state, and coordinate transformation utilities. Used by all visual managers to ensure consistent rendering behavior.

TRANSFORM-BASED RENDERING (Phase 2):

Renderers create visuals at "logical size" (unscaled). The MatrixTransform on MainCanvas handles all zoom/pan transformations. This enables O(1) zoom operations.

  • Scale always returns 1.0 - use for visual sizing
  • ViewportZoom returns actual zoom - use for calculations
  • InverseScale returns 1/zoom - use for constant-size elements
public class RenderContext
Inheritance
RenderContext
Inherited Members

Constructors

RenderContext(FlowCanvasSettings?)

Creates a new render context with the specified settings.

public RenderContext(FlowCanvasSettings? settings = null)

Parameters

settings FlowCanvasSettings

Canvas settings. If null, default settings are used.

Properties

InverseScale

Gets the inverse scale factor for elements that should stay constant screen size.

Usage: Apply as a ScaleTransform to elements like:

  • Resize handles (should stay same size regardless of zoom)
  • Edge endpoint handles
  • Selection indicators
handle.RenderTransform = new ScaleTransform(context.InverseScale, context.InverseScale);
handle.RenderTransformOrigin = new RelativePoint(0.5, 0.5, RelativeUnit.Relative);
public double InverseScale { get; }

Property Value

double

Scale

Gets the logical scale factor for creating visuals.

ALWAYS RETURNS 1.0

In transform-based rendering, visuals are created at logical size (unscaled). The MatrixTransform on MainCanvas handles zoom. This enables O(1) zoom operations since visuals don't need to be recreated when zoom changes.

Usage: Use for visual sizing (Width, Height, FontSize, etc.)

For actual zoom: Use ViewportZoom instead.

public double Scale { get; }

Property Value

double

Remarks

This is a breaking change from the previous behavior where Scale returned viewport.Zoom. Custom renderers that used context.Scale for calculations should switch to ViewportZoom.

Settings

Gets the canvas settings.

public FlowCanvasSettings Settings { get; }

Property Value

FlowCanvasSettings

Viewport

Gets the current viewport state.

public ViewportState? Viewport { get; }

Property Value

ViewportState

ViewportZoom

Gets the actual viewport zoom level.

Usage:

  • Calculations that need to know the current zoom (e.g., level-of-detail decisions)
  • Hit testing calculations
  • Computing inverse scale for constant-size elements

DO NOT USE for visual sizing - use Scale instead.

public double ViewportZoom { get; }

Property Value

double

Methods

CanvasToScreen(Point)

Transforms a canvas point to screen coordinate.

public Point CanvasToScreen(Point canvasPoint)

Parameters

canvasPoint Point

Point in canvas space.

Returns

Point

The corresponding screen coordinate.

CanvasToScreen(double, double)

Transforms a canvas coordinate to screen coordinate.

USAGE GUIDANCE:

  • DO NOT USE when positioning visual elements with Canvas.SetLeft/SetTop - elements on MainCanvas should be positioned in canvas coordinates, the MatrixTransform handles viewport conversion
  • DO USE when rendering with DrawingContext (backgrounds, direct rendering) - DrawingContext needs screen coordinates
  • DO USE for calculations that need to know screen-space dimensions (hit areas, label placement)

CORRECT: drawingContext.DrawRectangle(brush, pen, CanvasToScreen(rect))

INCORRECT: Canvas.SetLeft(element, CanvasToScreen(x, y).X) - causes double transform!

public Point CanvasToScreen(double canvasX, double canvasY)

Parameters

canvasX double

X coordinate in canvas space.

canvasY double

Y coordinate in canvas space.

Returns

Point

The corresponding screen coordinate.

CanvasToViewport(Point)

Transforms a canvas point to viewport coordinates. Alias for CanvasToScreen(Point) for API consistency.

public Point CanvasToViewport(Point canvasPoint)

Parameters

canvasPoint Point

Returns

Point

CanvasToViewport(double, double)

Transforms canvas coordinates to viewport coordinates. Alias for CanvasToScreen(double, double) for API consistency.

public Point CanvasToViewport(double canvasX, double canvasY)

Parameters

canvasX double
canvasY double

Returns

Point

GetVisibleBoundsWithBuffer()

Gets the visible area in canvas coordinates, expanded by the virtualization buffer. Used for culling nodes/edges outside the viewport.

public Rect GetVisibleBoundsWithBuffer()

Returns

Rect

The visible rect with buffer, or an infinite rect if no viewport is set or view size is 0.

IsInVisibleBounds(double, double, double, double)

Checks if a rectangular area intersects with the visible viewport (with buffer).

public bool IsInVisibleBounds(double x, double y, double width, double height)

Parameters

x double

X coordinate of the rect.

y double

Y coordinate of the rect.

width double

Width of the rect.

height double

Height of the rect.

Returns

bool

True if the rect intersects the visible area.

ScaleValue(double)

Scales a value by the logical scale factor (always 1.0).

DEPRECATED: This method now returns the input value unchanged.

In transform-based rendering, visual sizing should use unscaled values.

For calculations that need actual zoom, use value * ViewportZoom.

[Obsolete("Use raw values for visual sizing. For zoom-aware calculations, use value * ViewportZoom.")]
public double ScaleValue(double value)

Parameters

value double

The value to scale.

Returns

double

The input value unchanged (Scale is always 1.0).

ScreenToCanvas(Point)

Transforms a screen point to canvas coordinate.

public Point ScreenToCanvas(Point screenPoint)

Parameters

screenPoint Point

Point in screen space.

Returns

Point

The corresponding canvas coordinate.

ScreenToCanvas(double, double)

Transforms a screen coordinate to canvas coordinate.

USAGE GUIDANCE:

  • PREFER using e.GetPosition(_mainCanvas) for hit testing - gives canvas coords directly
  • DO USE when you have screen coordinates (from GetPosition(_rootPanel)) and need canvas coords
  • DO USE for inverse calculations from CanvasToScreen operations

CORRECT: var canvasPos = e.GetPosition(_mainCanvas) (no conversion needed)

ALSO CORRECT: var canvasPos = ScreenToCanvas(e.GetPosition(_rootPanel))

public Point ScreenToCanvas(double screenX, double screenY)

Parameters

screenX double

X coordinate in screen space.

screenY double

Y coordinate in screen space.

Returns

Point

The corresponding canvas coordinate.

SetViewport(ViewportState?)

Sets the viewport state for coordinate transformations.

public void SetViewport(ViewportState? viewport)

Parameters

viewport ViewportState

The viewport state to use.

UpdateSettings(FlowCanvasSettings)

Updates the settings. Call this when FlowCanvasSettings property changes.

public void UpdateSettings(FlowCanvasSettings settings)

Parameters

settings FlowCanvasSettings

The new settings to use.

ViewportToCanvas(Point)

Transforms a viewport point to canvas coordinates. Alias for ScreenToCanvas(Point) for API consistency.

public Point ViewportToCanvas(Point viewportPoint)

Parameters

viewportPoint Point

Returns

Point

ViewportToCanvas(double, double)

Transforms viewport coordinates to canvas coordinates. Alias for ScreenToCanvas(double, double) for API consistency.

public Point ViewportToCanvas(double viewportX, double viewportY)

Parameters

viewportX double
viewportY double

Returns

Point