I am building a CAD app that runs in a browser.
E.G: I thought of rasterizing paths that are not directly selected and worked on, save their path data into LocalStorage and as soon as
the user clicks on them to manipulate them, to re-import back their
path data. That would minimize the memory usage on non-worked-on
This can be done with rasterization in a process/technique similar to Bitmap Caching.
The issue with high-node count designs is that rendering them causes the rendering engine to groan. The browser has to traverse their nodes and blit those pixels on the canvas.
So here's a nice solution:
The solution is to replace the vectors with images,rasterizing them - only when rendering, but still keeping the original shape below it's image copy, in a hidden state only when inactive(not being currently manipulated).
On clicking the images - we remove them and toggle the visibility of the original shape. This way inactive shapes are rendered as images and active shapes are released from their bitmap representation and act as vectors, free to be manipulated around. When not active they just sit there invinsible with their Raster copy on top of them.
This allows the engine to keep the vector representation of the shapes but avoids rendering them as vectors - instead images that look similar to them are layered on top of them.
1000's of path commands are essentially replaced by a single image - but only when rendering - the original path actually exists as an object in the Scene Graph, or whatever type of DOM you are using
The trick is to perform the rasterization in groups - group 10-15 shapes together and rasterize them as a single image. This keeps the raster count low. On clicking an image - we can release the whole group or just the item that was clicked on.
When rasterizing a group, we can simply attach a
click handler on it, so when clicked we toggle bitmap with vector. Images do not behave the same as vectors when hit testing - images are
squares by nature and cannot be assymetrically hit-tested. While a vector considers it's edges to be on it's path boundaries - an image considers it's boundaries to be it's whole bounding box. The solution is when clicking on the image to actually hit-test the click point with the vector path below the image - if it returns true then perform a release.
The rest are optimizations and making the whole thing smart - Currently I allow my users to press a button and perform this optimization to all the shapes on the canvas - Maybe this can be done in a more automatic/intuitive way.