English 中文(简体)
Adding reverse highlighting to the jQuery image map plugin?
原标题:

I m using the jQuery Map Hilighter plugin, but instead of fading a dark patch over each area, I would like to reverse this and instead make the surrounding area dark, keeping the hovered area the same colour.

I ve looked at the code for the plugin and it appears to use the canvas element (and an MS equivalent I guess) to do the highlighting. However, Firebug isn t very forthcoming with exactly where the shapes are defined - on the demo above it just shows this:

 <canvas style="border: 0pt none ; padding: 0pt; width: 960px; height: 593px; position: absolute; left: 0pt; top: 0pt; opacity: 1;" height="593" width="960"></canvas>

And I can t see anything that specifies an element shape to hover. This is the part of the JS that appears to be creating the shape:

add_shape_to = function(canvas, shape, coords, options) {
    var i, context = canvas.getContext( 2d );
    context.beginPath();
    if(shape ==  rect ) {
        context.rect(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]);
    } else if(shape ==  poly ) {
        context.moveTo(coords[0], coords[1]);
        for(i=2; i < coords.length; i+=2) {
            context.lineTo(coords[i], coords[i+1]);
        }
    } else if(shape ==  circ ) {
        context.arc(coords[0], coords[1], coords[2], 0, Math.PI * 2, false);
    }
    context.closePath();
    if(options.fill) {
        context.fillStyle = css3color(options.fillColor, options.fillOpacity);
        context.fill();
    }
    if(options.stroke) {
        context.strokeStyle = css3color(options.strokeColor, options.strokeOpacity);
        context.lineWidth = options.strokeWidth;
        context.stroke();
    }
    if(options.fade) {
        fader(canvas, 0);
    }
};
最佳回答

The shapes of the elements are defined as <area> elements inside a <map> element in the HTML code.

The simple answer to your question is to add something like this

  if (options.inverseFill) {
    context.rect(0,0, canvas.width,canvas.height);
  }

after the context.beginPath(); line, then an option in the defaults: inverseFill: true,, and then to make sure that all the areas in your image map are declared in the same clockwise direction.

What happens is that you define an extra subpath for the path that defines the dark patch, and when you fill, the overlapping area (ie, the original patch) will cancel out, as long as the winding numbers of the subpaths cancel, resulting in your desired inverted behaviour.

It gets rather more complicated if your area polygon definitions can rotate in both directions. For example if you do the above for the original MapHilight demo of the US map, only some of the states will behave correctly, since their shapes are defined in the correct clockwise direction - the rest stay dark, as their winding numbers are the wrong sign.

If you have full control over the image map definition, my advice would be to leave it at that and just ensure all areas go the same direction (ie, just reverse the coords list for each area that doesn t work).

If not, then at init time you d need to precalculate for each shape the winding number. This would most likely involve going over the list of points and summing the angle between every two consecutive segments - calculated using atan2. Then in the add_shape_to function traverse the 4 corners of the canvas in the correct direction.

Anyway, I hope that helps

Update:

sorry I didn t see your comment earlier. for the circular area, in the add_shape_to function, replace the } else if(shape == circ ) { part with

  } else if(shape ==  circ ) {
    context.closePath();
    context.moveTo(coords[0] + coords[2], coords[1]);
    context.arc(coords[0], coords[1], coords[2], 0, Math.PI * 2, true);
    context.closePath(); 
  }

It closes the subpath before and after, then moves to the correct place, to avoid nasty red line to the top corner, and then changes the anticlockwise parameter to true to match the outside rectangle. Modify as needed

问题回答

暂无回答




相关问题
anchor tags and image maps

how can i associate an anchor tag with image maps? Suppose, I hover over an anchor tag, it should select the mapped region on the image. Something like this http://myffi.biz/. This is in flash, but ...

Highlight a section of an image in JavaScript

I run a small webpage that allows users to click on various links using image maps. I d like to highlight the section that a user clicks on to give some feedback to the user (they may be clicking on ...

plot points for image map

I want to add automatic area highlighting to image maps on my webpage. I ve found the mapper.js library to be very useful in achieving this, however creating the x,y plots around a regional map is ...

jQuery not creating area tags

I m trying to use jQuery to dynamically create an image map, and I ran into a weird behavior: alert( $( <area /> ).size() ); // 1 alert( $( <area shape="circle" /> ...

How could I reduce the complexity of an image map?

I m using KImageMapEditor on Linux (Ubuntu) to create an image map. The shapes in the image are a little complex so I m using the freehand tool to draw them. However, this is really the same as the ...

Adding reverse highlighting to the jQuery image map plugin?

I m using the jQuery Map Hilighter plugin, but instead of fading a dark patch over each area, I would like to reverse this and instead make the surrounding area dark, keeping the hovered area the same ...

Div, Background Image, and Image Map

On a web page I m creating, I have a div with a background image. I overlay some text over the image that I ll be changing frequently, so that s why I m using a background image and real text instead ...

How to display image on the defined <Area> coordinates

We can use image maps to create hyperlinks... but is it also possible to use an area segment of an image map to fill it up with another image. If yes then how we do it? there is no img tag in tag!!...

热门标签