16 Interactivity


Contents


 

16.1 Introduction

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:

16.2 User interface events

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.

16.3 Pointer events

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 graphical content is under the pointer at the time of the event. (See property 'pointer-events' for a description of how to determine whether an element's relevant graphical content is under the pointer, and thus in which circumstances that graphic element can be the target element for a pointer event.) When an element is not displayed (i.e., when the 'display' property on that element or one of its ancestors has a value of none), that element cannot be the target of 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, bubbling occurs up to all direct ancestors of the target element. 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.

16.4 Processing order for user interface events

The processing order for user interface events is as follows:


16.5 The 'pointer-events' property

In different circumstances, authors may want to control under what circumstances particular graphic elements can become the target of pointer events. For example, the author might want a given element to receive pointer events only when the pointer is over the stroked perimeter of a given shape. In other cases, the author might want a given element to ignore pointer events under all circumstances so that graphical elements underneath the given element will become the target of pointer events.

For example, suppose a circle with a 'stroke' of red (i.e., the outline is solid red) and a 'fill' of none (i.e., the interior is not painted) is rendered directly on top of a rectangle with a 'fill' of blue. The author might want the circle to to be the target of pointer events only when the pointer is over the perimeter of the circle. When the pointer is over the interior of the circle, the author might want the underlying rectangle to be the target element of pointer events.

The 'pointer-events' property specifies under what circumstances a given graphics element can be the target element for a pointer event. It affects the circumstances under which the following are processed:

'pointer-events'
Value:   visiblePainted | visibleFill | visibleStroke | visibleFillStroke | visible |
painted | fill | stroke | fillstroke | all | none | inherit
Initial:   visiblePainted
Applies to:   container elements and graphics elements
Inherited:   yes
Percentages:   N/A
Media:   visual
Animatable:   yes
visiblePainted
The given element can be the target element for pointer events when the 'visibility' property is set to visible and when the pointer is over a "painted" area. The pointer is over a painted area if it is over the interior (i.e., fill) of the element and the 'fill' property is set to a value other than 'none' or it is over the perimeter (i.e., stroke) of the element and the 'stroke' property is set to a value other than 'none'.
visibleFill
The given element can be the target element for pointer events when the 'visibility' property is set to visible and when the pointer is over the interior (i.e., fill) of the element. The value of the 'fill' property does not effect event processing.
visibleStroke
The given element can be the target element for pointer events when the 'visibility' property is set to visible and when the pointer is over the perimeter (i.e., stroke) of the element. The value of the 'stroke' property does not effect event processing.
visible
The given element can be the target element for pointer events when the 'visibility' property is set to visible . and the pointer is over either the interior (i.e., fill) or the perimeter (i.e., stroke) of the element. The values of the 'fill' and 'stroke' do not effect event processing.
painted
The given element can be the target element for pointer events when the pointer is over a "painted" area. The pointer is over a painted area if it is over the interior (i.e., fill) of the element and the 'fill' property is set to a value other than 'none' or it is over the perimeter (i.e., stroke) of the element and the 'stroke' property is set to a value other than 'none'. The value of the 'visibility' property does not effect event processing.
fill
The given element can be the target element for pointer events when the pointer is over the interior (i.e., fill) of the element. The values of the 'fill' and 'visibility' properties do not effect event processing.
stroke
The given element can be the target element for pointer events when the pointer is over the perimeter (i.e., stroke) of the element. The values of the 'stroke' and 'visibility' properties do not effect event processing.
all
The given element can be the target element for pointer events whenever the pointer is over either the interior (i.e., fill) or the perimeter (i.e., stroke) of the element. The values of the 'fill', 'stroke' and 'visibility' properties do not effect event processing.
none
The given element does not receive pointer events.

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.

16.6 Magnification, zooming and panning

Magnification represents a 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).

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 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.

SVG user agents that operate in interaction-capable user environments are required to support the ability to magnify, zoom and pan.

The outermost 'svg' element in an SVG document fragment has attribute zoomAndPan, which takes the possible values of disable, magnify and zoom, with the default being magnify.

If disable, the user agent shall disable any zooming, magnification and panning controls and not allow the user to magnify, zoom or pan on the given document fragment.

If magnify, in environments that support user interactivity, the user agent shall provide controls to allow the user to perform a "magnify" operation on the document fragment.

If zoom, in environments that support user interactivity, the user agent shall provide controls to allow the user to perform a "zoom" operation on the document fragment.

If a zoomAndPan attribute is assigned to an inner 'svg' element, the zoomAndPan setting on the inner 'svg' element will have no effect on the SVG user agent.

Animatable: no.

16.7 Cursors

16.7.1 Introduction to cursors

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.

16.7.2 The 'cursor' property

'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:   container elements and graphics 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:

auto
The UA determines the cursor to display based on the current context.
crosshair
A simple crosshair (e.g., short line segments resembling a "+" sign).
default
The platform-dependent default cursor. Often rendered as an arrow.
pointer
The cursor is a pointer that indicates a link.
move
Indicates something is to be moved.
e-resize, ne-resize, nw-resize, n-resize, se-resize, sw-resize, s-resize, w-resize
Indicate that some edge is to be moved. For example, the 'se-resize' cursor is used when the movement starts from the south-east corner of the box.
text
Indicates text that can be selected. Often rendered as an I-bar.
wait
Indicates that the program is busy. Often rendered as a watch or hourglass.
help
Help is available for the object under the cursor. Often rendered as a question mark or a balloon.
<uri>
The user agent retrieves the cursor from the resource designated by the URI. If the user agent cannot handle the first cursor of a list of cursors, it shall attempt to handle the second, etc. If the user agent cannot handle any user-defined cursor, it must use the generic cursor at the end of the list.
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.

16.7.3 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 (%descTitleMetadata;) >
<!ATTLIST cursor
  %stdAttrs;
  %xlinkRefAttrs;
  xlink:href %URI; #REQUIRED
  %testAttrs;
  externalResourcesRequired %Boolean; #IMPLIED
  x %Coordinate; #IMPLIED
  y %Coordinate; #IMPLIED >

Attribute definitions:

x = "<coordinate>"
The x-coordinate of the position in the cursor's coordinate system which represents the precise position that is being pointed to.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
y = "<coordinate>"
The y-coordinate of the position in the cursor's coordinate system which represents the precise position that is being pointed to.
If the attribute is not specified, the effect is as if a value of "0" were specified.
Animatable: yes.
xlink:href = "<uri>"
A URI reference to the file or element which provides the image of the cursor.
Animatable: yes.

Attributes defined elsewhere:
%stdAttrs;, %testAttrs;, %xlinkRefAttrs;, externalResourcesRequired.

SVG user agents are required to support PNG format images as targets of the xlink:href property.

16.8 DOM interfaces

The following interfaces are defined below: SVGCursorElement.


Interface SVGCursorElement

The SVGCursorElement interface corresponds to the 'cursor' element.


IDL Definition
interface SVGCursorElement : 
                SVGElement,
                SVGURIReference,
                SVGTests,
                SVGExternalResourcesRequired { 

           attribute SVGAnimatedLength x;
                       // raises DOMException on setting
           attribute SVGAnimatedLength y;
                       // raises DOMException on setting
};

Attributes
SVGAnimatedLength x
Corresponds to attribute x on the given 'cursor' element.
Exceptions on setting
DOMException
NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
SVGAnimatedLength y
Corresponds to attribute y on the given 'cursor' element.
Exceptions on setting
DOMException
NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.