views:

35

answers:

2

I have a series of shapes that I want the user to be able click anywhere on the shape to pick up as part of a drag and drop feature. All of these shapes are bounded by a rectangle.

For example:

<g id="shape1" fill="none" stroke="black">
    <rect x="0" y="0" width="100" height="100"/> 
    <circle cx="50" cy="50" r="50"/>
</g>
<g id="shape2" fill="none" stroke="black">
    <rect x="0" y="0" width="100" height="100"/> 
    <line x1="0" y1="0" x2="50" y2="100"/> 
    <line x1="100" y1="0" x2="50" y2="100"/>
</g>

I already have all of the drag and drop parts working, the problem is all of these shapes have to have fill="none" so you can see anything that may be underneath of them. This means that even though they are bounded by the rectangle, at the moment users have to physically click on one of the lines in order to pick it up instead of being able to click anywhere on the shape like I want.

My original idea was to use fill="white" and then set opacity="0" or some really low value but this applies to the stroke as well so this didn't work out.

Any ideas on how I can get this working?

+2  A: 

So it turns out I was a lot closer then I thought. Who knew they had a fill-opacity attribute...

<g id="shape1" fill="white" stroke="black" fill-opacity="0">
    <rect x="0" y="0" width="100" height="100"/> 
    <circle cx="50" cy="50" r="50"/>
</g>
<g id="shape2" fill="white" stroke="black" fill-opacity="0">
    <rect x="0" y="0" width="100" height="100"/> 
    <line x1="0" y1="0" x2="50" y2="100"/> 
    <line x1="100" y1="0" x2="50" y2="100"/>
</g>
Shaunwithanau
+2  A: 

Pointer-events is probably what you want here.

Here are some examples:

Erik Dahlström
Is this better then doing what I am doing currently? and wow! you don't know how much this helps me. This is exactly what I am going to need for hotspots as part of shapes. If this is the 'right' way to do this then I will accept this as the better answer. My way does feel extremely 'hackish'
Shaunwithanau
It's a rather common technique to use an invisible (visibility=hidden) rect to capture events. It's usually a good idea to set pointer-events=none on subtrees/elements that you don't want to capture mouseevents on, it can improve the performance since it allows the useragent to skip parts of the tree when handling mouseevents.
Erik Dahlström