简体   繁体   English

如何在Javascript中使用鼠标事件在画布上绘画

[英]How to draw on canvas using mouse events with Javascript

I am trying to create a canvas that allows me to draw on it rectangles using mouse events. 我正在尝试创建一个画布,允许我使用鼠标事件在其上绘制矩形。 Very similar to the way selection works in windows, I want to be able to click down anywhere on my page, drag left/right/up/down, then once I release, a rectangle is drawn with the starting coordinates (x1 and y1) on mousedown and the ending coordinates (x2, y2) on mouseup . 与Windows中选择的工作方式非常相似,我希望能够在页面上的任意位置单击,向左/向右/向上/向下拖动,然后释放后,将绘制一个带有起始坐标(x1和y1)的矩形在mousedownmousedown ,在mouseup上使用结束坐标(x2,y2)。

I've been trying to create eventListener s bound to my canvas that return the starting and ending coordinates on mousedown and mouseup respectively, but my variables stay undefined. 我一直在尝试创建绑定到画布的eventListener ,它们分别返回mousedownmouseup的开始和结束坐标,但是我的变量保持未定义状态。

My canvas spans the entire length of my window so no need to take into account relative positioning. 我的画布跨越了窗口的整个长度,因此无需考虑相对位置。 Also the solution has to be implemented using purely Javascript, no JQuery allowed. 同样,该解决方案必须使用纯Javascript来实现,不允许使用JQuery。

Thank you for your help! 谢谢您的帮助!

UPDATE 更新

Ignore most of what is underneath, re-reading your question, it seems like you have the theory nailed. 忽略下面的大部分内容,重新阅读您的问题,似乎您已扎根理论。 Your problem is most likely the mousedown and mouseup function parameters are missing. 您的问题很可能是缺少mousedown和mouseup函数参数。 Try the following; 尝试以下方法;

document.getElementsByTagName('canvas')[0].addEventListener('mousedown', function(e){
 // I THINK IT'S THE e ON THE LINE ABOVE THIS THAT YOU'RE MISSING.
 // YOU CAN THEN ACCESS THE POSITION OF THE MOUSE USING;

 mouse.x = e.pageX;
 mouse.Y = e.pageY;
})

I won't write the code for you, but I can give you theory. 我不会为您编写代码,但是我可以为您提供理论。

Store your mouse x & y variables in an object such as mouse = {} . 将鼠标的x和y变量存储在诸如mouse = {}的对象中。

Add an event listener to the canvas for mouse down (click) and store the e.pageX and e.pageY in mouse.firstX and mouse.firstY. 将事件侦听器添加到画布中以使鼠标向下(单击),并将e.pageX和e.pageY存储在mouse.firstX和mouse.firstY中。 Use something like: 使用类似:

document.getElementsByTagName('canvas')[0].addEventListener('mousedown', function(e){ mouse.firstX = e.pageX; mouse.firstY = e.pageY; })

Create a second event listener for the canvas for mouseup and store this set of e.pageX and e.pageY in mouse.secondX and mouse.secondY. 为mouseup的画布创建第二个事件侦听器,并将这组e.pageX和e.pageY存储在mouse.secondX和mouse.secondY中。

Calculate the difference between firstX and secondX, firstY and secondY to work out big your rectangle sound be. 计算firstX和secondX,firstY和secondY之间的差,以得出矩形声音。

var width = mouse.firstX + mouse.secondX // rect width

var height = mouse.firstY + mouse.secondY // rect height

Then, using these calculations, draw a rectangle on your canvas using firstX and firstY as the position parameters. 然后,使用这些计算,使用firstX和firstY作为位置参数在画布上绘制一个矩形。

I hope this is relatively clear and helpful for you. 我希望这相对清晰,对您有帮助。 If not, this might help; 如果没有,这可能会有所帮助;

http://simonsarris.com/blog/140-canvas-moving-selectable-shapes http://simonsarris.com/blog/140-canvas-moving-selectable-shapes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM