HTML5 Canvas 和 Firefox 4 - 获取点击坐标

发布于 2024-11-03 05:50:38 字数 2079 浏览 5 评论 0原文

我一直致力于大量 HTML5 视频和 Canvas 演示。到目前为止我主要关注 Chrome,但现在我也尝试针对 Firefox 和 Safari 优化它们。

我现在正在做的工作是在 Canvas 中绘制视频并将视频移动到鼠标单击位置。到目前为止,我的功能在 Chrome 和 Safari 中有效,但在 Firefox 中无效。我无法找到有关这些主题的太多信息(点击事件、坐标、特定于 Firefox 的等)。我从这里复制了代码:

如何获取鼠标在画布元素上单击的坐标? http:// /answers.oreilly.com/topic/1929-how-to-use-the-canvas-and-draw-elements-in-html5/

因为他们给我的印象是它应该适用于所有浏览器,但 Firefox仍然拒绝。它所做的只是显示视频,对鼠标点击没有反应。

这是我的代码(不包括变量的定义):

function activateVideo(){
setTarget();
videoElement.play();
animate();
}

function setTarget(){   
targetX=xCoord;
targetY=yCoord
moverX=(targetX-currentX)/100;
moverY=(targetY-currentY)/100;  
}
canvasElement.addEventListener('click', function(){
    /*xCoord = event.clientX - canvasElement.offsetLeft;
    yCoord = event.clientY - canvasElement.offsetTop;
    txtCount.value = xCoord + " + " + yCoord;*/
    if (event.pageX || event.pageY) {
        xCoord = event.pageX;
        yCoord = event.pageY;
    } else {
        xCoord = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        yCoord = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    xCoord -= canvasElement.offsetLeft;
    yCoord -= canvasElement.offsetTop;
    setTarget();
},false);

function animate(){
currentX += moverX;
currentY += moverY;
if(dist(currentX,targetX,currentY,targetY)<1) {
    moverX=0;
    moverY=0;
}
drawVideo(videoElement,context,320,256);
timer = setTimeout(animate,20);
}

function dist(x1,x2,y1,y2){
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

function drawVideo(videoElement,context,w,h) {
context.clearRect(0,0,1000,600);
context.drawImage(videoElement,currentX,currentY,w,h);
}
playCanvas.addEventListener('click', activateVideo, false);

所以显然我有点迷失了,如果有人能指出我正确的方向,我将不胜感激。

I've been working on a bunch of HTML5 Video and Canvas demos. Til now I focused on Chrome, but now I'm trying to optimize them for Firefox and Safari as well.

The one I'm working on right now draws a video in Canvas and moves the video to mouseclick positions. What I have so far works in Chrome and Safari, but not in Firefox. I haven't been able to find much info on these topics (click events, coordinates, firefox-specific etc). I copied the code from here:

How do I get the coordinates of a mouse click on a canvas element?
http://answers.oreilly.com/topic/1929-how-to-use-the-canvas-and-draw-elements-in-html5/

because they gave me the impression it should work in all browsers, but Firefox still refuses. All it does is display the video, it doesn't react to mouse clicks.

This is my code (not including the definition of variables):

function activateVideo(){
setTarget();
videoElement.play();
animate();
}

function setTarget(){   
targetX=xCoord;
targetY=yCoord
moverX=(targetX-currentX)/100;
moverY=(targetY-currentY)/100;  
}
canvasElement.addEventListener('click', function(){
    /*xCoord = event.clientX - canvasElement.offsetLeft;
    yCoord = event.clientY - canvasElement.offsetTop;
    txtCount.value = xCoord + " + " + yCoord;*/
    if (event.pageX || event.pageY) {
        xCoord = event.pageX;
        yCoord = event.pageY;
    } else {
        xCoord = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        yCoord = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    xCoord -= canvasElement.offsetLeft;
    yCoord -= canvasElement.offsetTop;
    setTarget();
},false);

function animate(){
currentX += moverX;
currentY += moverY;
if(dist(currentX,targetX,currentY,targetY)<1) {
    moverX=0;
    moverY=0;
}
drawVideo(videoElement,context,320,256);
timer = setTimeout(animate,20);
}

function dist(x1,x2,y1,y2){
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

function drawVideo(videoElement,context,w,h) {
context.clearRect(0,0,1000,600);
context.drawImage(videoElement,currentX,currentY,w,h);
}
playCanvas.addEventListener('click', activateVideo, false);

So obviously I'm a bit lost, if anybody could point me in the right direction, I would appreciate it.

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

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

发布评论

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

评论(2

无畏 2024-11-10 05:50:38

我有一个使用 jquery 的工作版本,这可能是最跨浏览器的方式。没那么糟糕。这是我的小提琴 http://jsfiddle.net/jaredwilli/D3PWN/

代码

var canvas = $('canvas');
canvas.mousemove(function(e){
    var pageCrds = '('+ e.pageX +', '+ e.pageY +')',
        clientCrds = '('+ e.clientX +', '+ e.clientY +')';

    $('span:first').text('(e.pageX, e.pageY) - '+ pageCrds);    
    $('span:last').text('(e.clientX, e.clientY) - '+ clientCrds);

});

var arr = [];
canvas.click(function(e) {
    arr.unshift($('span.first').text());
    console.log(arr);
});

希望这可以帮助你一些。 :)

I have a working version of this that uses jquery, which is likely the most cross-browser way of doing it. It's not that bad. Here's my fiddle http://jsfiddle.net/jaredwilli/D3PWN/

Code

var canvas = $('canvas');
canvas.mousemove(function(e){
    var pageCrds = '('+ e.pageX +', '+ e.pageY +')',
        clientCrds = '('+ e.clientX +', '+ e.clientY +')';

    $('span:first').text('(e.pageX, e.pageY) - '+ pageCrds);    
    $('span:last').text('(e.clientX, e.clientY) - '+ clientCrds);

});

var arr = [];
canvas.click(function(e) {
    arr.unshift($('span.first').text());
    console.log(arr);
});

Hope this helps you out some. :)

回心转意 2024-11-10 05:50:38

您需要将事件对象传递给 FF 的处理程序(IE 可以工作,因为 event 可通过 window.event 获得)

canvasElement.addEventListener('click', function(event){
   event = event || window.event;
   ...

You need to pass the event object to the handler for FF (IE works becuse event is available via window.event)

canvasElement.addEventListener('click', function(event){
   event = event || window.event;
   ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文