JavaScript Add click event to canvas or make area map click event canvas javascript,click event canvas,click event on

I write this html code :

<div id="container">
        <canvas id="imageView" width="1181" height="1181">
            <p>Unfortunately, your browser is currently unsupported by our web 
                application.</p>
        </canvas>
        <script type="text/javascript">
            window.onload = function() {
                var c = document.getElementById('imageView');
                var cxt = c.getContext('2d');
                var img = new Image();
                img.src = "map.jpg";
                cxt.drawImage(img, 0, 0);
            };
        </script>
</div>

And write this javascript :

this.mousemove = function(ev) {
                        if (!tool.started) {
                            return;
                        }

                        var x = Math.min(ev._x, tool.x0),
                                y = Math.min(ev._y, tool.y0),
                                w = Math.abs(ev._x - tool.x0),
                                h = Math.abs(ev._y - tool.y0);
                        context.clearRect(0, 0, canvas.width, canvas.height);
                        if (!w || !h) {
                            return;
                        }
                        context.clearRect(x, y, w, h);
                        context.strokeRect(x, y, w, h);

this code is make a rectangle . I want change this rectangle to a area map , that when I click on the area do something , (for example open google.com) .

Answer:1

If I understand you correctly you want to invoke a function when you hit a pixel on the actual map - not just in the map area.

Method 1

You can check a map click in more than one way. You can simply check for the pixel value at the click point to check if it is inside the area you want it to be by comparing the map color value.

I provided an example below for this method.

Method 2

You can pre-define a polygon which traces the outline of the map area you want to check.

Then build a path (ctx.beginPath(); and ctx.lineTo(..); etc.) to allow the use of the method:

if (ctx.isPointInPath(x, y)) { ... };

This is a good method if you have small regions to check.

Method 3

Store a separate image of the map containing only a matte (sort of an alpha map), That is usually black (or transparent) for non-clickable areas, white for clickable areas.

This is useful if your map is complex color-wise and a simple pixel value check is not trivial.

And speaking of which: you can even provide different solid color values for different areas so that you can define red color = USA, blue = Argentina, etc. As these are not visible to the user the only thing that matters is that the color value can be recognized (for this reason don't save images for this use with an ICC color profile).

Then project the mouse position from the click onto the matte image (which is basically an off-screen canvas where the matte image is drawn into) and check for the color (white or other color).

Example for method 1

This is a simple example, but in any case there are a couple of things you need to know in advance:

  1. That the image is loaded from same server as the page or from a domain that allow cross-origin use. Or else you cannot grab a pixel from the map due to security reasons.
  2. You need to know what color or alpha value to check for. If the map is solid and everything is transparent you just need to check for alpha value above zero (as in this example), and if not just check the RGB value of the region you want to trigger an action with.

ONLINE DEMO HERE

HTML:

<canvas width=725 height=420 id="demo"></canvas>

JavaScript:

var ctx = demo.getContext('2d'),
    img = new Image();

/// we need to wait for the image to actually load:
img.onload = function() {

    /// image is loaded and we can raw it onto canvas
    ctx.drawImage(this, 0, 0);

    /// enable mouse click
    demo.onclick = function(e) {

        /// adjust mouse position to be relative to canvas
        var rect = demo.getBoundingClientRect(),
            x = e.clientX - rect.left,
            y = e.clientY - rect.top;

        /// grab a pixel
        var data = ctx.getImageData(x, y, 1, 1).data;

        /// check it's alpha value to see if we're in a map point
        /// this of course assumes the map has transparent areas.
        /// if not just check for the color values instead.
        if (data[3] > 0) alert('We hit map');
    }   
}

/// we need crossOrigin allowed image or we can't grab pixel later
img.crossOrigin = 'anonymous';
img.src = 'http://i.imgur.com/x8Ap3ij.png';

Just replace the alert with:

window.open('http://google.com/');

if you want it to open a new window/tab.

Answer:2

I guess I'm used to using something like ant or maven that produces a war file and I deploy that war file to my server. With grunt I don't see what I'm supposed to deploy. Am I supposed to grab the ...

I guess I'm used to using something like ant or maven that produces a war file and I deploy that war file to my server. With grunt I don't see what I'm supposed to deploy. Am I supposed to grab the ...

  1. when was building with asbestos banned
  2. max iterations when building with cycles

I have found a short script which finds a line with a certain phrase in it and replaces the whole line with a new line. However I can't work out how to include a variable where it says '#RemoveMe' in ...

I have found a short script which finds a line with a certain phrase in it and replaces the whole line with a new line. However I can't work out how to include a variable where it says '#RemoveMe' in ...

  1. javascript replace with regex
  2. javascript replace with variable
  3. javascript replace with space
  4. javascript replace with new line
  5. javascript replace with nothing
  6. javascript replace with regular expression
  7. javascript replace with '
  8. javascript replace with line break
  9. javascript replace with wildcard
  10. javascript replace with group
  11. javascript replace with callback
  12. javascript replace with unicode
  13. javascript replace with match
  14. javascript replace with carriage return
  15. javascript replace with backslash
  16. javascript replace with double quotes
  17. javascript replace with empty string
  18. javascript replace with multiple strings
  19. javascript replace with escape character
  20. javascript replace with capture groups

I have the following array ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod'] I want to get this array from it: ['....

I have the following array ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod'] I want to get this array from it: ['....

  1. merge elements array
  2. merge elements array php
  3. merge array elements javascript
  4. merge array elements python
  5. merge array elements ruby
  6. merge array elements c#
  7. merge array elements in perl
  8. merge elements in array js
  9. merge elements into array
  10. merge elements into array php
  11. mongodb merge array elements
  12. matlab merge array elements
  13. numpy merge array elements
  14. merge two array elements in java
  15. merge two array elements in c
  16. jq merge array elements
  17. java merge array elements
  18. powershell merge array elements
  19. jquery merge array elements
  20. merge two array elements

Some operations in javascript returns unexpected results. One is extremely strange: [] + 1 = "1" Can anybody explain why it works like that?

Some operations in javascript returns unexpected results. One is extremely strange: [] + 1 = "1" Can anybody explain why it works like that?

  1. javascript array plus array
  2. javascript array plus operator
  3. javascript array plus object
  4. javascript array plus number
  5. array javascript plus sign
  6. javascript empty array plus empty object