Having more success now, thanks to the hint from Operator not to use // as a comment marker. You have to enclose all comments in the P5 code with /* */ Otherwise nothing appears in the browser! Many of the demo scripts have // comments, and I had to change them all.
I got this script with the P5.js app available on the App Store. It is called Array of Objects. Icopied it to the smart BASIC P5 folder and did some tweaking. It draws a bunch of circles and makes them dance randomly - exactly the way gas molecules behave. I changed colors and increased the number of "molecules".
You'll notice in the code that I draw the same text multiple times. I wanted to outline the text, but I found that what works to outline text in Function Setup does not work if the text statements are located in Function Draw. In setup I can outline text in any thickness by preceding it with a strokeWeight(x) command that is very similar to DRAW SIZE in smart Basic.
But no matter where I locate the strokeWeight command, it affects only the circles. I moved the text to the Draw function because to get the text to appear in front of the circles it has to be drawn after them! So I drew the text in black first at multiple offset locations to create an outline and shadow effect, then drew the colored text on top.
You will need to save this file as a code file. I named it P5_code_brownian.tx
Code: Select all
var bugs = []; /*array of Jitter objects*/
function setup() {
createCanvas(1024, 768); /* Change this to fit your screen size */
/*Create objects*/
for (var i=0; i<400; i++) {
bugs.push(new Jitter());
}
strokeWeight(2);
}
function draw() {
background(50, 89, 100);
fill(0,255,255);
for (var i=0; i<bugs.length; i++) {
bugs[i].move();
bugs[i].display();
}
textSize(64);
fill(0);
text("Brownian motion",32,98);
text("Brownian motion",28,102);
text("Brownian motion",30,98);
text("Brownian motion",30,102);
text("Brownian motion",28,98);
text("Brownian motion",32,103);
text("Brownian motion",34,104);
fill(255,255,0);
text("Brownian motion",30,100);
}
/* Jitter class */
function Jitter() {
this.x = random(width);
this.y = random(height);
this.diameter = random(10, 30);
this.speed = 5;
this.move = function() {
this.x += random(-this.speed, this.speed);
this.y += random(-this.speed, this.speed);
};
this.display = function() {
ellipse(this.x, this.y, this.diameter, this.diameter);
};
}
Next is the code from the copy I made of the Test file. I am getting so many script samples, it is hard to keep track of them using number identication. So now each copy I make of the original Test file is named for what it creates on screen, and each code file is named to match it. Hence the name Brownian or brownian appears in both filenames.
This test file is set up to create a 1024 x 768 browser (full screen), and the code file specifies a Canvas of that same size. If running the script on a device other than iPad, the file below will automatically fit the browser to your screen size. But with the code script, you will need to edit the canvas size to match your device. I have commented in the script where you need to change it.
I named it P5 -Brownian.tx
Code: Select all
REM P5.js inside sBasic
REM P5 version: 0.5.4
REM visit http://p5js.org/ for details
REM
REM sB5.6/iOS6.1/iPhone4/by Operator
REM (thxs to rBytes)
/*
To use html/javascript set:
!! "set editor capsyntax off" !!
to avoid syntax errors with reserved words of sBasic
P5-code in a file (with js-lib in a second file) if changed after first run of program wont change the result --> 'BUG' ?
A change in the P5-code will only be adopted by sBasic's browser only by cpl. closing and restarting sBasic.
Deleting browser doesn't help...
Maybe due to of how sBasic handles loading files with javascript..(?)..
If the P5-code is included in the html-file WITH the P5-lib (js) then the changes in the P5 code will lead
to a change in the result (canvas-content) --> 'NO BUG' (w/o cpl. closing and reloading sBasic).
But editing the html file with the
P5-lib = pain in the ass, since the
sB editor almost freezes (guess file is too big for sB-editor).
So for a quick and dirty solution
the html file including the P5 code and the P5-js-lib is splitted into 3 files, being the second file which has the P5-code. The main programm just loads the content of the 3 files to a single string which is then passed to the browser...
Open P5_html_2.tx to edit the P5 code
close it and run this file...
*/
GRAPHICS
GRAPHICS CLEAR 1,1,1
SET TOOLBAR OFF
set buttons custom
GET SCREEN SIZE SW,SH
'html-file was splitted in three files
'second file has the P5-code to be
'executed..., edit it to code...
fname1$ = "P5_html_1.tx"
fname2$ = "P5_code_brownian.tx"
fname3$ = "P5_html_3.tx"
a$ = ""
temp$ = ""
b$ = "1"
'read all 3 files into one string
FILE fname1$ SETPOS 0
FOR t=1 TO 20
FILE fname1$ READLINE temp$
a$ &= temp$&""
NEXT t
'change amount of loops for bigger
'P5 code line size...
FILE fname2$ SETPOS 0
FOR t=1 TO 50
FILE fname2$ READLINE temp$
a$ &= temp$&""
NEXT t
FILE fname3$ SETPOS 0
FOR t=1 TO 200
FILE fname3$ READLINE temp$
a$ &= temp$&""
NEXT t
BROWSER b$ AT 0,0 SIZE SW,SH
browser b$ hide
BROWSER b$ TEXT a$
BROWSER b$ SHOW
DRAW COLOR 0,0,0
button "quit" text "Q" at 990,10 size 24,30
LOOP:
if button_pressed("quit") then
browser 1 delete
a$ = ""
temp$ = ""
text
end
end if
GOTO LOOP
The only thing that gets me down is gravity...