主頁 > 知識庫 > HTML5 Canvas鼠標與鍵盤事件demo示例

HTML5 Canvas鼠標與鍵盤事件demo示例

熱門標簽:地圖標注柱狀圖 四川移動電銷外呼客戶管理系統(tǒng) 百度地圖標注為什么總是封號 咸陽穩(wěn)定外呼系統(tǒng)軟件 400開頭的電話好申請不 智能芯電話機器人 小朱地圖標注 怎么做百度地圖標注 臨海地圖標注app
演示HTML5 Canvas鼠標事件,獲取Canvas對象上的鼠標坐標,演示鍵盤事件通過鍵盤控制Canvas上對象移動。

Canvas對象支持所有的JavaScript的鼠標事件,包括鼠標點擊(MouseClick), 鼠標按下(Mouse Down), 鼠標抬起(Mouse Up),鼠標移動( Mouse Move)對Canvas添加鼠標事件方式有兩種,一種方式是通過API來完成:

復制代碼
代碼如下:

// mouse event
canvas.addEventListener("mousedown",doMouseDown,false);
canvas.addEventListener('mousemove', doMouseMove,false);
canvas.addEventListener('mouseup', doMouseUp, false);
另外一種方式在JavaScript中稱為反模式:


復制代碼
代碼如下:

canvas.onmousedown = function(e){
}
canvas.onmouseup = function(e){
}
canvas.onmousemove = function(e){
}

獲取鼠標在Canvas對象上坐標:
由于Canvas上鼠標事件中不能直接獲取鼠標在Canvas的坐標,所獲取的都是基于整個
屏幕的坐標。所以通過鼠標事件e.pageX與e.pageY來獲取鼠標位置,然后通過
Canvas. getBoundingClientRect()來獲取Canvas對象相對屏幕的相對位置,通過計算

得到鼠標在Canvas的坐標,代碼如下:

復制代碼
代碼如下:

function getPointOnCanvas(canvas, x, y) {
var bbox =canvas.getBoundingClientRect();
return { x: x- bbox.left *(canvas.width / bbox.width),
y:y - bbox.top * (canvas.height / bbox.height)
};
}

鍵盤事件:
HTML5 Canvas本身不支持鍵盤事件監(jiān)聽與獲取,常用的有兩種方法來解決這個問題:

一:通過windows對象來實現(xiàn)Canvas鍵盤事件監(jiān)聽與處理
// key event - use window as object
window.addEventListener('keydown', doKeyDown,true);

二:通過在Canvas對象上添加其它支持鍵盤事件的DOM元素實現(xiàn)鍵盤事件支持

復制代碼
代碼如下:

<canvas id="event_canvas"tabindex="0"></canvas>
// key event - use DOM element asobject
canvas.addEventListener('keydown', doKeyDown,true);
canvas.focus();

其中tabindex為HTML5 DOM元素,支持鍵盤事件。
演示,一個可以根據(jù)鍵盤上下左右移動的矩形塊:
 
一個完整的鼠標與鍵盤事件演示代碼如下:

復制代碼
代碼如下:

var tempContext = null; // global variable 2d context
var started = false;
var mText_canvas = null;
var x = 0, y =0;
window.add
window.onload = function() {
var canvas = document.getElementById("event_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}
// get 2D context of canvas and draw rectangel
tempContext = canvas.getContext("2d");
tempContext.fillStyle="blue";
x = canvas.width/2;
y = canvas.height/2;
tempContext.fillRect(x, y, 80, 40);
// key event - use DOM element as object
canvas.addEventListener('keydown', doKeyDown, true);
canvas.focus();
// key event - use window as object
window.addEventListener('keydown', doKeyDown, true);
// mouse event
canvas.addEventListener("mousedown", doMouseDown, false);
canvas.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('mouseup', doMouseUp, false);
}
function getPointOnCanvas(canvas, x, y) {
var bbox = canvas.getBoundingClientRect();
return { x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
};
}
function doKeyDown(e) {
var keyID = e.keyCode ? e.keyCode :e.which;
if(keyID === 38 || keyID === 87) { // up arrow and W
clearCanvas();
y = y - 10;
tempContext.fillRect(x, y, 80, 40);
e.preventDefault();
}
if(keyID === 39 || keyID === 68) { // right arrow and D
clearCanvas();
x = x + 10;
tempContext.fillRect(x, y, 80, 40);
e.preventDefault();
}
if(keyID === 40 || keyID === 83) { // down arrow and S
clearCanvas();
y = y + 10;
tempContext.fillRect(x, y, 80, 40);
e.preventDefault();
}
if(keyID === 37 || keyID === 65) { // left arrow and A
clearCanvas();
x = x - 10;
tempContext.fillRect(x, y, 80, 40);
e.preventDefault();
}
}
function clearCanvas() {
tempContext.clearRect(0, 0, 500, 500)
}
function doMouseDown(event) {
var x = event.pageX;
var y = event.pageY;
var canvas = event.target;
var loc = getPointOnCanvas(canvas, x, y);
console.log("mouse down at point( x:" + loc.x + ", y:" + loc.y + ")");
tempContext.beginPath();
tempContext.moveTo(loc.x, loc.y);
started = true;
}
function doMouseMove(event) {
var x = event.pageX;
var y = event.pageY;
var canvas = event.target;
var loc = getPointOnCanvas(canvas, x, y);
if (started) {
tempContext.lineTo(loc.x, loc.y);
tempContext.stroke();
}
}
function doMouseUp(event) {
console.log("mouse up now");
if (started) {
doMouseMove(event);
started = false;
}
}

HTML部分:

復制代碼
代碼如下:

<body>
<h1>HTML Canvas Event Demo - By Gloomy Fish</h1>
<pre>Press W, A, S, D keys to move</pre>
<div id="my_painter">
<canvas id="event_canvas" tabindex="0"></canvas>
</div>
</body>

標簽:平頂山 南平 陜西 黃石 公主嶺 黃石 山南

巨人網(wǎng)絡通訊聲明:本文標題《HTML5 Canvas鼠標與鍵盤事件demo示例》,本文關鍵詞  HTML5,Canvas,鼠標,與,鍵盤,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《HTML5 Canvas鼠標與鍵盤事件demo示例》相關的同類信息!
  • 本頁收集關于HTML5 Canvas鼠標與鍵盤事件demo示例的相關信息資訊供網(wǎng)民參考!
  • 推薦文章