From SVG to Canvas – A new way of building interactions
I&#x27;m interested in Canvas - I write a lot of data viz in SVG. I find it to have lots of warts, but provide lots of value. I&#x27;m interested by this article and wish to challenge it too, here&#x27;s my thoughts.<p>Maybe I&#x27;m an idiot though - where is this library? I can&#x27;t find it on their GitHub or linked from the articles<p>&gt; In the SVG world, we use CSS z-index to place the DragBox at the back and the SelectionFrame at the front.<p>Not a great start IMO - SVGs don&#x27;t support z-index. The front-to-back ordering z-index influences in HTML is decided in SVG directly by the order in the HTML within the &lt;svg&gt; element (in reverse - later elements are &quot;on top&quot;). What they could mean to say, if this isn&#x27;t entirely inaccurate, is that &quot;we render multiple SVGs and arrange them with z-index&quot;, which... I also find strange. I don&#x27;t want to read their source code but I also don&#x27;t find this to be giving them huge credibility.<p>&gt; Performance<p>&gt; Having every element produce a whole load of DOM elements for React to manage becomes a problem for performance when every element attaches event handlers, has to manage their lifecycles, observe state, etc., etc.<p>My concerns are mounting. This is fundamentally the problem React solves, not one it creates.<p>The amount of layers they picture in their example is not huge. I had the target to get 60fps on a Graph with 1000 nodes + smart edge routing, and you can do lots of interactions in the graph - each node can be moved independently or in a group, selected, you can drag a background box to select objects geometrically intersecting, and you can create new connections with drag, you know - basics. Not sure how performance is impacting them at an order of magnitude less, unless they are struggling with React itself.<p>&gt; Interactivity Limitations<p>&gt; with the text taking up so much space it’s very easy to accidentally drag the text instead<p>&gt; This was a very longstanding bug<p>I interpret this as them saying &quot;the text background was not displayed to users, so while it appeared the mouse was over the polygon, it was actually over the text background, therefore the text was dragged&quot;. Seems to me hover state would give the user the hints they need here, but I look forward to the Canvas solution<p>&gt; Maintainability<p>Once again I really can&#x27;t help but interpret this as a weak handle on React.<p>Onto the solution.<p>&gt; The new system: per-state InteractionHandlers<p>Honestly, the fact they had to leave React to manage this once again speaks to bad React practices. Have they never heard of Context&#x2F;Provider?<p>&gt; The general idea is this: there are a lot of different things you can do in Felt, but at any given time, the number of things you can do is limited. If we can therefore define which interactions are active at any given time, and also manage the flow of events through the handlers we should be in a good place<p>Yes, this is good architecting. However, the only thing they describe here that piques my interest is this:<p>&gt; Here is the full sequence of events that an InteractionHandler can process, with the ability to break the chain at any point, and prevent other handlers in the stack from being called.<p>&gt; &lt;graph showing how mouse events are managed manually, enabling mouse movement to correlate to available mouse events&gt;<p>This is a big pain point in React - very difficult to manage onMouseDown separately from onClick. Usually I accomplished this by overloading onMouseDown and onMouseUp to call the actual dev-user-available action handlers, but their described result is more graceful.<p>&gt; Goal 1: Interactions should be decoupled from each other<p>&gt; Each interaction handler has no knowledge of any other. The knowledge about how to prevent “collisions” in interactions resides with the map of handlers and the manager.<p>Right, but why couldn&#x27;t this be done in React?<p>&gt; Goal 2: Interactions should be performant<p>&gt; Because there is only one handler per feature as opposed to a bunch of handlers per element, there is a lot less allocation of resources.<p>Sounds and looks cool, is the implication that there is only O(feature count) event listeners for the Canvas?<p>I&#x27;ll have to profile my library for this<p>&gt; Goal 3: Interactions should be decoupled from access control<p>&gt; Access control is now simply a case of writing a different list of interaction handlers for each access level. There’s no longer any isEditor code littered throughout<p>&gt; Goal 4: Interactions should be decoupled from rendering<p>Yikes. I think they really conflated bad architecture with React problems a lot in this article.<p>Alright though, I see some clear wins at the end:<p>&gt; Goal 5: Complex interactions should be achievable<p>&gt; &lt;video shows that text is interacted with not as a bounding rect but character polygons&gt;<p>This is a win, and one they obviously wanted. It&#x27;s probably true this isn&#x27;t possible with the DOM, unless you render each character individually as a polygon or something that would undoubtedly be insanely painful<p>&gt; Doing this with an element-centric approach is nigh-on impossible. There’s no way (as far as we know) to say to a DOM element: “receive clicks, but pass-through mousedowns and drags.”<p>I agree with this claim, but wish they would elaborate what these use cases look like.<p>&gt; OK, there are some hidden details there such as ...<p>&gt; we maintain a spatial index of our elements using rbush which we query for coarse intersections with the cursor before performing more accurate hit tests using Turf.js.<p>Right, this all sounds quite graceful, indeed it sounds a bit like implementing a graphics system. It sounds like it met some of their goals too, and enabled some long-desired features the DOM didn&#x27;t make possible.<p>But their criticisms of React &amp; SVG are really weak here, and it sounds to me like they never properly decided how the system should behave and what parts should interact in the DOM world, and their issues fell out of that.<p>I also think that you should really not talk so much about z-index with SVG unless you make it more clear how those things could even be related.<p>Overall, I think they had a great team that implemented a great solution that aligned with their end-goals, but the work they replaced doesn&#x27;t sound like it was even well-aligned with the framework it was implemented in, let alone their end goals. I would not agree with their recommendation to move to Canvas if SVG doesn&#x27;t &quot;feel right&quot; until you do more analysis of why your solution is failing and what options are available.