Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Help] Drawing a polyline over a tokens lastmove property

I'm looking for a script that, when called, will draw a polyline along the path of the token's last movement.  (Think TRON lightcycles, your token is the cycle and the script adds the polyline after the token moves(via !api not change:graphic))  What's screwing me up on my own attempt at it is that creating a path uses points relative to the bounding box, whereas the lastmove property uses screen coordinates.  Top,Left of the bounding box is a virtual point that's rarely on the path itself so it has to be derived.  My skill level means I can probably get it working given a few weeks, but I'm hoping the pros here can help me reduce that time (and amount of hair-pulling).   Know of anything like this I can use or look over?
1501933456
The Aaron
Pro
API Scripter
I can probably help you with that later today, but if you want to get a head start on it, this is what you needs to do: for the points on the move find the smallest X and smallest Y (not necessarily from the same point). These become the left and top values.  Create a new list of points by subtracting the minimum X and minimum Y from the X and Y of each point. This translates the move points into the line's bounding box relative coordinate system.  Create the path with the left and top from step 1 and the points from step 2. 
1501981487

Edited 1501981667
Muse
Pro
I understand the logic needed to derive the numbers, just don't have the coding background to apply that logic in js very quickly :) I have an array built from obj.get("lastmove").split(","); Then I run it through a loop to turn each point into an integer because it will be on a hex grid where tokens snap to non-integer values.  Probably not necessary buts its easier on the eyes to debug outputs that are 35,35,... rather than 34.45235907,35.01349078,...... This loop also finds the lowest x and y values for the Top and Left of the path object later.   Then I run it through a loop to build the string for the path like so: while(j < Steps) {   var coords;   var a;   var b;   if (j == 0) {     a = Path[j] - TopMin;      b = Path[j+1] - LeftMin;     coords = '[\"M\", ' + a + ', ' + b + ']';   } else {     a = Path[j] - TopMin;     b = Path[j+1] - LeftMin;     coords += ',[\"L\", ' + a + ', ' + b + ']';   }   j+=2; } var coordinates = JSON.stringify([coords]); However coords is outputting as "[\"M\", 0, 0],[\"L\", 70, 140],[\"L\", 210, 140],[\"L\", 210, 70]"  (whether I escape the " with a \ or not in the while loop) so of course JSON.stringify is giving me "[\"[\\\"M\\\", 210, 70],[\\\"L\\\", 140, 70],[\\\"L\\\", 0, 70],[\\\"L\\\", 0, 0]\"]" which is meaningless to the createObj laster on.
1501987519
Lithl
Pro
Sheet Author
API Scripter
Don't make coords a string that you stringify. The purpose of JSON.stringify is to turn a JavaScript object or primitive into a valid JSON string representative of that object/primitive. Which means if you call JSON.stringify([stringValue]), the JSON string you create will be representative of an array with a single string element. Instead of var coords; coords = '[\"M\", ' + a + ', ' + b + ']'; coords += ',["L\", ' + a + ', ' + b + ']'; , try var coords = []; coords.push(['M', a, b]); coords.push(['L', a, b]);  JSON.stringify will do the rest.
1501993391

Edited 1501993487
Muse
Pro
Output still looks like: "[[[\"M\",0,210],[\"L\",420,140],[\"L\",420,0]]]" and its creating a blank path.
1501995530

Edited 1501996048
Muse
Pro
Okay so if you leave out top, left, width, and height in the createObj call and use actual screen x and y coordinates in the path object it works them out internally.  It works perfectly now. Thanks Aaron and Brian!
1502023058
The Aaron
Pro
API Scripter
Cheers!  Happy Rolling!