I have a map (background image) on a Canvas, and "markers" in some cities (images duplicated from an Array of Objects and included in Canvas by drawImage ()).
Now I have to include href and title attributes in these markers. The data is also in the same Array of Objects.
The code so far:
var mkr = [
        {markerX: 222, markerY: 9, mkrTitle: "t1", mkrAction: "http://www.google.com"},
        {markerX: 210, markerY: 93, mkrTitle: "t2", mkrAction: "http://www.google.com"},
        {markerX: 122, markerY: 82, mkrTitle: "t3", mkrAction: "http://www.google.com"},
        {markerX: 103, markerY: 190, mkrTitle: "t4", mkrAction: "http://www.google.com"},
        {markerX: 96, markerY: 209, mkrTitle: "t5", mkrAction: "http://www.google.com"},
        {markerX: 122, markerY: 762, mkrTitle: "t6", mkrAction: "http://www.google.com"}
    ]
    var x = [];
    var y = [];
    var title = [];
    var action = [];
    Object.keys(mkr).forEach(function(key) {
      var valX = mkr[key]["markerX"];
      var valY = mkr[key]["markerY"];
      var valT = mkr[key]["mkrTitle"];
      var valA = mkr[key]["mkrAction"];
      x.push(valX);
      y.push(valY);
      title.push(valT);
      action.push(valA);
      var c = document.getElementById("mCanvas");
      //EDIT:
      var cLeft = c.offsetLeft;
      var cTop = c.offsetTop;
      //
      var ctx = c.getContext("2d");
      var img = document.getElementById("marker");
      ctx.drawImage(img, val, val2);
      //EDIT:
    c.addEventListener('click', function(event) {
                valX = event.pageX - cLeft,
                valY = event.pageY - cTop;
                console.log(x, y);
                mkr.forEach(function(img) {
                    if (valY > img.top && valY < img.top + img.height && valX > img.left && valX < img.left + img.width) {
                        ctx.fillText = mkr.valA;
                        window.location.href = mkr.valA;
                    }
                });
            }, false);
    });
And the HTML:
<img src="img/marker.png" id="marker" style="display: none;" />
<canvas id="mCanvas" width="295" height="809"></canvas> <!-- where are the markers -->
<canvas id="mapCanvas" width="600" height="600"></canvas> <!-- where is the background image -->
I can retrieve the data from the Array, but I can not include them as image attributes. My intent is to add click event on each image (markers) that I have over the map, using the data from the "mkrAction" item.
EDIT: As suggested, I have included the addEventListener code to try it, but simply it didn't work. Nothing happens, neither error on console... Where am I doing wrong?
 
    