开发者

Mouse Coordinates in HTML5 Canvas

I have tried many different ways of trying to get mouse coordinates in HTML5 canvas in compliment with video and none have seemed too work very well in either Chrome or Safari.

At the moment I am using:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script src="modernizr-1.6.min.js"></script>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded,false);
var videoElement;
var VideoDiv;
var Object1;
var Mouse = {
      x:0
      x:y}
function eventWindowLoaded(){
      videoElement = document.createElement("video");
      videoDiv = document.createElement('div');
      document.body.appendChild(videoDiv);
      videoDiv.appendChild(videoElement);
      videoDiv.setAttribute("style", "display:none;");
      var videoType = supportedVideoFormat(videoElement);
      if (videoType == ""){
            alert("no video support");
            return;
      }
      videoElement.setAttribute("src", "different_movement>" + videoType);
      videoElement.addEventListener("canplaythrough", videoLoaded, false);
}
function supportedVideoFormat(video){
      var returnExtension= "";
      if(video.canPlayType("video/webm") =="probably" || video.canPlayType("video/webm") == "maybe"){
            returnExtension = "webm";
      } else if (video.canPlayType("video/mp4") == "probably" || video.canPlayType("video/mp4") == "maybe"){
            returnExtension = "mp4";
      }else if(video.canPlayType("video/ogg") == "probably" || video.canPlayType("video/ogg") == "maybe"){
            returnExtension = "ogv";
      }
      return returnExtension;
}

function videoLoaded(event){
      canvasApp();
}
canvasOne.onmousemove = function (event){
      Mouse={
            x: event.offsetX,
            y: event.offsetY}
      }
}
function canvasApp(){
      function drawScreen(){
            context.drawImage(videoElement, 0, 0);
            context.fillStyle = '#ffffff';
            context.fillText(Mouse.x, 280, 280);
            context.fillText(Mouse.y, 280, 300);
            }
      var theCanvas = document.getElementByID('canvasOne');
      var context = theCanvas.getContext('2d');
      videoElement.play();

      setinterval(drawScreen, 33);
}
</script>
</head>
<body>
<canvas id="canvasOne" width="640" height="480">
Your browser does not support HTML5 Canvas.
</canvas>
</div>
</body>
</html>

The result of this is the 0,0 will be shown on the video from the initial variable set at 0,0 but then instead of changing as the mouse is moved around the screen, it stays 0,0. This leads me to believe that it is the part of the code that is finding the mouse coordinates that is not working.

I have tried various other attempts at finding mouse co开发者_如何学Cordinates including:

Mouse={
x: event.pageX,
y: event.pageX}

,

if (e.pageY)   {
    posy = e.pageY;
  } else if (e.clientY)   {
    posy = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
  }

,

var mouseX;
var mouseY;
var pieceX;
var pieceY;
if (e.pageX || e.pageY) { 
      mouseX = e.pageX;
      mouseX = e.pageY;
      } else { 
      mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
      mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
} 

My end product is supposed to be a video that has mouse interactions that will play sounds when certain parts on the video are clicked (thus the part of video). I have tried not using canvas at all for this, and instead positioning a image on top of the canvas which has image mapping on it, but it doesn't seem to work.

Another issue I am going to run into when I figure out mouse coordinates is what I will test collisions with the mouse coordinates to initiate it to play the sounds.

EDIT: Completely rewrote the code using e.offset, seems to work.


I used <iframe> to set an html page with a canvas element positioned top left of document. Then when I get clientX-Y it's origin is the top left of the canvas document, that's in the iframe, that you can have positioned anywhere on the canvas-containing document. It's easy as pie.

<iframe scrolling="no" height="100%" width="100%" src="canvas.html"></iframe>

also, I got the canvas to scale when it is scaled by style sheet. I added this to my canvas program.

c = canvas element, ctx = canvas context;
ctx.scale(c.width/630,c.height/800); // I originally intended it to be 630x800

(note: I am not sure if this answers your problem, but it is how I find coordinates without having to offset.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜