画布元素:单击的图像

发布于 2024-10-05 21:30:05 字数 1576 浏览 3 评论 0原文

我想知道用户是否单击了画布中绘制的图像。我点击图像但没有任何反应。警报没有被调用。最后一个 if 条件永远不会通过。有什么想法吗?

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="wrapper">
            <canvas id="game" height="500" width="700">
            </canvas>
        </div>
        <script>
        (function() {
            var canvas = document.getElementById('game'),
                context = canvas.getContext('2d'),
                fps = 1,
                character = Image(),
                positions = [[125, 55], [480, 55], [125, 185], [480, 182], [125, 315], [480, 315]],
                random, x, y, xc, yc = null;

            canvas.addEventListener('click', function(event) {
                xc = event.screenX - canvas.offsetLeft;
                yc = event.screenY - canvas.offsetTop;

                if((xc >= x) && (xc <= (x + character.width)) && (yc >= y) && (yc <= (y + character.height))) {
                    alert('X = ' + x + 'Y = ' + y);
                }
            }, true);

            character.src = 'character.png';

            setInterval(function() {
                random = (Math.floor(Math.random() * 6));

                random = positions[random];
                x = random[0];
                y = random[1];

                context.clearRect(0, 0, canvas.width, canvas.height);
                context.drawImage(character, x, y);
            }, 1000 / fps);
        }());
        </script>
    </body>
</html>

I want to know if the user clicked over an image drawed in a canvas. I click over the image but nothing happens. The alert isn't called. The last if condition never pass. Any idea?

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="wrapper">
            <canvas id="game" height="500" width="700">
            </canvas>
        </div>
        <script>
        (function() {
            var canvas = document.getElementById('game'),
                context = canvas.getContext('2d'),
                fps = 1,
                character = Image(),
                positions = [[125, 55], [480, 55], [125, 185], [480, 182], [125, 315], [480, 315]],
                random, x, y, xc, yc = null;

            canvas.addEventListener('click', function(event) {
                xc = event.screenX - canvas.offsetLeft;
                yc = event.screenY - canvas.offsetTop;

                if((xc >= x) && (xc <= (x + character.width)) && (yc >= y) && (yc <= (y + character.height))) {
                    alert('X = ' + x + 'Y = ' + y);
                }
            }, true);

            character.src = 'character.png';

            setInterval(function() {
                random = (Math.floor(Math.random() * 6));

                random = positions[random];
                x = random[0];
                y = random[1];

                context.clearRect(0, 0, canvas.width, canvas.height);
                context.drawImage(character, x, y);
            }, 1000 / fps);
        }());
        </script>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-10-12 21:30:05

这里有两个问题。

首先,你的事件代码坏了screenX返回的是鼠标相对于screen的位置,你需要使用clientX或者在IE中pageX 请参阅 quirksmode 了解更多信息。

canvas.addEventListener('click', function(event) {
    event = event || window.event; // IE does not pass the event param!

    // you should use var infront of these local variables
    // otherwise you leak them into the global namespace
    // and you can even overwrite things there if you don't watch out
    var xc = (event.clientX ? event.clientX : pageX) - canvas.offsetLeft;
    var yc = (event.clientY ? event.clientY : pageY) - canvas.offsetTop;

其次,代码永远不会在 Chrome 中运行!它在第 15 行失败,并显示 Uncaught TypeError: DOM object constructor cannot be call as a function.。创建图像时需要使用 new 关键字,因为不能保证它否则,Image() 将返回一个新实例。

...
context = canvas.getContext('2d'),
fps = 1,
character = new Image(),
...

Two problems here.

First, your event code is broken screenX returns the mouse position relative to the screen, you need to use clientX or in IE pageX see quirksmode for more information.

canvas.addEventListener('click', function(event) {
    event = event || window.event; // IE does not pass the event param!

    // you should use var infront of these local variables
    // otherwise you leak them into the global namespace
    // and you can even overwrite things there if you don't watch out
    var xc = (event.clientX ? event.clientX : pageX) - canvas.offsetLeft;
    var yc = (event.clientY ? event.clientY : pageY) - canvas.offsetTop;

Second, the code never runs in Chrome! It fails with Uncaught TypeError: DOM object constructor cannot be called as a function. at line 15. You need to use the new keyword when creating the image, since it's not guaranteed otherwise, that the Image() will return a new instance.

...
context = canvas.getContext('2d'),
fps = 1,
character = new Image(),
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文