简体   繁体   English

在EaselJS中获取直线的起点和终点

[英]Get starting point and ending point of a line in EaselJS

Is it possible to retrieve these points from a drawn line? 是否可以从绘制的线中检索这些点? The API didn't really help me. API并没有真正帮助我。

var line = new createjs.Graphics();
line.beginStroke( 'black' );
line.moveTo( 0, 0 );
line.lineTo( 100, 100 );

Right now I'm creating an instance of the Point class and use the coordinates as parameters. 现在,我正在创建Point类的实例,并将坐标用作参数。

var point1 = new Point(0, 0);
var point2 = new Point(100, 100);

var line = new createjs.Graphics();
line.beginStroke( 'black' );
line.moveTo( point1.x, point1.y );
line.lineTo( point2.x, point2.y );

While it would be possible in theory, to retrieve those coordinates, you are far better of saving them as custom objects (Points for example, as you do already). 从理论上讲,虽然可以检索这些坐标,但最好将它们另存为自定义对象(例如,像Points一样)。


The Graphics -object saves those draw-instructions and coordinates in Commands ( Command(f, params, path) ) - and all active commands are saved in an array: line._activeInstructions Graphics将这些绘制指令和坐标保存在CommandsCommand(f, params, path) )-所有活动命令均保存在一个数组中: line._activeInstructions

You would have to go through the first and the last command and retrieve the command's params -array, those would be your points. 您将必须通过第一个和最后一个命令,并检索命令的params -array,这就是您的观点。 But this would only work in your case of a simple line. 但这仅适用于简单行的情况。 And since this uses internal variables and methods of the Graphics-object, I highly recommend not to do it this way. 由于这使用了Graphics对象的内部变量和方法,因此我强烈建议要这样做。 Also I'd recommend you to keep the readability of your code in this case instead of trying to save some memory by not using 2 Points with an pretty much unmeasureable memory-effect. 另外,我建议您在这种情况下保持代码的可读性,而不是尝试通过不使用2点而导致内存影响无法估量的方式来节省一些内存。

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

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