JavaScript How do I capture the onclick event called in HTML? capture onclick event javascript,capture onclick event jquery,onclick capture

So, I have an <img> tag that has an onclick attribute. The onclick calls a function called analyze(this), with this being the image.

The analyze function does some things to the image that aren't entirely relevant, except for the fact that it draws it onto the <canvas> element (using the drawImage function).

But now, I want to also pick the color I just clicked on in the image. I am currently using the method answered here (the answer with 70+ votes, not the chosen one): How do I get the coordinates of a mouse click on a canvas element?

But, I think I might be doing this wrong. I have the image drawn and my functions called (and those all work), but the color picking part isn't being called. I think that this is because I didn't actually capture the event. This is generally how my code looks:

<img onclick="javascript:analyze(this);" />

function analyze(img_elem) {
    // This is getting the canvas from the page and the image in it
    var canvaselement = document.getElementById('canvas').getContext('2d'),
        img = new Image();

    img.onload = function () {
        canvaselement.drawImage(img, 0, 0, 250, 250);

        ...

        canvaselement.onClick = function () {
            var coords = canvaselement.relMouseCoords(event);
            pick(img, canvaselement, coords); // pass in coordinates            
        }

    }

    img.src = img_elem.src;
}

function relMouseCoords(event) {
    var totalOffsetX = 0;
    var totalOffsetY = 0;
    var canvasX = 0;
    var canvasY = 0;
    var currentElement = this;

    do {
        totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
        totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
    }
    while (currentElement = currentElement.offsetParent)

    canvasX = event.pageX - totalOffsetX;
    canvasY = event.pageY - totalOffsetY;

    return {
        x: canvasX,
        y: canvasY
    }
}

function pick(img, canvaselement, coords) {
    var pickedColor = "";
    canvaselement.drawImage(img, 0, 0, 250, 250);

    xx = coords.x;
    yy = coords.y;
    var imgData = canvas.getImageData(xx, yy, 1, 1).data;

    pickedColor = rgbToHex(imgData);

    //alert(pickedColor);
    return pickedColor;
}

So, the code never gets to the pick function. I have a feeling that it's because I didn't actually capture the onclick event. I'm also not even sure if this is the right way to get the coordinates on the canvas, I'm just sort of hoping that I even get to that part of the debugging process at this point.

Thanks for your help!

Answer:1

The problem is probably that you're assigning canvaselement to the results of getContext('2d') and not to the element itself, which you will need for the click event binding. Create two variables, one for the DOM element itself and one for the context, something like:

var canvaselement = document.getElementById('canvas'),
    canvaselementctx = canvaselement.getContext('2d');

...
canvaselement.onClick = function() {
    var coords = canvaselementctx.relMouseCoords(event);
    ...
}
Answer:2

how to change the color of a line in a HTML page(text in a pre>) Containing A specifique word for example i want to change the color a any line containig "ERROR" and "Erreur" 2014/05/22 02:27:02 - X....

how to change the color of a line in a HTML page(text in a pre>) Containing A specifique word for example i want to change the color a any line containig "ERROR" and "Erreur" 2014/05/22 02:27:02 - X....

  1. change color line plot matlab
  2. change color line matplotlib
  3. change color line edittext android
  4. change color line autocad
  5. change color in photoshop
  6. change color line chart ggplot
  7. change color line chart excel
  8. change color line procreate
  9. change color line in photoshop
  10. change color line illustrator
  11. change color line between columns word
  12. change color line graph illustrator
  13. change color line ggplot
  14. change color line sketchup
  15. change color line textinputlayout
  16. change color line ggplot2
  17. change color line gnuplot
  18. change color line plot python
  19. change color line highcharts
  20. change color line graph matlab

I have the following User object: { "_id" : ObjectId("someId"), "name" : "Bob", "password" : "fakePassword", "follower" : [...], "following" : [..] } I need to paginate over the follower ...

I have the following User object: { "_id" : ObjectId("someId"), "name" : "Bob", "password" : "fakePassword", "follower" : [...], "following" : [..] } I need to paginate over the follower ...

  1. mongodb limit fields returned
  2. mongodb limit fields
  3. mongodb find limit fields
  4. mongodb lookup limit fields
  5. mongodb aggregate limit fields
  6. mongodb query limit fields
  7. spring data mongodb limit fields

I am working through a project for a company I plan on working for and I was given a code challenge to create a choropleth map of Kenya. I have been able to generate the map using GeoJSON and TopoJSON ...

I am working through a project for a company I plan on working for and I was given a code challenge to create a choropleth map of Kenya. I have been able to generate the map using GeoJSON and TopoJSON ...

I am trying to show a list grouped by date but the list isn't sorted and I don't know when the date change. It's like I have this Json : list = {name: first, date: 2014-05-21}, { {name: second, date:...

I am trying to show a list grouped by date but the list isn't sorted and I don't know when the date change. It's like I have this Json : list = {name: first, date: 2014-05-21}, { {name: second, date:...

  1. array with arrays javascript
  2. array with arrays
  3. array with arrays python
  4. array with arrays php
  5. array with arrays c#
  6. array with arrays numpy
  7. array with arrays merge
  8. array with arrays in java
  9. array of arrays matlab
  10. array of arrays js
  11. array of arrays typescript
  12. array of arrays swift
  13. array of arrays vba
  14. array of arrays arduino
  15. array of arrays json
  16. array of arrays perl
  17. array of arrays bash
  18. array of arrays ruby
  19. array of arrays golang
  20. array of arrays powershell