[js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标

时间:2022-10-06 17:26:39

有了前面的canvas基础之后,现在开始就精彩了,后面写的canvas教程都是属于综合应用,前面已经写了常用的canvas基础知识,参考链接如下:

[js高手之路] html5 canvas系列教程 - 认识canvas以及基本使用方法

[js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解

[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)

[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)

[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

[js高手之路] html5 canvas系列教程 - 图片操作(drawImage,clip,createPattern)

[js高手之路] html5 canvas系列教程 - 状态详解(save与restore)

[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)

[js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

[js高手之路] html5新增的定时器requestAnimationFrame实战进度条

本文来做一个简单的实时获取鼠标坐标的功能,在canvas动画开发中,获取鼠标的坐标,键盘的按键等等,都是常用的操作,我们就慢慢得把他们封装成一个公共库。

一、事件的兼容:

 function bindEvent(obj, event, fn) {
if (obj.attachEvent) { //ie
obj.attachEvent('on' + event, function () {
fn.call(obj);
});
} else {
//chrome&ff
obj.addEventListener(event, fn, false);
}
}

上面兼容ie8以及修正this关键字在ie低版本的指向,下面兼容chrome和ff。其他更常用的封装可以参考我的javascript开源框架gdom

二、用立即表达式搭建一个基本的库

添加获取鼠标坐标的方法

 ;(function (window) {
window.G = {};
function bindEvent(obj, event, fn) {
if (obj.attachEvent) { //ie
obj.attachEvent('on' + event, function () {
fn.call(obj);
});
} else {
//chrome&ff
obj.addEventListener(event, fn, false);
}
} G.getPos = function( dom ){
var oPos = { x : 0, y : 0 };
bindEvent( dom, 'mousemove', function( ev ){
var oEvent = ev || event, x, y;
if ( oEvent.pageX || oEvent.pageY ){
x = oEvent.pageX;
y = oEvent.pageY;
}else {
x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
}
x -= dom.offsetLeft;
y -= dom.offsetTop;
oPos.x = x;
oPos.y = y;
} );
return oPos;
}; })(window);

三、引入封装的js库,绑定canvas为监听对象,打印当前鼠标的坐标

鼠标的坐标,我这里画了2根线,便于观察.

 <head>
<meta charset='utf-8' />
<script>
;(function (window) {
window.G = {};
function bindEvent(obj, event, fn) {
if (obj.attachEvent) { //ie
obj.attachEvent('on' + event, function () {
fn.call(obj);
});
} else {
//chrome&ff
obj.addEventListener(event, fn, false);
}
} G.getPos = function( dom ){
var oPos = { x : 0, y : 0 };
bindEvent( dom, 'mousemove', function( ev ){
var oEvent = ev || event, x, y;
if ( oEvent.pageX || oEvent.pageY ){
x = oEvent.pageX;
y = oEvent.pageY;
}else {
x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
}
x -= dom.offsetLeft;
y -= dom.offsetTop;
oPos.x = x;
oPos.y = y;
} );
return oPos;
}; })(window);
</script>
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
width = oCanvas.width, height = oCanvas.height,
oInfo = document.querySelector( "#info" ),
oPos = G.getPos( oCanvas );
oCanvas.addEventListener( "mousemove", function(){ oGc.clearRect( 0, 0, width, height );
oGc.beginPath();
oGc.moveTo( oPos.x, 0 );
oGc.lineTo( oPos.x, height );
oGc.moveTo( 0, oPos.y );
oGc.lineTo( width, oPos.y );
oGc.closePath();
oGc.strokeStyle = '#09f';
oGc.stroke(); oInfo.innerHTML = '鼠标的当前坐标是:(' + oPos.x + ',' + oPos.y + ')';
}, false );
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<div id="info"></div>
</body>

四、点击'run code'可以预览效果额

<head>
<meta charset='utf-8' />
<script>
;(function (window) {
window.G = {};
function bindEvent(obj, event, fn) {
if (obj.attachEvent) { //ie
obj.attachEvent('on' + event, function () {
fn.call(obj);
});
} else {
//chrome&ff
obj.addEventListener(event, fn, false);
}
}

G.getPos = function( dom ){
var oPos = { x : 0, y : 0 };
bindEvent( dom, 'mousemove', function( ev ){
var oEvent = ev || event, x, y;
if ( oEvent.pageX || oEvent.pageY ){
x = oEvent.pageX;
y = oEvent.pageY;
}else {
x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
}
x -= dom.offsetLeft;
y -= dom.offsetTop;
oPos.x = x;
oPos.y = y;
} );
return oPos;
};

})(window);
</script>
<style>
#canvas{
border:1px dashed #aaa;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
width = oCanvas.width, height = oCanvas.height,
oInfo = document.querySelector( "#info" ),
oPos = G.getPos( oCanvas );
oCanvas.addEventListener( "mousemove", function(){

oGc.clearRect( 0, 0, width, height );
oGc.beginPath();
oGc.moveTo( oPos.x, 0 );
oGc.lineTo( oPos.x, height );
oGc.moveTo( 0, oPos.y );
oGc.lineTo( width, oPos.y );
oGc.closePath();
oGc.strokeStyle = '#09f';
oGc.stroke();

oInfo.innerHTML = '鼠标的当前坐标是:(' + oPos.x + ',' + oPos.y + ')';
}, false );
}
</script>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<div id="info"></div>
</body>
run code