@startuml
title Canvas/ViewPort: алгоритм вычисления видимой области и редроу

start
:Get zoomed document rect\n(getZoomedImageSize);
:Read viewport rect + scroll offset;
:Compute visible width/height\nmin(viewport, image size);
:Compute raw visible x/y\nfrom scroll - image offset;
:Clamp x/y into image bounds;
:Store Rect_i{x,y,w,h} as _visible_rect;

if (visible rect changed\nOR texture is null?) then (yes)
  :request redraw;
else (no)
  :no redraw needed;
endif

if (redraw pending?) then (yes)
  :Read render quality %;
  :Convert area-quality to side-scale\nscale = sqrt(q/100);
  :Scale full canvas size;
  :Scale visible rect;
  :Clamp scaled rect to scaled canvas;
  if (scaled visible rect valid?) then (yes)
    :Render only scaled visible area\nthrough RenderEngine;
    :Update texture + stats + throttle;
  else (no)
    :Drop texture / skip rendering;
  endif
else (no)
  :Keep previous frame;
endif
stop

@enduml
