Note that there are some explanatory texts on larger screens.

plurals
  1. POasync sort for kineticjs line points?
    primarykey
    data
    text
    <p>The problem I've been fighting for the last 2 days is a bit strange, so I was not really sure how specify it in the title.</p> <p>I have a KineticJs Line, which has an array of points (line.attrs.points) in the form of objects with x and y values. While dragging a certain Kinetic element, I want to dynamically add points to the line and re-draw it. (Example: the line looks like that: \ _ _ _ / and under certain conditions while dragging another shape I want to dynamically add a point and make it look like that: \ _ / \ _ /) So, when that condition is met in the dragmove event of the other shape, I use the following code:</p> <pre><code>line.attrs.points.push({x:xValue, y:yValue}); line.attrs.points.sort(function(a,b){ return a.x &gt; b.x; // I tried with a.x - b.x and with short if-s which return only -1, 0 or 1, still nothing happened }); line.setPoints(line.attrs.points); layer.draw(); </code></pre> <p>but the points are not sorted so the line goes to some x, then returns to a less x and then again goes forward which is not the behavior I expected after a sort of the points!</p> <p>The strange thing is that when I use console.log(line.attrs.points); for three consecutive point additions (+2 points in each addition) I get the following output:</p> <pre><code>[Object, Object, Object, Object, Object, Object] [Object, Object, Object, Object, Object, Object, Object, Object] [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object] </code></pre> <p>but when I open the arrays, there are 10 objects in each array. How is it even possible to open an array of 6 objects and see 10 objects inside (I'm using Google Chrome's default console)? Can it be that the console.log is asynchronous and logs them after all the additions have taken place?</p> <p>The main problem is that the sort of the points isn't working, but still I'll be glad to understand why the console acts so strange.</p> <p>Thanks in advance and sorry for the long explanations!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload