Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying vector graphics in a browser

I need to display some interactive (attaching with DOM listeners etc. and event handling) vector graphics in web site I am working on. There is a W3C recommendation for SVG though this format is still not recognized by Internet Explorer support of which is a must (for a public website). IE handles VML though and there are even javascript libraries that do some canvas-like drawing depending on a browser (SVG vs. VML) - excanvas, GFX of Dojo Toolkit and more. That would be nice and acceptable though none of them can display an SVG image from the given markup.

So the question actually consists of several parts:

  1. Are there any cross-browser Javascript libraries that display vector graphics from given markup (not obligatory SVG) and offer availability to attach to DOM events?
  2. If there are not, which of the most pupular browser-embedded technologies would be most suitable for doing such a task? I can choose from Flex/Flash, Java applet. Silverlight is not an option because of Windows lock-in.

[EDIT] Thank you all for your comments/suggestions. Below are just some my random notes/conclusions on this matter:

  • The level of interactivity I need is ability to detect DOM events on the vector image being displayed - mouseover, mouseout, click etc. - and ability to react on them like changing background color, displaying dialog etc.
  • The idea of sticking with SVG format is quite well as it is native on many browsers except the most popular one - IE. After some experimenting with displaying dynamic SVG I realized that IE version 7 the most problematic. There's too much hassle because of browser incompatibilities.
  • Cake seems a great Javascript framework, though I could not get the examples working on IE7.
  • Java Applets - I liked that idea the most as I though I could use the Apache Batik library, a quality SVG renderer. However, Batik is very big library and I cannot afford deploying an applet that weights few megabytes.
  • I decided to stick with the Flex option. I found a nice vector graphics library Degrafa. It uses its own markup format however it recognizes SVG path notation, so in my case it is going to be quite easy to transform my SVGs using XSLT or just parsing them.

[EDIT 2] Some more comments appeared. I'd like to clarify that by "Windows lock-in" I mean the situation that Silverlight would normally run on Windows, more specifically, IE. I doubt it is an accepted solution (like Flash or Java Applet, for instance) on other systems. Yes, I have no doubt that one is able to launch Silverlight app on any system though I fear it would be too much effort for an average user.

@Akira: Have you had any problems with those "SVG renderers" on IE7? I get thrown Javascript errors all the time.

like image 869
martinsb Avatar asked Aug 13 '08 09:08

martinsb


People also ask

How do I display a vector in HTML?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.

Can websites display SVG?

Using an <img> tagThe <img> tag is now supported across all major browsers that support SVG (IE9+). Both available.

Can you use vector images on websites?

Vector images are defined using algorithms — a vector image file contains shape and path definitions that the computer can use to work out what the image should look like when rendered on the screen. The SVG format allows us to create powerful vector graphics for use on the Web.

What file type do you use for a vector graphic for a web page?

svg: The Scalable Vector Graphics format is based in XML (a markup language used widely across the Internet that's readable by both machines and humans). It's useful for the web, where it can be indexed, searched, and scripted.


4 Answers

Safari, Opera and Firefox all support SVG natively (eg. without plugins) to varying degrees of completeness and correctness, including the ability to script the svg from javascript.

There's also the canvas element which is now being standardised in html5, and is already supported in the above browsers as well (with various quirks in certain edge cases due to relatively recent changes in the html5 draft).

Unfortunately any standards based approach is kind of destroyed by IE's willful disregard of what is happening outside its own ecosystem, however there are a number of libraries that try to convert canvas/svg into VML (IE's proprietary vector language) such as iecanvas.

[Edit: whoops, i forgot my favourite js library -- Cake! which can parse and display svg in canvas, and believe supports IE as well]

[Yet another edit: Cake actually has a demo doing what i think you want to do]

like image 164
olliej Avatar answered Sep 28 '22 21:09

olliej


Take a look at the Raphael Javascript library. It's early days but it looks very promising.

I remember the IE roadmap that had SVG support listed in IE7.2.

Depends on how interactive you want it?

like image 24
graham.reeds Avatar answered Sep 25 '22 21:09

graham.reeds


Can you clarify what you mean by the "Windows lock-in" thing with Silverlight? It runs on Windows and MacIntel, and the vector portions run just fine on Linux with the Moonlight plugin.

Were you thrown off by the lack of Amiga support?

like image 2
Jon Galloway Avatar answered Sep 28 '22 21:09

Jon Galloway


Have a look at the new Canvas element which has been implemented in many browsers. I heard also that there is an ActiveX control for IE that implements the Canvas element too.

Update: Wait, you already said that. I should read the whole question next time! :)

like image 1
Greg Hewgill Avatar answered Sep 29 '22 21:09

Greg Hewgill