简体   繁体   English

使用JavaScript在画布上倾斜线

[英]Skew a line on canvas using JavaScript

I am drawing lines on a canvas. 我在画布上画线。 The user can select a particular line and be able to skew that line. 用户可以选择特定的行,并可以使该行倾斜。 By skew , I mean they can drag one end point of the line to a desired point on the same x-axis. 通过偏斜 ,我的意思是他们可以将线的一个端点拖动到同一x轴上的所需点。 How can I do this using JavaScript and HTML5 canvas? 如何使用JavaScript和HTML5画布执行此操作?

The general way to draw a line is this: 画线的一般方法是:

ctx.moveTo(line.startX, line.startY);
ctx.lineTo(line.endX, line.endY);
ctx.stroke();

and then you can add EventListeners and check to see if the mouse is near the line... 然后您可以添加EventListeners并检查鼠标是否在线条附近...

window.addEventListener("mousemove", function(e)
{
    mouse.x = e.layerX || e.offsetX;
    mouse.y = e.layerY || e.offsetY;
    // check to see if the mouse is near the line(s) here...
    // you can change to x/y and start/end
    // example:
    if (mouse.x <= line.startX + 5 || mouse.x >= line.startX - 5)
    {
        // mouse is within 5px of first x
    }
});

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

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