Skip navigation.

ed.blog

You will be vectorized.

Getting screen boundingboxes in svg

, , ,

Last time we explored how to get boundingboxes of any svg element. Today I'd like to expand this a bit, and to show what the client (or screen) boundingbox can be used for.

How to get the screen boundingbox

Here's a simple function that returns the boundingbox of an element as a 'rect' element. If the 'rect' element is inserted just after the element that we got a boundingbox for it would in most cases cover that element.

var svgns = "http://www.w3.org/2000/svg";
function getScreenBBoxAsRectElement(elm)
{
  var bb = elm.getScreenBBox();
  var rect = document.createElementNS(svgns, "rect");
  rect.x.baseVal.value = bb.x;
  rect.y.baseVal.value = bb.y;
  rect.width.baseVal.value = bb.width;
  rect.height.baseVal.value = bb.height;
  return rect;
}
As you see from last time, it only changed one tiny thing: getBBox became getScreenBBox. However, not all browsers have native support for the getScreenBBox method so we have to provide a scriptbased fallback solution. This additional scriptfile can be dynamically loaded by another script if the native method is found to be missing.

Here's an example of how to show the boundingbox as a border around any element in an svg, along with the values in text format. The boundingbox in userspace is shown with a red border, the screen boundingbox is shown with blue border. Your browser or RSS reader does not support svg, please visit the blogpost using a modern web browser to see this image.

The script that shows the boundingboxes and their dimensions can be viewed here. To use it in another svg file just download that script and the getScreenBBox fallback script to the same directory, and add a 'script' element to the svg file you want to use it in, like this:

  ...
  <script xlink:href="showbboxdims.js"/>
  ...
Tested and works in Opera 9+, Firefox 3, Safari 4.

How to get the boundingbox of an svg element

,

A common scenario is that you have an svg file (which often has a 'viewBox' attribute) created in a graphics editor like Inkscape, and now want to make this static svg file interactive or animated. Another scenario is that you want to place/layout elements dynamically and need to know the positions and size of some given elements.

Prerequisites

First it's important to at least be somewhat acquainted with the various coordinate systems at play inside an svg file.

The initial viewport (sometimes referred to as the client- or screen-) coordinate system
This is the actual coordinate system that you see on your computer screen, often defined in pixels. You will need to use this when handling mouseevents for example, since they are going to be in this coordinate system, and they may need to be converted into userspace.
The userspace coordinate system
This is the coordinate system at the place of any given element in the svg file. It depends on what transforms have been specified as well as what the viewport coordinate system specified by the parent svg element(s) and the 'viewBox' if any.

The fun stuff

In order to get the geometric boundingbox of an svg element you can call the getBBox DOM method. This method returns a rectangle DOM object that corresponds to the boundingbox of the element in user units. The returned DOM object is called an SVGRect, and has the following four properties that can be accessed: x, y, width and height.

Note: The boundingbox object is a snapshot of the element's boundingbox at the time the getBBox method was called. Changing the values of the boundingbox has no effect on the visual appearance of the element.

Here's a simple function that returns the boundingbox of an element as a 'rect' element. If the 'rect' element is inserted just after the element that we got a boundingbox for it would in most cases cover that element.

var svgns = "http://www.w3.org/2000/svg";
function getBBoxAsRectElement(elm)
{
  var bb = elm.getBBox();
  var rect = document.createElementNS(svgns, "rect");
  rect.x.baseVal.value = bb.x;
  rect.y.baseVal.value = bb.y;
  rect.width.baseVal.value = bb.width;
  rect.height.baseVal.value = bb.height;
  return rect;
}

Here's an example of how to show the boundingbox as a border around any element in an svg. Your browser or RSS reader is uncapable of showing this svg, please visit the blogpost using a modern web browser to see this image.

The script that shows the boundingbox as a red rect can be viewed here. To use it in another svg file just download that script and add a 'script' element to the svg file you want to use it in, like this:

  ...
  <script xlink:href="showbbox.js"/>
  ...
Tested and works in Opera 9+, Firefox 3 and Safari 3.

SVG Open 2008

, ,

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here. After nearly two weeks of Bavarian bratwurst, beer and scalable pretzels (though most were of the lesser kind) you'd almost figure it'd take me at least until all the food was digested to make a blogpost about the SVG Open conference, right? :wink: Anyway, without further delay here are some random thoughts about the SVG Open 2008 conference, which was held this year in Nuremberg, Germany.

  • Andrea Cassera presented a webapp for visualizing historic volcanic activity using svg, which was demo:ed in Opera 9.5. Currently not publicly available in the version that was shown at the conference.
  • Shaun Roe gave a very interesting presentation of a tool called SEDUX which transforms XML data files from the Atlas experiment at CERN into an interactive SVG webapp by using XSLT and clientside scripting. See the paper, and example. Note that Opera 9.6 is required for proper display of the example.
  • Yet another cool example of presenting data was the GPS track webapp by Olaf Schnabel.
  • Zack Rusin talked about how KDE is using SVG and how he'd like to see SVG improved in the future.

The workshops were also quite interesting:
  • Jon Cruz showed some of the new cool features in Inkscape 0.47. The workshop was especially helpful, with many useful tips and tricks.
  • David Dailey gave a session on SVG Filters, and I don't think we had time to more than skim the surface before time was up.

Winmodems are from Venus, Macs are from Mars

, ,

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here.Posting from a dialup connection today, which has a certain retro charm to it. Getting online wasn't as easy as I'd hoped though, read on for the full story.

First off, since I'm packing a macbook and the modem I have to use is a winmodem, a Conexant v90 modem to be exact, there of course weren't any mac drivers for it. So I decided to try using the Parallels VM to get online in Windows XP. No luck though, the modem was detected but it didn't work correctly. This prompted some googling and I thought that maybe I could install and run Windows on the macbook without emulation/virtualization. This turned out to be a major hassle since I didn't want to use the internal harddrive for this installation, but an external usb/firewire drive. A few unsuccessful attempts later, I gave up on that, and tried installing linux on the external drive instead.

Now I learned that Apple recently expired the BootCamp support for OS X Tiger (10.4), as a way of getting people to pay up for Leopard (10.5) I guess, so I had to find some other solution. Turns out there's a free mac boot menu toolkit called rEFIt, which worked just fine. rEFIt can see my external linux install, but alas is still unable to boot from it. Even when I put the linux /boot directory on a partition on the internal drive it didn't want to boot.

Then I remembered that I installed VirtualBox on a few machines recently, but I hadn't yet tried it on my macbook. So I did, and lo and behold the modem just started working!

VirtualBox is free and Open Source virtual machine that can run many different OSes, and it can be installed on OS X, Windows, Linux and Solaris (and I've now tried all except Solaris). Though it should be noted that not all parts of VirtualBox have been released as Open Source.

Probably only a matter of time before I install linux on the internal harddrive anyway, just need to sort out what to throw away first. Though it still sucks that I can't boot linux off the external firewire drive, which is something that obviously works fine for OS X installs.

In conclusion, thanks to Sun for releasing VirtualBox.

Hey little sister!

, ,

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here.

I'd like to send these virtual flowers to Camilla Lindberg (fp), as the single parlamentarist in the current government that stood up for the people and voted against massive privacy invasion, a.k.a the FRA law (link in Swedish only). Thank you Camilla.

I only wish that a few more parlamentarists would have been as wise...

Assorted svg news

,

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here.Here's a brief assortment of svg news from June. I don't think any of you missed that the final public version of Opera 9.5 was released, but if you did, nothing is stopping you from picking it up via bittorrent.

  • Jeff Schiller (Motorola) has been appointed co-Chair of the SVG Interest Group. The SVG IG is chartered to produce supplementary documents for the improvement, adoption, and evangelism of SVG; and to foster the engagement of the SVG community, both members and public, in W3C activities. Congratulations Jeff!
  • Opera Mini adds SVG support in their latest server upgrade. Great news for everyone that wants to efficiently use scalable web graphics anywhere.
  • Robert O'Callahan (Mozilla) writes:

    One of the problems with the standards-based Web is that it's hard to use SVG's features to enhance HTML content. [...] So I've been experimenting with better ways to apply SVG effects to HTML content. The first step is to make SVG's 'clip-path', 'mask' and 'filter' properties work when applied to HTML content.

    I'll just let his work speak for itself.

Timetravel

,


The above image should have been an svg image, since that makes it much smaller. However, it tended to freeze in Safari and Firefox, so I'll just link to it for now. Safari 3.1 has issues with the masking and sometimes the fill disappears for the tspans. Firefox 2 doesn't support masks, and has major issues with tspans. Firefox 3.0rc1 still has problems with tspans. Batik 1.7 renders it correctly, but doesn't run the animations (though possibly that's due to the FakeSMILe script). Opera 9.x should be able to play the animation just fine without FakeSMILe though, and you can click the image to stop the rotate animation should you get dizzy or something :smile:

Falling back, or falling down...

,

Microsoft tries to improve web standards support in IE8 apparently...well, except for...

Known issues we are not planning to change in IE8

  1. <OBJECT> is not parsed in a cross-browser compatible way (parsing stops at the OBJECT, whereas other browsers continue parsing all the fallback content and make it available. No support for this parsing behavior is planned for IE8; I'll take this opportunity to ask for real-world scenarios that can help me prioritize this feature.
  2. <OBJECT> elements cannot be 'reactivated' by dynamically correcting the attributes that caused the original fallback. Again, your feedback on the potential benefits/use-cases for this feature appreciated.

Source: IE8Blog

Being web standards compliant is not important enough?

Anyway, in practice it means that IE users won't be able to see most graphics on my blog. I usually provide some kind of fallback content for the tags I use. This makes it easier to do progressive enhancement, browsers that don't handle some type of content at all get some other content to munge on instead. Or anyway, that was the thought.

Modelling state machines

, , ,

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here.I was trying out some various ways of creating a simple state machine illustration in SVG this weekend. Basically I'm looking for a fairly highlevel description language that allows me to make changes easily while still providing automated layout and a good aestetic result that can be exported to SVG.

The following example (from Wikipedia) shows a simplified example of what I'd like to get as the end result.



It seemed like Graphviz was a sure fit to those criteria. This is what the above graph would look like if written in the DOT language that graphviz groks:

digraph simple_state_machine {
  node [shape = doublecircle]; S1;
  node [shape = circle] S2;
  node [shape = point] S0;
  S0 -> S1;
  S1 -> S2 [ label = "0" ];
  S1 -> S1 [ label = "1" ];
  S2 -> S1 [ label = "0" ];
  S2 -> S2 [ label = "1" ];
}
As you see this is fairly easy to write, and super easy to change. Exactly what I wanted in other words. Here's the output file:
[statemachine in SVG format]

Now the end result isn't as nice as the first example, which was "hand-drawn" in Inkscape judging by the comments in the file. Which brings me to my question: is there a program that can process the DOT input to produce something more similar to the first picture?

A new way to say Hooray!

, , , ...

Your browser doesn't support SVG or this feed was mangled to remove all 'object' elements. You may see the post in its original form at my.opera.com/macdev_ed. You may download a browser that supports SVG here. Since ACID3 seems to be the talk of the town lately, I figured I should at least say something about it. First of all, it's been fun "racing" against Webkit. To me the real fun isn't until the final result can be seen in stable final products though, simply passing ACID3 by itself doesn't mean full support for SVG or any other web standard AFAIK. I don't think that was the case for ACID1 or ACID2 either. Though I would consider passing these tests as a sign of commitment to web standards.

Maciej Stachowiak wrote up a nice piece on the struggles to pass the ACID3 test. We too had to fix a number of things to get the test 79, submitted by Cameron McCormack, to work, though the other SVG tests were of course easy for us since Opera has supported most of SVG 1.1 since Opera 9, and SMIL Animation since Opera 8.5.

The tests I wrote for ACID3 are probably easier to pass than the test Cameron submitted, and I agree with Maciej that they are indeed not as stringent. It would have been possible to create a far more advanced SMIL test. Time was in short supply though, that's the only excuse for the somewhat simplistic SMIL test. Really I would have liked to have a few visual tests for SVG there instead, but the rules of the contest weren't such that it allowed that unless it had verifiable results using scripting. There could have been one or more tests for SVG filters for example. The SVG 1.1 testsuite has a number of tests for all of this though, so I would recommend using that to accurately judge how a particular implementation fares with regards to SVG.

Eric Meyer writes:

...what’s really needed right now is exhaustive test suites for specifications– XHTML, CSS, DOM, SVG, you name it.


For SVG there are no less than two quite extensive public testsuites, one for SVG 1.1 and one for SVG 1.2 Tiny. These are still being actively worked on, with or without the help of ACID tests. The CDF WG also has a testsuite covering integration aspects of XHTML and SVG. Of course, exhaustive test suites take time to create, which is why the W3C is researching how to allow people to submit tests more easily. In general it seems the W3C is changing to become more open to the public, which IMHO is a good thing.

In closing, congrats to Webkit, and congrats to all the people involved at Opera for releasing a public Opera build that gets a pixel-perfect 100/100 pass rate on ACID3. Umm, what's that? An SVG-only Acid test? Naah, that's just preposterous. :wink: