Adam Rackis
Adam Rackis

Reputation: 83358

Canvas tag is shifted on my page

I'm trying to simply track the mouse over my canvas tag, and log how far it is from the center of a circle. The problem is, it's off slightly.

When I'm on the right side of the circle, the mouse has to be about 1 cm inside the circle before I show a distance of r (radius = 200). When I'm on the left side of the circle, I have to be about 1cm outside the circle to register a distance of r. It's like the circle is shifted a tad to the right.

I tried reproducing this with a fiddle, but oddly enough, the fiddle is perfect; it's spot on.

So I guess my question is, what would cause my canvas tag to be shifted about 1cm on my page (but not on the fiddle). Do I need a better DOCTYPE? I reproduce my entire source below. I've tested in both FF and Chrome—same result.

EDIT

I also tried <!DOCTYPE html> to no avail.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        $(function () {

            var context = document.getElementById("myCanvas").getContext("2d");

            context.beginPath();
            context.moveTo(250, 250);
            context.arc(250, 250, 200, 0, Math.PI * 2, false);
            context.closePath();
            context.fillStyle = 'rgb(255,100,0)';
            context.fill();


            $("canvas").mousemove(function (e) {
                var x = e.pageX;
                var y = e.pageY;

                var dist = Math.round(Math.sqrt(Math.pow(x - 250.0, 2) + Math.pow(y - 250.0, 2)));

                console.log(dist);
            });
        });


    </script>


</head>
<body>

    <canvas id="myCanvas" width="800" height="800">

    </canvas>

</body>
</html>

Upvotes: 0

Views: 184

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

Yeah, actually it seems that body{margin:0, padding:0, border:0} does the trick. Also, on a side note, rather than calling all those Math functions the following would be simpler and faster:

var x = e.pageX - 250;
var y = e.pageY - 250;
var dist = Math.round(Math.sqrt(x * x + y * y));

Also, using a css reset kit would probably solve all your problems for all browsers.

I was curious, so I checked the defaults:

var bodyElement = $('body');

// Get the padding
var widthPadding = bodyElement.css('padding-left') + bodyElement.css('padding-right');
var heightPadding = bodyElement.css('padding-top') + bodyElement.css('padding-bottom');
console.log(widthPadding + ", "+ heightPadding);
// Get the margins
var widthMargin = bodyElement.css('margin-left') + bodyElement.css('margin-right');
var heightMargin = bodyElement.css('margin-top') + bodyElement.css('margin-bottom');
console.log(widthMargin + ", "+ heightMargin);
// Get the borders
var widthBorder = bodyElement.css('border-left-width') + bodyElement.css('border-right-width');
var heightBorder = bodyElement.css('border-top-width') + bodyElement.css('border-bottom-width');
console.log(widthBorder + ", "+ heightBorder);

The output was like this on FF 9.0.1 was:

0px0px, 0px0px 
8px8px, 8px8px <-- So obviously you only need to reset the margins to 0
0px0px, 0px0px

Upvotes: 1

Related Questions