Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas context.drawimage doesn't draw on the right coordinates

I have a canvas in the center of a website. When I perform a mouse click on the canvas, I want a small image to be drawn at the click-location. In manage to get the correct coordinates of a canvas click I structute a JavaScript-function like this:

function click( event ) {

    var ctxt;
    var myX =  event.clientX;
    var myY =  event.clientY;

    myX-=canvas.offsetTop;
    myY-=canvas.offsetLeft;

    ctxt = canvas.getContext("2d");

    ctxt.drawImage(myImage, myX, myY);

    alert(myX + " " + myY);
}

The alert function shows the correct coordinates, but the image is drawn at a location with much higher coordinate-values. If I click a little bit to far down or to the left, the image is not drawn at all (probably because its outside the canvas).

The drawn image has a x-coordinate that's about 3 times as high as the x-coordinate of the click, and the y-coordinate is about 5 times as high.

What can be the problem?

Hank

like image 311
iHank Avatar asked Oct 22 '25 05:10

iHank


2 Answers

You probably forgot to define a size for the canvas bitmap, and is only using CSS to set the size. Remember that canvas size must set implicit as CSS does not affect its bitmap size.

<canvas id="myCanvas" width=500 height=500></canvas>

If not your bitmap which defaults to 300x150 will be stretched to whatever you set with CSS which means your coordinates will be scaled as well.

CSS should be skipped for this but if you absolutely want to use it set width and height in CSS to the same size as defined for your canvas element.

The mouse position you get will be relative to the window so you need to subtract the canvas position to make it relative to canvas element. You probably have this working already and iHank's example should work, although I would not obtain the context each time:

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

canvas.addEventListener('click', mouseClick, false);

ctx.strokeRect(0, 0, canvas.width, canvas.height);

function mouseClick(e) {

    var rect = canvas.getBoundingClientRect(),
        x = e.clientX - rect.left,
        y = e.clientY - rect.top;
 
    // draw image here, for demo - drawn from corner not center:
    ctx.fillRect(x, y, 5, 5);      
}
Canvas: <canvas id="myCanvas" width=500 height=180></canvas>

Seems like I missed that the default size of a canvas was (300, 150). If I change the width and height of the canvas-object to the sizes specified in the cs-file, it works!

like image 42
iHank Avatar answered Oct 24 '25 17:10

iHank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!