简体   繁体   English

如何通过拖动JSXGraph中的另一个元素来拖动滑翔机

[英]How to drag a glider by dragging another element in JSXGraph

I have a glider on a line that animates after you drag it. 拖动后,动画线上会出现一个滑翔机。 I have then created a box (using 4 points and polygon) next to the glider. 然后,我在滑翔机旁边创建了一个框(使用4个点和多边形)。

My problem is, I would like to have the user drag this box to drag the glider, and have the box move with the glider. 我的问题是,我希望用户拖动此框来拖动滑翔机,并使框与滑翔机一起移动。

Here is a link to the full code I'm currently working with, which helps visualize the problem https://jsfiddle.net/uobdpw0y/2/ 这是我目前正在使用的完整代码的链接,可帮助可视化问题https://jsfiddle.net/uobdpw0y/2/

The glider is the red point, and the line it glides on is blue. 滑行器是红色点,滑行线是蓝色。 As you can see, the box moves when dragging the red point, but won't move when dragging any other part of the box. 如您所见,拖动红点时该框会移动,但是拖动框的任何其他部分时该框将不会移动。

Here is the code relevant to the question: 这是与问题相关的代码:

  // create the mass at the moving end of the spring
  var point = board.create('glider', [5, 0, line], {withLabel: false, size: 1});

...

  // create moving box at end of spring
  var opts = {withLabel: false, size: 0, strokeColor:'green', fillColor:'green'};
  var boxPoints = [];
  boxPoints.push(board.create('point',[function(){return point.X()}, 0.5], opts));
  boxPoints.push(board.create('point',[function(){return point.X()}, -0.5], opts));
  boxPoints.push(board.create('point',[function(){return point.X()+2}, -0.5], opts));
  boxPoints.push(board.create('point',[function(){return point.X()+2}, 0.5], opts));

  board.create('polygon', boxPoints, {hasInnerPoints: true});
  board.create('group', boxPoints);

The desired result would be similar to the functionality seen in the Group documentation https://jsxgraph.org/docs/symbols/Group.html#781b5564-a671-4327-81c6-de915c8f924e Here the box they've created moves as you drag any part of it. 期望的结果将类似于在组文档https://jsxgraph.org/docs/symbols/Group.html#781b5564-a671-4327-81c6-de915c8f924e中看到的功能。它的任何部分。

The initial solution I tried was to include the glider in the Group of points used for the box, but only Point objects can be added, and not Glider objects. 我尝试的最初解决方案是将滑翔机包含在用于框的一组点中,但是只能添加“点”对象,而不能添加“滑翔机”对象。

This is a very nice construction. 这是一个非常好的结构。 My suggestion is the following: 我的建议如下:

var point = board.create('point', [5, 0], {withLabel: false, size: 1});

// ...

var boxPoints = [];
boxPoints.push(board.create('point',[point.X(), 0.5], opts));
boxPoints.push(board.create('point',[point.X(), -0.5], opts));
boxPoints.push(board.create('point',[point.X()+2, -0.5], opts));
boxPoints.push(board.create('point',[point.X()+2, 0.5], opts));

var pol = board.create('polygon', boxPoints, {hasInnerPoints: true});
board.create('group', boxPoints.concat([point]));
boxPoints[0].on('move', function(e) {
  this.moveTo([this.X(), 0.5], 0);
});
point.on('drag', function(e) {
  this.moveTo([this.X(), 0], 0);
});
pol.on('drag', function(e) {
  point.moveTo([point.X(), 0], 0);
  boxPoints[0].moveTo([point.X(), 0.5], 0);
});

That is, the glider is now a simple point and the vertical position of that point and the box is corrected in each move/drag event. 也就是说,滑翔机现在是一个简单的点,并且该点和框的垂直位置在每次移动/拖动事件中均得到纠正。 See it at https://jsfiddle.net/rqx48shb/3/ in action. 实际在https://jsfiddle.net/rqx48shb/3/上查看。

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

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