Selection and Navigation of Overlapping SVG Objects

Jimmy Oh (15 July 2013)

Abstract

SVG is a vector graphic image format that can be used with browsers to deploy dynamic, interactive graphics over the web. We present a method that enables generalised Selection and Navigation of Overlapping SVG Objects by toggling pointer-events CSS styles and the use of the mousewheel, adding another tool developers can use to grant users greater and more intuitive interaction with SVG images. We also present several live examples that demonstrate the method in practice, both on script generated SVG images and an externally generated SVG image.

Introduction

One of the advantages of vector graphics like SVGref is that renderers understand what it is drawing. Not only is the browser displaying a circle, it understands that the circle is there. This makes interactivity on the objects easy; if the circle should gain a coloured border on mouseover, the coder can simply assign such to the object itself. Compare this with raster graphics (such as HTML5 Canvas, JPEG, PNG and many others) where calculations are required to check if the mouse coordinates match the object; calculations that quickly become impractical to be exact when dealing with non-trivial objects.

The problem is that modern browsers will only register events on the top-most objects. Any overlapping objects hidden below are blocked by the objects above it. We present a method that makes use of the browsers' understanding of the objects (DOM) to select overlapping objects, along with a method to navigate between these selections intuitively with the mousewheel. As it uses the browsers' understanding it requires minimal additional computation and is as exact as the browser can be.

At present no other generalised solution to this problem could be found, only case-specific work-arounds. A method could be devised using a collision_detection_library, though this is likely to be computationally expensive.

Note that this method does not use collision detection and by itself cannot determine what objects are below other objects. It can only be used to determine what objects are below the current cursor position, which it will do with extreme precision.

Core Concepts

The core idea for Selection is to toggle pointer-events on the objects through classes. When pointer-events: none, the object becomes invisible with respect to the mouse, allowing us to then see below the object.

The core idea for Navigation is to bind a mousewheel event listener to the SVG, allowing users to navigate between potential selections in a manner similar to zooming, an action which many users will find familiar and intuitive.

Specifics of the implementation of these core ideas result in a variety of potential applications, from simple selection of all objects below the cursor at the current point (with knowledge of which ones are deeper in the nesting), to using the methods as a way to toggle transparency to literally see below top elements.

Technical Details

The method is implemented using D3.jsref1, ref2 and jQueryref with the jQuery Mouse Wheel Pluginref. It should be possible to cut out one of the libraries (or indeed, to cut both out and work in base JavaScript only) but using both makes implementation easier.

As the method relies on toggling pointer-events, caution should be applied if this style is otherwise adjusted. In particular, CSS cascading orderref should be consulted with regards to which style takes priority. If pointer-events must be used elsewhere, the CSS should be adjusted acordingly with !important rules to ensure correct operation (the example implementations do not do this as it is not necessary).

Refer to the example implementations for further details on the specifics of the implementation.

Summary

We have presented a method of Selecting and Navigating Overlapping SVG Objects by toggling pointer-events CSS styles and the use of the mousewheel, adding another tool developers can use to grant users greater and more intuitive interaction with SVG images.

Copyright Notice

All code is released Open Source under the MIT License (MIT), find all related files here

Copyright (c) 2013 Jimmy Oh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

References

  1. Aaron, B. (2013). jQuery Mouse Wheel Plugin URL https://github.com/brandonaaron/jquery-mousewheel
  2. Bostock, M., Ogievetsky, V., Heer, J., (2011). D3: Data-driven documents. IEEE Trans. Visualization & Comp. Graphics (Proc. InfoVis). URL http://vis.stanford.edu/papers/d3
  3. D3.js (2013). URL http://d3js.org/
  4. jQuery.js (2013). URL http://jquery.com/
  5. World Wide Web Consortium, (2011). Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification - Cascading order subsubsection URL http://www.w3.org/TR/CSS2/cascade.html#cascading-order
  6. World Wide Web Consortium, (2011). Scalable Vector Graphics (SVG) 1.1 (Second Edition) URL http://www.w3.org/TR/SVG/