SVG content can be interactive (i.e., responsive to user-initiated events) by utilizing the following features in the SVG language:
This chapter describes:
Related information can be found in other chapters:
On user agents which support interactivity, it is common for authors to define SVG document such that they are responsive to user interface events. Among the set of possible user events are pointer events, keyboard events, and document events.
In response to user interface (UI) events, the author might start an animation, perform a hyperlink to another web page, highlight part of the document (e.g., change the color of the graphics elements which are under the pointer), initiate a "roll-over" (e,g., cause some previously hidden graphics elements to appear near the pointer) or launch a script which communicates with a remote database.
For all UI event-related features defined as part of the SVG language via event attributes or animation, the event model corresponds to the event bubbling model described in DOM2 [DOM2-EVBUBBLE]. The event capture model from DOM2 [DOM2-EVCAPTURE] can only be established from DOM method calls.
User interface events that occur because of user actions performed on a pointer device are called pointer events.
Many systems support pointer devices such as a mouse or trackball. On systems which use a mouse, pointer events consist of actions such as mouse movements and mouse clicks. On systems with a different pointer device, the pointing device often emulates the behavior of the mouse by providing a mechanism for equivalent user actions, such as a button to press which is equivalent to a mouse click.
For each pointer event, the SVG user agent determines the target element of a given pointer event. The target element is the topmost graphics element whose relevant graphics content is under the pointer at the time of the event. (See property 'pointer-events' for a description of how to determine the situations in which a graphic element receives pointer events.)
The event is either initially dispatched to the target element, to one of the target element's ancestors, or not dispatched, depending on the following:
When event bubbling [DOM2-EVBUBBLE] is active, descendant elements receive events before their ancestors. Thus, if a 'path' element is a child of a 'g' element and they both have event listeners for click events, then the event will be dispatched to the 'path' element before the 'g' element.
When event capturing [DOM2-EVCAPTURE] is active, ancestor elements receive events before their descendants.
After an event is initially dispatched to a particular element, unless an appropriate action has been taken to prevent further processing (e.g., by invoking the preventCapture() or preventBubble() DOM method call), the event will be passed to the appropriate event handlers (if any) for that element's ancestors (in the case of event bubbling) or that element's descendants (in the case of event capture) for further processing.
The processing order for user interface events is as follows:
In different circumstances, authors may or may not want events to be triggered when the pointer is over the unfilled interior of a graphics element or the pointer is over an invisible graphics element. The 'pointer-events' property specifies under what circumstances a given graphics element receive pointer events.
Value: | visiblePainted | visibleFill | visibleStroke | visibleFillStroke | visible | painted | fill | stroke | fillstroke | all | none | inherit |
Initial: | visiblePainted |
Applies to: | all elements |
Inherited: | yes |
Percentages: | N/A |
Media: | visual |
Animatable: | yes |
For text elements, hit detection is performed on a character cell basis. The values visiblePainted, visibleFill, visibleStroke and visibleFillStroke are all defined to be equivalent to the value visible, and the values painted, fill, stroke and fillStroke are all defined to be equivalent to the value all.
For raster elements, hit detection can be defined to be dependent on whether the pixel under the pointer is fully transparent. For any of the values visiblePainted, visibleFill, visibleStroke and visibleFillStroke, the raster element receives the event if the 'visibility' property is set to visible and the pixel under the pointer is not fully transparent. For a value of visible, the raster element receives the event if the 'visibility' property is set to visible even if the pixel under the pointer is fully transparent. For any of the values painted, fill, stroke and fillStroke, the raster element receives the event if the the pixel under the pointer is not fully transparent, no matter what the value is for the 'visibility' property. For a value of all, the raster element receives the event even if the pixel under the pointer is fully transparent, no matter what the value is for the 'visibility' property.
Zooming represents a (potentially) non-uniform scale transformation on an SVG document fragment in response to a user interface action. All elements which are specified in user coordinates will scale uniformly, but elements which use CSS unit identifiers to define coordinates or lengths may be transformed differently. A zoom operation has the effect of a supplemental scale and translate transformation inserted into the transformation hierarchy between the outermost 'svg' element and its children, as if an extra 'g' element enclosed all of the children and that 'g' element specified a transformation to achieve the desired zooming effect.
Panning represents a translation (i.e., a shift) transformation on an SVG document fragment in response to a user interface action.
Magnification represents complete, uniform transformation on an an SVG document fragment, where the magnify operation scales all graphical elements by the same amount. A magnify operation has the effect of a supplemental scale and translate transformation placed at the outermost level on the SVG document fragment (i.e., outside the outermost 'svg' element)..
Some ability to zoom and pan SVG document fragments are required for SVG user agents that operate in interaction-capable user environments. Document-level magnification capabilities are recommended for SVG user agents to enable accessibilty to those who are partially visually impaired.
The outermost 'svg' element in an SVG document fragment has attribute enableZoomAndPanControls, which takes the possible values of trueand false, with the default being true. If true, in environments that support user interactivity, the user agent shall provide user interface controls to allow the user to zoom in, zoom out and pan around the given document fragment. If false, the user agent shall disable these controls and not allow the user to zoom and pan on the given document fragment. If a enableZoomAndPanControls attribute is assigned to an inner 'svg' element, the enableZoomAndPanControls setting on the inner 'svg' element will have no effect on the SVG user agent.
Animatable: no.
Some interactive display environments provide the ability to modify the appearance of the pointer, which is also known as the cursor. Three types of cursors are available:
The 'cursor' property is used to specify which cursor to use. The 'cursor' property can be used to reference standard built-in cursors by specifying a keyword such as crosshair or a custom cursor. Custom cursors are references via a <uri> and can point to either an external resource such as a platform-specific cursor file or to a 'cursor' element, which can be used to define a platform-independent cursor.
Value: | [ [<uri> ,]* [ auto | crosshair | default | pointer | move| e-resize | ne-resize | nw-resize | n-resize | se-resize | sw-resize | s-resize | w-resize| text | wait | help ] ] | inherit |
Initial: | auto |
Applies to: | all elements |
Inherited: | yes |
Percentages: | N/A |
Media: | visual, interactive |
Animatable: | yes |
This property specifies the type of cursor to be displayed for the pointing device. Values have the following meanings:
P { cursor : url("mything.cur"), url("second.csr"), text; }
The 'cursor' property for SVG is identical to the 'cursor' property defined in the "Cascading Style Sheets (CSS) level 2" specification [CSS2], with the exception that SVG user agents must support cursors defined by the 'cursor' element.
The 'cursor' element can be used to define a platform-independent custom cursor. A recommended approach for defining a platform-independent custom cursor is to create a PNG [PNG01] image and define a 'cursor' element that references the PNG image and identifies the exact position within the image which is the pointer position (i.e., the hot spot).
<!ELEMENT cursor (%descTitle;) > <!ATTLIST cursor id ID #IMPLIED system-required NMTOKEN #IMPLIED system-language CDATA #IMPLIED x CDATA "0" y CDATA "0" %xlinkRefAttrs; xlink:href CDATA #REQUIRED > |
Attribute definitions:
SVG user agents are required to support PNG format images
as targets of the xlink:href property.
The SVGCursorElement interface corresponds to the 'cursor' element.
interface SVGCursorElement : SVGElement { attribute SVGLength x; attribute SVGLength y; attribute DOMString role; attribute DOMString title; attribute DOMString show; attribute DOMString actuate; attribute DOMString href; }; |
The SVGViewElement interface corresponds to the 'view' element.
interface SVGViewElement : SVGElement { // Object-oriented access SVGRect viewBox; SVGPreserveAspectRatio preserveAspectRatio; boolean enableZoomAndPanControls; SVGElement viewTarget; }; |