简体   繁体   English

Javascript画布使用算法绘制线的奇怪行为

[英]Javascript canvas draw line strange behavior using algorithm

There are plenty of examples on how to draw lines on canvas, in js. 在js中,有很多关于如何在画布上绘制线条的示例。

But for only educational purposes i want to draw line using algorithm. 但是 ,仅出于教育目的,我想使用算法画线。 basically method gets two Vector2 points, from them it finds middle point, then it continues like that recursively until minimum distance of 2 pixels is reached. 基本上,方法是获取两个Vector2点,从中找到中间点,然后以这种方式递归继续,直到达到2个像素的最小距离。

I have DrawPoint method to basically draw 1 point on canvas, and DrawLine method that does all the job. 我有DrawPoint方法基本上可以在画布上绘制1个点,而DrawLine方法可以完成所有工作。

For now I have 2 problems: 现在我有两个问题:

1: points are not colored red, as they should be. 1:点未按应有的方式标记为红色。

2: 2:

线

It doesnt look like a line. 它看起来不像一条线。

For Vector2 i used "Victor.js" plugin, and it seems to be working well. 对于Vector2,我使用了“ Victor.js”插件,它似乎运行良好。

this is code i have: 这是我有的代码:

JS: JS:

var point2 = new Victor(100, 100);
var point3 = new Victor(150, 150);

DrawLine(point2, point3);

function DrawLine(vec0, vec1)
{
    var point0 = new Victor(vec0.x, vec0.y);
    var point1 = new Victor(vec1.x, vec1.y);

var dist = point1.distance(point0);

if (dist < 2)
    return;


//this is how it should look like in c# var middlePoint = point0 + (point1 - point0)/2; But looks like i cant just divide by 2 using victor js because i can only divide vector by vector. 
    var middlePoint = point0.add(point1.subtract(point0).divide(new Victor(2,2)));


DrawPoint(middlePoint);

DrawLine(point0, middlePoint);
DrawLine(middlePoint, point1);
}

function DrawPoint(point){
    var c = document.getElementById("screen");
    var ctx = c.getContext("2d");
    ctx.fillStyle = "FF0000";
    ctx.fillRect(point.x, point.y, 3,1); 
}

I really appreciate any help you can provide. 我非常感谢您可以提供的任何帮助。

The victor.js documentation shows that most functions of Victors do not return new Victors, but operate on the current instance. victor.js文档显示, Victor的大多数功能不会返回新的Victor,而是在当前实例上运行。 In a way, v1.add(v2) is semantically more like v1 += v2 and not v1 + v2 . 从某种意义上讲, v1.add(v2)从语义上更类似于v1 += v2而不是v1 + v2

The problem is with calculating the midpoint. 问题在于计算中点。 You could use the mix() method, which blends two vectors with a weight. 您可以使用mix()方法,该方法将两个向量与权重混合。 You must clone() the Victor first, otherwise point0 will be midofied: 您必须先clone() Victor,否则point0将成为midofied:

var middlePoint = point0.clone().mix(point1, 0.5);

If you don't change the original Vectors, you don't need to create new instances of Victors from the arguments, you can use the arguments directly: 如果不更改原始向量,则不需要根据参数创建Victor的新实例,可以直接使用参数:

function DrawLine(point0, point1)
{
    var dist = point1.distance(point0);

    if (dist < 2) return;

    var middlePoint = point0.clone().mix(point1, 0.5);

    DrawPoint(middlePoint);

    DrawLine(point0, middlePoint);
    DrawLine(middlePoint, point1);
}

Finally, as Sven the Surfer has already said in a comment, "FF0000" isn't a valid colour. 最后,正如冲浪者Sven在评论中所说, "FF0000"不是有效的颜色。 Use "#FF0000" , note the hash mark, or one of the named web colours such as "crimson" . 使用"#FF0000" ,记"#FF0000"号,或使用诸如"crimson"类的已命名Web颜色之一。

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

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