124 points · by andreykh · 10y ago · 43 comments
I'm interested, why does SVG seem to de the default choice for web graphics? And is there something like d3 that makes working with Canvas more accessible?
http://bl.ocks.org/syntagmatic/954b31aa8b8beb91b30ccb0c9e57f...
Notice the nodes are draggable, an interaction supported by the new simulation.find https://github.com/d3/d3-force/#simulation_find
Because SVG IS web graphics (emphasis on "web"). It lives on the DOM, it's objects have event callbacks, can be styled in CSS-fashion, have nested items, links, etc.
In contrast, Canvas is just a bitmap pane that you can draw own in an imperative way through a JS API -- and everything above that has to be programmed by you or a third party lib in an ad-hoc way.
The most well known WebGL library is https://github.com/mrdoob/three.js/ Which has been used for many examples like this http://armsglobe.chromeexperiments.com/
But you could look at the libraries focused on 2d like these http://paperjs.org/ http://fabricjs.com/ But the larger 2d canvas libraries also have much of the same abilities. http://createjs.com/ - http://codepen.io/ianwhitfield/pen/BKOJmj https://github.com/pixijs/pixi.js - http://anvaka.github.io/ngraph/examples/pixi.js/06%20-%20Pac...
If you are just rendering vectors like an icon image, there is no harm in using SVG. If you want access to nodes on a page with a JavaScript library like jQuery, SVG is still the best option. For everything else SVG is not the most performant and is not able to deliver on many graphics abilities that the Canvas tag is capable of. I respect that in most visualizations it doesn't matter and it might be easier but it doesn't mean SVG is the best thing to use for all graphics.
Graphs, though? Graphs?
The sweet spot for SVGs is when you have relatively small data set sizes, need interactivity, and update the data very little. There's also the question of how competently different browser vendors handle SVG and animation--if you're on a nice desktop or laptop, it'll be great!
Once any of those things change, you need to start looking at using raster or GPU graphics. If your dataset updates frequently (say, faster than every few hundred milliseconds) or is large (say, larger than a few thousand points) or needs to reliably run on mobile devices or older workstations, the SVG approach will show issues pretty quickly.
Oh, and it's not trivial performance gains, unless you have your head so far up your ass that you don't watch how balls slow the average user's device and browsing experience is. Nor is it at the expense of easily-writable or modifiable code, unless you're a hack script-kiddie who cannot fathom how to modularize their code and write clear solutions to problems.
I'm not even going to apologize for this, because there is just so much garbage advice and bad practices going on in the frontend right now because some chucklefuck found D3 and has decided that it has solved all of their charting and drawing problems forever--and never notices how badly their cutesy demos perform in the wild.
Nothing "hack" about vector graphics, and nothing about graphics that necessitates "performance" above all else.
We're not all making AAA games.
Brzzt, wrong.
Computers are, at the end of the day, meant for crunching numbers. Graphics is perhaps one of the last pure workloads that is about crunching numbers--it's probably the best match of the advances of the last 20 years to a problem domain we could hope for.
If your code cannot quickly do graphics, you have failed to competently make use of the user's resources. You are bad and are wasting their time and power, and in the one place where that inefficiency isn't justifiable.
> We're not all making AAA games.
Sure but we're also not all running MacBook Pros or GTX1080 cards on Xeons, now are we?
There's this mindset that high-performance graphics somehow only benefits gamers and games, and that's wrong.
Especially in the (mostly single-threaded, highly-marshalled) graphics environment of the browser, it is really easy to write code that will choke older machines to death and murder mobile devices.
This can be done with a large chart dataset, a quickly updating visualization, or any number of other reasonable and normal and non-game use cases.
Disclaimer: I work at AnyChart.
I would love to have a library that can "import" an SVG generated from, say, inkscape or Sketch and then allows to animate just a few parts of that vector -- this would be useful in this context too -- draw Bender, and simply move the eyes from JS.
[1] http://playground.anychart.com/gallery/7.11.1/Graphics/Bende... [2] http://playground.anychart.com/gallery/7.11.1/Graphics/Bende...
For example, https://api.anychart.com/7.11.1/anychart.palettes.DistinctCo... - why would you want to attach listeners to palette colors? There might be a great reason, or those methods might just have been inherited from the base class, but the documentation is no to your understanding help whatsoever. The example is for the base class rather than the palette object.
This is actually a good example of bad documentation.