6

I have an image with more than 100 geometrical shapes with different size and dimensions. I used image mapping over it and assign ID to each <area> like <area id="1">. I stored records in MySQL db about each shape like:

--------------------
box_id | color_code
--------------------
   1      #AEEE11
   2      #AEEE01
   3      #DEEF11
   4      #0EE001
--------------------

Now I want to set background-color for each area with respect to their IDs.

Here I paste HTML code for some area as whole page will increase my post:

<img src="images/map.gif" border="0" usemap="#Msj_Map" alt="map" class="map" />
<map name="Msj_Map" id="Msj_Map">   
    <area id="8" shape="poly" coords="436,141,486,141,486,207,436,206" />
    <area id="1" shape="poly" coords="163,148,163,170,159,170" />
    <area id="2" shape="poly" coords="163,207,153,207,159,173,163,173" />
    <area id="189" shape="poly" coords="198,281,199,307,161,307,161,282" />
    <area id="190" shape="poly" coords="198,309,199,333,161,334,161,309" />
    <area id="165" shape="poly" coords="540,230,570,230,577,236,577,271,540,271" />
    <area id="40" shape="poly" coords="384,1156,419,1156,419,1180,383,1180" />
    <area id="39" shape="poly" coords="422,1156,458,1156,458,1180,422,1181" />
    <area id="54" shape="poly" coords="321,1109,353,1109,359,1116,360,1159,321,1159" />
    <area id="29" shape="poly" coords="356,1235,387,1235,387,1274,356,1274" />
    <area id="22" shape="poly" coords="390,1277,457,1277,457,1311,453,1315,390,1315" />
    <area id="23" shape="poly" coords="321,1277,387,1277,387,1315,321,1315" />
    <area id="24" shape="poly" coords="319,1277,319,1316,252,1316,252,1277" />
</map>

I also tried:

<area id="1" shape="poly" coords="604,140,657,140,677,160,677,234,605,234" style="background-color:#00FFEE;" />

but not work... :(

6
  • Please post the HTML markup and the PHP which generates the markup Commented Mar 22, 2012 at 20:31
  • For HTML to validate, an id begins with a letter, a-z or A-Z. Commented Mar 22, 2012 at 20:35
  • it doesn't matter if you are using ids for processing db tables or to increase or decrease rows based on specific id then you can use numeric value as element id. Commented Mar 22, 2012 at 20:39
  • 2
    Also, as far as I know, area tags have not visual styles because the browser doesn't show areas. Commented Mar 22, 2012 at 20:39
  • 1
    TRUE: you cannot style them. There is a jQuery plug-in that attempts to solve this: davidlynch.org/projects/maphilight/docs Commented Mar 22, 2012 at 20:42

4 Answers 4

8

I think you should use a jquery imagemap plugin ... this is my favorite

Link : http://archive.plugins.jquery.com/project/maphilight

Demo : http://davidlynch.org/projects/maphilight/docs/demo_usa.html

This topic has also been discussed in detail here .....

Using JQuery hover with HTML image map

I don't think there is need for duplication

============= Update on your comments ===================

Go to https://github.com/kemayo/maphilight/blob/master/jquery.maphilight.js

Can you see the following that maphilight accept fillColor: '000000' ;

You need to change fillOpacity to 1.0 to remove opacity

All you need to do is make with work without mouse over by editing the code below and replace with yours

        $(map).trigger('alwaysOn.maphilight').find('area[coords]')
            .bind('mouseover.maphilight', mouseover)
            .bind('mouseout.maphilight', function(e) { clear_canvas(canvas); });;

You have a working Background Color version ...

Thanks :)

2
  • i think you don't understood my question. please read my question again & again.. I want to set background-color for <area> not highlight <area>, i want to apply solid background color.. Commented Mar 22, 2012 at 20:56
  • The reason its not working because its am image not html and you can just set the background like that ..I asked you should use a plugin .. it can easily plot the coordinate and replace it with your color just like the way highlight is working now
    – Baba
    Commented Mar 22, 2012 at 21:12
3

The area element just makes part of an image clickable. It does not affect rendering, and setting background properties on it probably has no effect.

The background would matter if the image contains transparent areas. In such a case, you could overlay (with CSS positioning) the image with another image of the same dimensions and containing the desired colors; this image would of course have a lower z-index value. But it would be simpler to put the backgrounds into the image directly (unless you wish to use different backgrounds in different situations).

1
  • 1
    Can you please demonstrate it with any example?
    – Tanveer
    Commented Aug 28, 2012 at 11:17
3

As Baba already said, you can trick area background highlighting with maphilight script.

Checkout an example here:

davidlynch.org/projects/maphilight/docs/demo_features.html

Background will highlight somehow like this:

<script>
jQuery(document).ready(function() {
     var data = $('#s1').mouseout().data('maphilight') || {};
     data.alwaysOn = !data.alwaysOn;
     $('#s1').data('maphilight', data).trigger('alwaysOn.maphilight');
});
</script>

<img src="aaaa.jpg" usemap="#myMap" width="927" height="1106" />
    <map name="myMap" id="myMap">
        <area shape="rect" coords="219,800,314,819" id="s1" class="{fill:true,fillColor:'cd3333',fillOpacity:0.4,stroke:true,strokeColor:'003333',strokeOpacity:0.8,strokeWidth:1}" />
    </map>
1

Since you have a bunch of areas with id's corresponding to your tables, the only thing you really need to do is to create the CSS markup for each of those IDs. What you want to do is loop through your mysql table and "echo" the CSS markup somewhere between your head tags.

1) Establish MySQL connection

2) Create your select statement and initiate the while loop

3) echo your css code.

<html>
<head>
<style type="text/css">
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$result="SELECT `box_id`,`color_code` FROM `tablename`";
while ($row=mysql_fetch_assoc($result)) {
    echo "#{$row['box_id']}\{background-color: {$row['color_code']}\}";
} mysql_close();
?>
</style>
</head>
<body></body>
</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.