Skip to content

Multi setNodes

Change the nodes as many times as you like in one event handler - move them, recolour them, relabel them - and the canvas draws the result once, after the handler, the way several setNodes calls in one React tick apply together. A change made outside a render of the page, such as on a timer, is drawn when the page calls Refresh.

Node 1
Node 2
Node 3
Node 4
Node 5
bit Blow

Try it

  • Shuffle, recolour and relabel makes three passes over the nodes - new positions, new colours, new labels - and you see them land together, with the edges following, in one redraw.
  • Drift for 2 seconds nudges every node ten times a second from a timer. With Call Refresh while drifting ticked the nodes glide; untick it and run it again: nothing moves until the timer stops, then every node jumps to where it ended up.
  • Reset puts the row back and fits it in view.

How it works

  • The canvas edits and draws the page's own List<BlowNode>, so the handler simply assigns Position, Style and Label on the nodes, pass after pass. Nothing is drawn in between: Blazor renders the page once the handler returns, the page hands the canvas its parameters again, and the canvas re-reads the lists and re-measures the nodes, whose labels may have changed their size.
  • So a change made inside an event handler of the page needs no call at all. FitView() after the shuffle only brings the scattered nodes back into view.
  • The drift runs on a PeriodicTimer inside the handler. Blazor renders an async handler when it first waits and when it finishes, not after each tick, so the changes in between are invisible until something renders the canvas.
  • Refresh(remeasure: false) renders the canvas on its own, re-reading the lists, without rendering the page. Pass remeasure: true when the change can alter a node's size or handles, so they are measured again first. A change from a real timer callback or a message from a server, outside the renderer's context, goes through InvokeAsync first.
  • Reset rebuilds the lists in place - Clear and AddRange - rather than assigning new ones, and fits the view to the row.
An unhandled error has occurred. Reload 🗙