JavaScript How do I get the image data on specific objects?

In a canvas I created a 2d context. In that context... with a function... I'm able to create some 'circle objects'. Now, what I want, is to get the ImageData of a single circle object instead of the image data of the whole context.

In the code below, you can see my wish commented out.

var c = document.getElementById('canvas');
var ctx = c.getContext('2d');

var circle = function (X,Y) {
    var that = this;
    that.X = X;
    that.Y = Y;
    that.clicked = function(e) {
        //
        //
        //!!!!!!!!!!!!!!
        // Code below works fine, on context level
        imgData = ctx.getImageData(e.pageX, e.pageY, 1, 1);
        //
        // Code below is at the level of the circle, that's what I want, but isn't working
        imgData = that.getImageData(e.pageX, e.pageY, 1, 1);
        //!!!!!!!!!!!!!!
        //
        //
        alert(imgData.data[3]);
    }
    that.draw = function () {
        ctx.save();
        ctx.translate(that.X, that.Y);
        ctx.fillStyle = '#33cc33';
        ctx.beginPath();
        ctx.arc(0, 0, 50, 0, 2 * Math.PI);
        ctx.fill();
        ctx.stroke();
        ctx.restore();
    }
}
var circles = new Array();
circles.push(new circle(50,50));
document.addEventListener('click',function() {
    circles.forEach(function(circ,index){
        circ.clicked();
    });
})

So, how do I get the image data on specific objects?

edit: I understand that I need to draw the circle first, I do that later in my code, but what if I've got a background rect in the context, when I click next to the circle, it will get the imageData of the background rect, when I want to return the 0 value of the alpha rgba.

Answer:1

Remember that Canvas is a bitmap graphics tool. Anything you draw into a single context becomes part and parcel of the same object. You can't get separate image data for each "object" you used to draw on that canvas... it's painted ... flattened ... into those pixel positions for that bitmap as soon as you hit draw().

The only way you could do something like what you are looking for would be to create separate canvas contexts that you overlay on top of each other. This would be better handled by utilizing a library such as KineticJS (http://www.html5canvastutorials.com/kineticjs/html5-canvas-events-tutorials-introduction-with-kineticjs/). The only other option would be to use an object oriented drawing tool such as SVG, (through Raphael.js, for example: http://raphaeljs.com) which does preserve separate objects in the the graphics space.

For reference about getImageData, see http://www.html5canvastutorials.com/advanced/html5-canvas-get-image-data-tutorial/

Answer:2

You can use trigonometry instead of trying to locate your colors with getImageData.

For example, if you have a circle defined like this:

var centerX=150;
var centerY=150;
var radius=20;
var circleColor="red";

Then you can test if any x,y is inside that circle like this:

// returns true if x,y is inside the red circle
isXYinCircle(140,140,centerX,centerY,radius);

function isXYinCircle(x,y,cx,cy,r){
    var dx=x-cx;
    var dy=y-cy;
    return(dx*dx+dy*dy<=r*r);
}

If the x,y is inside that red circle then you know the color at x,y is "red"

If you have multiple overlapping circles you can test each circle in increasing z-index order. The last circle that reports x,y inside will be the color at x,y.

Answer:3

It is because that is not a CanvasGraphicsContext. Try:

that.draw();
imgData = ctx.getImageData(e.pageX, e.pageY, 1, 1);
Answer:4

Currently I am trying to pull videos from a youtube channel with youtube api and then using javascript to search and categorize it. I can get a search to work on an html table, but can't get it to ...

Currently I am trying to pull videos from a youtube channel with youtube api and then using javascript to search and categorize it. I can get a search to work on an html table, but can't get it to ...

  1. retrieving videos from icloud
  2. retrieving videos from iphone
  3. retrieving videos from facebook
  4. retrieving videos from snapchat
  5. retrieving videos on ipad
  6. retrieving deleted videos from android
  7. retrieving deleted videos from iphone
  8. retrieving deleted videos from samsung
  9. retrieving deleted videos from ipad
  10. retrieving deleted videos from samsung s7
  11. retrieving deleted videos from phone
  12. retrieving deleted videos from gopro
  13. retrieving deleted videos from sony handycam
  14. retrieving deleted videos from laptop
  15. retrieving deleted videos from samsung tablet
  16. retrieving deleted videos from galaxy note 4
  17. retrieving deleted videos from computer
  18. retrieving deleted videos from canon

I am using jQuery's getScript() method to load some third part js library, I am wondering whether there's a default time out value for this method. I don't really believe getScript will keep waiting ...

I am using jQuery's getScript() method to load some third part js library, I am wondering whether there's a default time out value for this method. I don't really believe getScript will keep waiting ...

I have created a simple extension for use on a clients pc to add a save image option to the context menu. Googles Chrome extension documentation, plus examples quoted on the web, leads me to believe I ...

I have created a simple extension for use on a clients pc to add a save image option to the context menu. Googles Chrome extension documentation, plus examples quoted on the web, leads me to believe I ...

I've looked at a lot of similar questions about this kind of problem but they haven't solved my problem... This is the string that I've to match: "|6[1]|" where the "6" is a variable that I've to put ...

I've looked at a lot of similar questions about this kind of problem but they haven't solved my problem... This is the string that I've to match: "|6[1]|" where the "6" is a variable that I've to put ...

  1. variable inside javascript string
  2. variable inside javascript
  3. variable inside regex javascript
  4. php variable inside javascript
  5. variable inside function javascript
  6. variable inside class javascript
  7. variable inside object javascript
  8. variable inside array javascript
  9. variable inside quotes javascript
  10. variable inside text javascript
  11. twig variable inside javascript
  12. php variable inside javascript function
  13. variable inside foreach javascript
  14. php variable inside javascript alert
  15. variable inside loop javascript
  16. jsp variable inside javascript
  17. razor variable inside javascript
  18. global variable inside javascript function
  19. smarty variable inside javascript
  20. access variable inside javascript function