我很新的HTML5和JavaScript,下面的一些教程,
我已經想出了這樣的事情:查看別人的行爲
<html>
<head>
<title> Track Mouse </title>
<canvas id="myCanvas" width="400" height="200"
style="border:1px solid #000000;">
</canvas>
<script type="text/javascript">
function main()
{
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
window.onmousemove = handleMouseMove;
function handleMouseMove(event) {
event = event || window.event; // IE-ism
// event.clientX and event.clientY contain the mouse position
xPos = event.clientX;
yPos = event.clientY;
// Within entire canvas
if (xPos >= 0 && xPos <= myCanvas.width &&
yPos >= 0 && yPos <= myCanvas.height) {
document.Form1.posx.value = xPos;
document.Form1.posy.value = yPos;
// Top-left
if (xPos <= myCanvas.width/2 && xPos >= 0 &&
yPos <= myCanvas.height/2 && yPos >= 0) {
ctx.beginPath();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.moveTo(0, 0);
ctx.lineTo(xPos, yPos);
ctx.stroke();
}
// Top-right
if (xPos <= myCanvas.width && xPos >= myCanvas.width/2 &&
yPos <= myCanvas.height/2 && yPos >= 0) {
ctx.beginPath();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.moveTo(myCanvas.width, 0);
ctx.lineTo(xPos, yPos);
ctx.stroke();
}
// Bottom-left
if (xPos <= myCanvas.width && xPos >= 0 &&
yPos <= myCanvas.height && yPos >= myCanvas.height/2) {
ctx.beginPath();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.moveTo(0, myCanvas.height);
ctx.lineTo(xPos, yPos);
ctx.stroke();
}
// Bottom-right
if (xPos <= myCanvas.width && xPos >= myCanvas.width/2 &&
yPos <= myCanvas.height && yPos >= myCanvas.height/2) {
ctx.beginPath();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.moveTo(myCanvas.width, myCanvas.height);
ctx.lineTo(xPos, yPos);
ctx.stroke();
}
}
}
}
</script>
</head>
<body onload="main()">
<form name="Form1">
POSX: <input type="text" name="posx"><br>
POSY: <input type="text" name="posy"><br>
</form>
</body>
</html>
這裏是上面的代碼演示試運行:http://jsfiddle.net/9YXBD/
我想借此更進了一步,我希望它在同一時間顯示別人的動作。因此,例如,如果我有兩個Chrome瀏覽器打開並排,並在右窗口的畫布中進行移動,則我的移動也會顯示在左側窗口的畫布中。我查看了HTML5的server-side events,但我不知道從哪裏開始。
什麼你有一臺服務器了嗎? –
我在HostMonster上有一臺主機服務器 – user3397452
我應該澄清。你有一個你想用這個功能增強的服務器端Web應用程序嗎? –