|
||||||||
|
Chapter 10 Scripting the DOM Extract Touching Elements Since we can look at the computer mouse as the elongated arm of the user, mouse events play a big role in design for interactivity.
While focusing on events I chose a
simple SVG document. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG
1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd"> All of these rectangles are child elements of a common group and have the opacity style attribute defined with a value of 0.4. We want to exploit this for a highlighting effect by setting the opacity to 1.0, when the user moves the mouse pointer over it. HighlightingSo let’s start with the leftmost
blue rectangle, labelled highlight, adding appropriate event properties. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG
1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd"> Thanks to the Event object’s target property we get access to <rect> element that received the event. With this we can set the rectangles opacity to 1.0, when the mouse enters its area and reset it to 0.4 when the mouse pointer leaves.
( test this svg ) Now, if we want to give the other rectangles the same behaviour, we must add these onmouseover and onmouseout event properties to them also. 4. But there seems to be a simpler solution. Remembering the event flows towards the parent element – called event bubbling. We could add the onmouseover and onmouseout event properties to the <g> element instead and leave it at that.
<g onmouseover="Highlight(evt);"
onmouseout="Unhighlight(evt);"> Trying this out verifies that our considerations were correct. If we needed access to the <g> element inside the Highlight and Unhighlight functions, we would have used evt.currentTarget instead of evt.target. Changing ColorWe’ll come back to the ‘magical’
rectangle shortly but for now we want the third yellowgreen
rectangle to change its color to black whenever
the mouse is clicked on it.
<rect x="210" y="100" width="90"
height="40" fill="yellowgreen" opacity="0.4" 6. Next we must again implement the affiliated ECMAScript function. Now we intend to use a single function instead of one for onmousedown and another for onmouseup event. Here is the complete code. function ChangeColor(evt)
( test this svg ) Now when we move the mouse pointer onto the yellowgreen rectangle, it is highlighted due to the parents event handler. Then when we press the mouse button the colour changes to black. Releasing the button turns the colour back to yellowgreen. — still highlighted. On the other hand, if we keep the mouse button pressed, move the mouse pointer away from the rectangle and then release the mouse button, the color remains black, though unhighlighted now. Getting InvisibleFor our next trick we’ll use the 2nd rectangle that we skipped over a moment ago. The goal here is for the fourth rectangle to disappear when the mouse is over this ‘magical’ rectangle. On the side we also want to suppress the highlighting effect. 7. Adjust the code accordingly <rect
x="110" y="100" width="90" height="40"
fill="green" opacity="0.4" 8. The Magical function is similar to ChangeColor. function
Magical(evt)
( test this svg ) As I am illustrating here, we can use the evt.target element as a start to navigate through the DOM tree and affect other elements by that particular event too. We also tested the interruption of the event flow via a call to evt.stopPropagation(). Mutating ElementsThe next task is to change the fourth rectangle when clicking with the mouse on it, so that it becomes an ellipse. As we learned so far, we know that we cannot change an element’s type, we have to remove the element from the DOM tree and insert a new one instead. But I also told you, before using create and remove operations we should consider attribute manipulation in the first place. So here is one possible and simple solution. We add an <ellipse> element statically to our document and hide it with the display="none" attribute. 9. Enter the following code to the fourth rectangle <rect
x="310" y="100" width="90" height="40"
fill="orange" opacity="0.4" 10. Then we need only to move the display="none" attribute to the <rect> element and back to the <ellipse> element with each mouse click. function Mutate(evt)
( test this svg ) Since the <ellipse> element is also a child of the same parent group, highlighting works automatically here too. You might be urged to ask here what the difference between the display attribute and the visibility attribute is, as you cannot see the elements then either. Regarding the mouse event sensibility there is indeed a significant difference: · Elements with visibility="hidden" attribute set are invisible and can receive mouse events, if they additionally define the attribute pointer-events="all". · Elements with display="none" attribute set are invisible and cannot receive mouse events. And that last behaviour was exactly what we wanted here. Double ClicksSometimes we’ll want to differentiate between single and double mouse clicks. If you’ve read the MouseEvent’s properties, you’ll probably have noticed the detail property. This is a very commonly used property and its value varies with the event type. With the onclick event it stores the count of clicks the user performed. 11. Just to test this we’ll register the last red rectangle as an onclick event listener.
<rect x="410" y="100" width="90"
height="40" fill="red" opacity="0.4" 12. With the implementation of the ClickCounter function we need to display the number of current click counts. We’ll use the rectangles caption for this. function
ClickCounter(evt)
( test this svg ) It obviously works. The first click always writes 1. click under the rectangle. The second click will produce different results based on several factors. · If the time between the first and the second click is too long we get 1. click again. · If the time between the first and the second click is short enough, but we displaced the mouse pointer slightly we get 1. click too. · If the time between the first and the second click is short enough and we didn’t displace the mouse pointer we get 2. click. chapters: intro toc intro sample svg elements toc svg elements sample |
|
|||||||
|
| Rainbow Seeker | SVG PHP
Learn SVG: Contact Learn SVG
©2002-2008 Learn SVG. All Rights Reserved. Questions and comments to: learnsvg@gmail.com |
||||||||