0

I'm trying to use the javascript port of processing found at http://ejohn.org/blog/processingjs/ I want to use the following constructor. Processing(CanvasElement, "some massive block of code"); I know javascript doesn't natively support multiline strings but is there a way to pass something like the following with out having to concatenate every line and escape every special character?

/**
 * Array. 
 * 
 * An array is a list of data. Each piece of data in an array 
 * is identified by an index number representing its position in 
 * the array. Arrays are zero based, which means that the first 
 * element in the array is [0], the second element is [1], and so on. 
 * In this example, an array named "coswav" is created and
 * filled with the cosine values. This data is displayed three 
 * separate ways on the screen.  
 */

size(200, 200);

float[] coswave = new float[width];

for (int i = 0; i < width; i++) {
  float amount = map(i, 0, width, 0, PI);
  coswave[i] = abs(cos(amount));
}

for (int i = 0; i < width; i++) {
  stroke(coswave[i]*255);
  line(i, 0, i, height/3);
}

for (int i = 0; i < width; i++) {
  stroke(coswave[i]*255 / 4);
  line(i, height/3, i, height/3*2);
}

for (int i = 0; i < width; i++) {
  stroke(255 - coswave[i]*255);
  line(i, height/3*2, i, height);
}

5 Answers 5

3

Javascript actually does support multi-line strings: append a backslash to the end of each line:

alert('1\
    2\
    3');

Not very pretty, but it works.

An alternative would be to use a script to encode your text... I'd suggest PHP as it's a 1-liner:

<?=json_encode('your text');?>
Sign up to request clarification or add additional context in comments.

1 Comment

+1. And I thought I knew every nuance of Javascript syntax. Nice answer.
1

For a more maintainable solution, I would place it in script tags tags in the body of the page, eg:

<script type="text/processing" id="code">
/**
 * Array. 
 * ...
 */

size(200, 200);
...
</script>

And construct your Processing object like:

var canvas = document.getElementById('canvas');
var code = document.getElementById('code');
Processing(canvas , code.textContent);

For a quick and dirty solution run your processing code through a JSON encoder as RoBorg suggested and you'll get a string that you can just copy and paste into the second parameter.

Comments

0

Place the "massive block of code" in its own file on the server, fetch with AJAX and ensure it has reasonable cache headers.

1 Comment

This is a quick and dirty solution to a problem so don't want to go the ajax route if I can avoid it. See this question. stackoverflow.com/questions/460085/…
0

Alternatively, you can just put the text in the HTML page (hidden, for example) and get it from there...

Comments

0

Another option would be to use E4X literals

var s = "" + <r><![CDATA[
line 1
line 2
]]></r>;

Although I doubt it is supported by IE6.

1 Comment

It's not even supported by IE7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.