Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update SVG dynamically

I have some objects inside of svg that can be clicked by user.

Is there any way to: - send information about object (id) that was clicked by user to the 'main html document'? - draw from outside document in the svg file.

Probably, my description is unclear,... I want to implement something like this:

  1. user click on any object inside of svg-image;
  2. main document will receive id of the clicked object and:
    • display some information about that object;
    • draw additional object inside of the svg-image.

Questions: how to communication from svg to document and from document to svg?

Thanks a lot, any thoughts are welcome!

P.S. Probably SVG is not the best way do that? What is better then?

EDIT: I saw recommendation regarding use of Raphael,.. but I would like to see 'native' options. (For now I'm analyzing Raphaels implementation to see that, but don't think it is doing exactly what I need).

like image 810
Budda Avatar asked May 11 '11 15:05

Budda


People also ask

Can SVG be dynamic?

As most of us know, SVGs load faster than images, are dynamically flexible for clear scaling, and generally are the preferred way of rendering anything that could be generated by vector on the web.

Is SVG a static file?

You can see that the final markup for both SVG is exactly the same. The only difference is one is static in the HTML, the other is created dynamically via javascript.

What is SVG class in HTML?

SVG is a language for describing 2D graphics in XML. Canvas draws 2D graphics, on the fly (with a JavaScript). SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element. In SVG, each drawn shape is remembered as an object.

How does SVG work?

SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded directly into HTML for display or saved to a . svg file and inserted like any other image.


2 Answers

See this example for how to get the DOM of a referenced svg from the parent document.

And here's an example of how you can call from an svg file to the parent document.

SVG is very well suited for doing what you describe.

like image 106
Erik Dahlström Avatar answered Sep 22 '22 02:09

Erik Dahlström


I'd suggest using a library like Raphaël to support your SVG building. You can attach events to DOM objects that you can get through the node property of an image component.

like image 28
Nick Avatar answered Sep 23 '22 02:09

Nick