javascript

Javascript problem. Probably newb error but I just don't get it!

usefuldesign.au's picture

I'm writing a simple JS routine to filter a structure of points into those points that are within a prescribed distance of a particular point, which is described by it's index value in the point structure.

So it's not the first time JS has refused to pull nested values even when I'm using code I've used successfully elsewhere ie. [i] ["X"]. And it's not for the first time it refuses to parse a line like:

 total_points = all_points.length;

even though the exact same line works in another patch parsing the same structure data. (so I've used the structure count patch and pull it in ;) )

But this really gets me, as a way of debugging I'm logging some point co-ords (X,Y,Z) as __numbers out of the patch.

If I literally assign them as in result.X = 0.123 no worries but if I use the incredibly complicated code of

var x1 = new Number();
x1 = 2 + 3;
result.X = x1;

I get told Argument X: Incorrect type, should be a number ( :\ x100 )

If anybody can explain this to me I'd be enormously grateful. Shutting down in frustration.

Cheers Alastair

copy Nested Structures / Objects in Javascript

M.Oostrik's picture

Hi

I'm trying to copy a nested structure in the Javascript patch so i can manipulate it. The function i came up with does work, but it messes up the order of the structure.

function cloneObject(oldObject) {
 
   var newObject = new Object()
 
     for (i in oldObject) {
      if (typeof oldObject[i] == 'object') {
         newObject[i] = new cloneObject(oldObject[i]);
      }
      else newObject[i] = oldObject[i];
    }
    return newObject
}

Anyone has a suggestion? i'm kinda at the end of my javascript knowledge here. thanx

Best way to strip line breaks

jeremydouglass's picture

I'm writing a series of patches that take text input from either the keyboard or through a file loader, and I've been trying Kineme Freeboard and Kineme String With File.

My problem: I'm trying to strip line feeds / enters / carriage returns from text strings, whether loaded from a file or aggregated through keystrokes. However, they seem to be encoded differently by Freeboard and String With File. I have come up with a temporary solution, but I'd love suggestions for something more robust.

In the attached patch, you can either load a file (Multiplex 0) or type (Mutiplex 1). Either way, the string gets passed through two "String Replace":

  1. Once using the invisible "ctr+opt+enter" character that can be entered into QC via keyboard (and matches against enter keys passed by Kineme FreeBoard) -- not sure, maybe this is '\r'?
  2. Once using a '\n' passed from JavaScript (that matches against line breaks passed by Kineme String With File)

This works (the lines are successfully stripped), however I suspect that there is some easier / more graceful / more efficient way of doing this. Possibly with a single JavaScript command that uses either str.replace or RegExp?

Any advice appreciated.

http://kineme.net/Discussion/DevelopingCompositions/CarriageReturns http://cybero.co.uk/QuartzComposerJavaScriptGuide-Eng/objects/String/rep...

JavaScript Lazier-Than-Expected Execution

toneburst's picture

Another probably very stupid question:

Is there a reason the JavaScript patch only ever seems to get executed once, when the composition is first started? Tweaking the input values only has an effect if rendering is stopped and restarted.

I've been away from QC for ages, so apologies if this is a(nother) stooopid question.

a|x

JavaScript Number Weirdness

toneburst's picture

This is probably not a QC-specific thing, but I've noticed some slightly odd behaviour with a simple JavaScript patch that's supposed to generate an array of XY values.

The script basically just creates a 10x10 grid of XY-coordinate pairs using a nested loop. Simple stuff, and all it's ever doing is multiplying 0.1 by an integer.

The odd thing is, as you can see from the attached screenshot, not all the values produced are quite as expected.

As I said, probably not a QC issue per-se, but I'd be interested to know what's causing it.

Incidentally, I've tried using the JS toFixed(1) function to truncate the numbers to a single decimal place, but it looks as if the extra teeny-tiny amount is being added after the structure appears at the JavaScript patch's output.

a|x http://machinesdontcare.wordpress.com