HTML5 Canvas鼠標(biāo)與鍵盤事件demo示例
發(fā)布時(shí)間:2013-07-04 15:52:02 作者:佚名
我要評(píng)論

本文的主要母的是演示HTML5 Canvas鼠標(biāo)事件,獲取Canvas對(duì)象上的鼠標(biāo)坐標(biāo),演示鍵盤事件通過(guò)鍵盤控制Canvas上對(duì)象移動(dòng),感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助
演示HTML5 Canvas鼠標(biāo)事件,獲取Canvas對(duì)象上的鼠標(biāo)坐標(biāo),演示鍵盤事件通過(guò)鍵盤控制Canvas上對(duì)象移動(dòng)。
Canvas對(duì)象支持所有的JavaScript的鼠標(biāo)事件,包括鼠標(biāo)點(diǎn)擊(MouseClick), 鼠標(biāo)按下(Mouse Down), 鼠標(biāo)抬起(Mouse Up),鼠標(biāo)移動(dòng)( Mouse Move)對(duì)Canvas添加鼠標(biāo)事件方式有兩種,一種方式是通過(guò)API來(lái)完成:
// 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){
}
獲取鼠標(biāo)在Canvas對(duì)象上坐標(biāo):
由于Canvas上鼠標(biāo)事件中不能直接獲取鼠標(biāo)在Canvas的坐標(biāo),所獲取的都是基于整個(gè)
屏幕的坐標(biāo)。所以通過(guò)鼠標(biāo)事件e.pageX與e.pageY來(lái)獲取鼠標(biāo)位置,然后通過(guò)
Canvas. getBoundingClientRect()來(lái)獲取Canvas對(duì)象相對(duì)屏幕的相對(duì)位置,通過(guò)計(jì)算
得到鼠標(biāo)在Canvas的坐標(biāo),代碼如下:
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)聽與獲取,常用的有兩種方法來(lái)解決這個(gè)問(wèn)題:
一:通過(guò)windows對(duì)象來(lái)實(shí)現(xiàn)Canvas鍵盤事件監(jiān)聽與處理
// key event - use window as object
window.addEventListener('keydown', doKeyDown,true);
二:通過(guò)在Canvas對(duì)象上添加其它支持鍵盤事件的DOM元素實(shí)現(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元素,支持鍵盤事件。
演示,一個(gè)可以根據(jù)鍵盤上下左右移動(dòng)的矩形塊:
一個(gè)完整的鼠標(biāo)與鍵盤事件演示代碼如下:
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>
Canvas對(duì)象支持所有的JavaScript的鼠標(biāo)事件,包括鼠標(biāo)點(diǎn)擊(MouseClick), 鼠標(biāo)按下(Mouse Down), 鼠標(biāo)抬起(Mouse Up),鼠標(biāo)移動(dòng)( Mouse Move)對(duì)Canvas添加鼠標(biāo)事件方式有兩種,一種方式是通過(guò)API來(lái)完成:
復(fù)制代碼
代碼如下:// mouse event
canvas.addEventListener("mousedown",doMouseDown,false);
canvas.addEventListener('mousemove', doMouseMove,false);
canvas.addEventListener('mouseup', doMouseUp, false);
另外一種方式在JavaScript中稱為反模式:
復(fù)制代碼
代碼如下:canvas.onmousedown = function(e){
}
canvas.onmouseup = function(e){
}
canvas.onmousemove = function(e){
}
獲取鼠標(biāo)在Canvas對(duì)象上坐標(biāo):
由于Canvas上鼠標(biāo)事件中不能直接獲取鼠標(biāo)在Canvas的坐標(biāo),所獲取的都是基于整個(gè)
屏幕的坐標(biāo)。所以通過(guò)鼠標(biāo)事件e.pageX與e.pageY來(lái)獲取鼠標(biāo)位置,然后通過(guò)
Canvas. getBoundingClientRect()來(lái)獲取Canvas對(duì)象相對(duì)屏幕的相對(duì)位置,通過(guò)計(jì)算
得到鼠標(biāo)在Canvas的坐標(biāo),代碼如下:
復(fù)制代碼
代碼如下: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)聽與獲取,常用的有兩種方法來(lái)解決這個(gè)問(wèn)題:
一:通過(guò)windows對(duì)象來(lái)實(shí)現(xiàn)Canvas鍵盤事件監(jiān)聽與處理
// key event - use window as object
window.addEventListener('keydown', doKeyDown,true);
二:通過(guò)在Canvas對(duì)象上添加其它支持鍵盤事件的DOM元素實(shí)現(xiàn)鍵盤事件支持
復(fù)制代碼
代碼如下:<canvas id="event_canvas"tabindex="0"></canvas>
// key event - use DOM element asobject
canvas.addEventListener('keydown', doKeyDown,true);
canvas.focus();
其中tabindex為HTML5 DOM元素,支持鍵盤事件。
演示,一個(gè)可以根據(jù)鍵盤上下左右移動(dòng)的矩形塊:

一個(gè)完整的鼠標(biāo)與鍵盤事件演示代碼如下:
復(fù)制代碼
代碼如下: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部分:
復(fù)制代碼
代碼如下:<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>
相關(guān)文章
如何在Canvas上的圖形/圖像綁定事件監(jiān)聽的實(shí)現(xiàn)
這篇文章主要介紹了如何在Canvas上的圖形/圖像綁定事件監(jiān)聽的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著2020-09-16- 這篇文章主要介紹了如何在Canvas中添加事件的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)2019-05-21
html5中監(jiān)聽canvas內(nèi)部元素點(diǎn)擊事件的三種方法
這篇文章主要介紹了html5中監(jiān)聽canvas內(nèi)部元素點(diǎn)擊事件的三種方法2019-04-28- 這篇文章主要介紹了詳解Canvas事件綁定的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-27
- 這篇文章主要介紹了HTML5 Canvas的事件處理介紹,本文講解了Canvas的限制、給Canvas元素綁定事件、isPointInPath方法、循環(huán)重繪和事件冒泡等內(nèi)容,需要的朋友可以參考下2015-04-24
一個(gè)不錯(cuò)的HTML5 Canvas多層點(diǎn)擊事件監(jiān)聽實(shí)例
寫到一個(gè)多層點(diǎn)擊事件的監(jiān)聽。覺得還是挺好玩的。于是把它從模塊中抽化出來(lái)了,喜歡的朋友可以參考下2014-04-29- 這篇文章主要介紹了詳解如何在Canvas中添加事件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)2021-04-16