javascript如何获取鼠标的坐标

u010313216 u010313216 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大神,我想做一个指向鼠标位置的箭头arrow,定义了this.mou**和this.mouseY的属性,在onmousemove中赋值,可是在更新方法enterFrame中却获取不到,请看看是**问题了,麻烦各位了,谢谢。
function Arrow(canvas){
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.x = 250;
this.y = 200;
this.rotation = 0;

Arrow.prototype.drawArrow = function(){
this.ctx.beginPath();
this.ctx.moveTo(-50,-25);
this.ctx.lineTo(0,-25);
this.ctx.lineTo(0,-50);
this.ctx.lineTo(50,0);
this.ctx.lineTo(0,50);
this.ctx.lineTo(0,25);
this.ctx.lineTo(-50,25);
this.ctx.lineTo(-50,-25);
this.ctx.stroke();
}

}

function RotateToMouse(canvas){
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.arrow;

this.mouseX;
this.mouseY;

RotateToMouse.prototype.init = function(){
this.arrow = new Arrow(canvas);
this.ctx.translate(this.arrow.x,this.arrow.y);

var self = this;
this.intervalID = setInterval(function(){self.enterFrame();},1000/2);

canvas.addEventListener("mousemove",function(e){
return self.getMousePosition(e);
});
}

RotateToMouse.prototype.getMousePosition = function(event){
event = event || window.event;
if(event.pageX || event.pageY){
this.mouseX = event.pageX;
this.mouseY = event.pageY;
}else{
this.mouseX = event.clientX + document.body.scrollLeft - document.body.clientLeft;
this.mouseY = event.clientY + document.body.scrollTop - document.body.clientTop;
}
}

RotateToMouse.prototype.enterFrame = function(){
this.arrow.rotation = Math.atan2(this.mouseY - arrow.y, this.mouseX - arrow.x);
this.ctx.rotate(this.arrow.rotation * Math.PI/180);
this.arrow.drawArrow();
}
this.init();
}

http://bbs.html5cn.org/forum.php?mod=viewthread&tid=86961&extra=
...全文

相关推荐

javascript获取当前鼠标坐标的方法

本文实例讲述了javascript获取当前鼠标坐标的方法。分享给大家供大家参考。具体实现方法如下: 对于javascript获取当前鼠标坐标来说,得对不同浏览器的坐标位置有所了解。具体代码如下: 代码如下:<html> <head> <title>javascript获取当前鼠标坐标</title> <meta http-equiv=”content-type” content=”text/html;charset=utf-8″ /> [removed]  function mousePosition(ev){   if(ev.pageX || e

weixin_38564598

JS获取鼠标坐标位置实例分析

主要介绍了JS获取鼠标坐标位置的方法,结合实例形式分析了JavaScript常见的获取鼠标页面、屏幕等坐标位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

weixin_38728347

JS获取鼠标坐标、获取鼠标像素点示例

JS获取鼠标坐标,获取鼠标像素点的典型实例,运行本页面后,随意移动鼠标的位置,可适时显现鼠标的坐标点,不占用系统资源 。 JavaScript获取鼠标坐标 .tip { width:200px; border:2px solid #ddd; padding:8px; background:#f1f1f1; color:#666; } 源码爱好者 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

weixin_38605801

JS获取鼠标坐标的实例方法

介绍了JS获取鼠标坐标的实例方法,有需要的朋友可以参考一下

weixin_38650508

javascript获取鼠标当前位置坐标并显示

javascript获取鼠标当前位置坐标,并显示出来

星辰hq
评论 8
收藏 已收藏
分享
返回顶部
u010313216
JavaScript 87,984 成员 224,681 内容