简体   繁体   English

油漆画布无法正常工作

[英]Paint canvas not working properly

http://www.asifslab.com/reveal.js-master/Why%20does%20wind%20feel%20cold.html#/4 Why doesn't the drawing canvas work properly? http://www.asifslab.com/reveal.js-master/为什么%20does%20wind%20feel%20cold.html# / 4为什么绘图画布无法正常工作? The line drawn is away from the point clicked. 绘制的线远离单击的点。 However, if I use the canvas out of reveal.js it works perfectly. 但是,如果我在Reveal.js中使用画布,则它可以完美工作。 http://codepen.io/anon/pen/eEaKh Also when the erase function is run, it leaves a white border outside. http://codepen.io/anon/pen/eEaKh此外,运行擦除功能时,它还会在外面留下白色边框。 How do I fix these problems? 我该如何解决这些问题?

just change e.pageX to e.clientX and e.pageY to e.clientY because in your codepen account 只是改变e.pageXe.clientXe.pageYe.clientY因为在你codepen帐户
canvas origin and page origin is almost at same place but in other it is not. 画布原点和页面原点几乎位于同一位置,但在其他地方则不同。

To calculate the mouse position you need to subtract the position of the canvas: 要计算鼠标位置,您需要减去画布的位置:

Here is one way of doing this (inside the event handler): 这是一种方法(在事件处理程序内部):

var rect = canvas.getBoundingClientRect(),
    x = e.clientX - rect.left,
    y = e.clientY - rect.top;

Now your x and y will be relative to canvas. 现在,您的xy将相对于画布。

Here's a copy/pasta of a small part of my paint app. 这是我的绘画应用程序一小部分的副本/部分。 Notice I'm using offsets of the canvas in my calculations. 注意,我在计算中使用的是画布的偏移量。 I also have a zoom function that scales the canvas, so taking into account that I added it to the calculation of the mouse cursor. 我还具有缩放画布的缩放功能,因此考虑到将其添加到鼠标光标的计算中。

$('canvas').mousemove(function(e) {
    // x and y are globals, x inits to null, mousedown sets x, mouseup returns x to null
    if (x==null) return;
    x = (100/$('#zoom').val())*(e.pageX - $(this).offset().left);
    y = (100/$('#zoom').val())*(e.pageY - $(this).offset().top);

    $('#debug').html(x+', '+y); // constantly update (x,y) position in a fixed div during debugging
});

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

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