P5.js inside sBasic

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

Thank, Operator, for this code to load a single HTML file. Up until now I have just commented out loading the HTML1 file, and that worked.

George, I found that once I switched to WHILE statements to detect end of file, I got a few more scripts to run that weren't working, because they weren't loading fully before. There is also a slight improvement to be made in the line
BROWSER b$ AT -1,-1 SIZE SW+1,SH+1 ' otherwise a white border can be seen!
FIrst,change this line in HTML1 from
padding:1 px
to
padding: 0 px;
Then the browser line can be changed to
BROWSER b$ AT 0,0 SIZE SW,SH
There is one script from the P5.js site Examples that still won't run. I would appreciate it if you both would try it out and see if it works for you. It is a very convincing example of how birds flock, which you can see in action on their site. This is the original script. I also have it with the comments corrected, and a version with all comments removed. None of them will run on my iPad. :cry:

Code: Select all

var flock;
var text;
function setup() {
  createCanvas(640,360);
  createP("Drag the mouse to generate new boids.");
  
  flock = new Flock();
  // Add an initial set of boids into the system
  for (var i = 0; i < 100; i++) {
    var b = new Boid(width/2,height/2);
    flock.addBoid(b);
  }
}
function draw() {
  background(51);
  flock.run();
}
// Add a new boid into the System
function mouseDragged() {
  flock.addBoid(new Boid(mouseX,mouseY));
}
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Flock object
// Does very little, simply manages the array of all the boids
function Flock() {
  // An array for all the boids
  this.boids = []; // Initialize the array
}
Flock.prototype.run = function() {
  for (var i = 0; i < this.boids.length; i++) {
    this.boids[i].run(this.boids);  // Passing the entire list of boids to each boid individually
  }
}
Flock.prototype.addBoid = function(b) {
  this.boids.push(b);
}
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Boid class
// Methods for Separation, Cohesion, Alignment added
function Boid(x,y) {
  this.acceleration = createVector(0,0);
  this.velocity = createVector(random(-1,1),random(-1,1));
  this.position = createVector(x,y);
  this.r = 3.0;
  this.maxspeed = 3;    // Maximum speed
  this.maxforce = 0.05; // Maximum steering force
}
Boid.prototype.run = function(boids) {
  this.flock(boids);
  this.update();
  this.borders();
  this.render();
}
Boid.prototype.applyForce = function(force) {
  // We could add mass here if we want A = F / M
  this.acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
Boid.prototype.flock = function(boids) {
  var sep = this.separate(boids);   // Separation
  var ali = this.align(boids);      // Alignment
  var coh = this.cohesion(boids);   // Cohesion
  // Arbitrarily weight these forces
  sep.mult(1.5);
  ali.mult(1.0);
  coh.mult(1.0);
  // Add the force vectors to acceleration
  this.applyForce(sep);
  this.applyForce(ali);
  this.applyForce(coh);
}
// Method to update location
Boid.prototype.update = function() {
  // Update velocity
  this.velocity.add(this.acceleration);
  // Limit speed
  this.velocity.limit(this.maxspeed);
  this.position.add(this.velocity);
  // Reset accelertion to 0 each cycle
  this.acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
Boid.prototype.seek = function(target) {
  var desired = p5.Vector.sub(target,this.position);  // A vector pointing from the location to the target
  // Normalize desired and scale to maximum speed
  desired.normalize();
  desired.mult(this.maxspeed);
  // Steering = Desired minus Velocity
  var steer = p5.Vector.sub(desired,this.velocity);
  steer.limit(this.maxforce);  // Limit to maximum steering force
  return steer;
}
Boid.prototype.render = function() {
  // Draw a triangle rotated in the direction of velocity
  var theta = this.velocity.heading() + radians(90);
  fill(127);
  stroke(200);
  push();
  translate(this.position.x,this.position.y);
  rotate(theta);
  beginShape();
  vertex(0, -this.r*2);
  vertex(-this.r, this.r*2);
  vertex(this.r, this.r*2);
  endShape(CLOSE);
  pop();
}
// Wraparound
Boid.prototype.borders = function() {
  if (this.position.x < -this.r)  this.position.x = width +this.r;
  if (this.position.y < -this.r)  this.position.y = height+this.r;
  if (this.position.x > width +this.r) this.position.x = -this.r;
  if (this.position.y > height+this.r) this.position.y = -this.r;
}
// Separation
// Method checks for nearby boids and steers away
Boid.prototype.separate = function(boids) {
  var desiredseparation = 25.0;
  var steer = createVector(0,0);
  var count = 0;
  // For every boid in the system, check if it's too close
  for (var i = 0; i < boids.length; i++) {
    var d = p5.Vector.dist(this.position,boids[i].position);
    // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
    if ((d > 0) && (d < desiredseparation)) {
      // Calculate vector pointing away from neighbor
      var diff = p5.Vector.sub(this.position,boids[i].position);
      diff.normalize();
      diff.div(d);        // Weight by distance
      steer.add(diff);
      count++;            // Keep track of how many
    }
  }
  // Average -- divide by how many
  if (count > 0) {
    steer.div(count);
  }
  // As long as the vector is greater than 0
  if (steer.mag() > 0) {
    // Implement Reynolds: Steering = Desired - Velocity
    steer.normalize();
    steer.mult(this.maxspeed);
    steer.sub(this.velocity);
    steer.limit(this.maxforce);
  }
  return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
Boid.prototype.align = function(boids) {
  var neighbordist = 50;
  var sum = createVector(0,0);
  var count = 0;
  for (var i = 0; i < boids.length; i++) {
    var d = p5.Vector.dist(this.position,boids[i].position);
    if ((d > 0) && (d < neighbordist)) {
      sum.add(boids[i].velocity);
      count++;
    }
  }
  if (count > 0) {
    sum.div(count);
    sum.normalize();
    sum.mult(this.maxspeed);
    var steer = p5.Vector.sub(sum,this.velocity);
    steer.limit(this.maxforce);
    return steer;
  } else {
    return createVector(0,0);
  }
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
Boid.prototype.cohesion = function(boids) {
  var neighbordist = 50;
  var sum = createVector(0,0);   // Start with empty vector to accumulate all locations
  var count = 0;
  for (var i = 0; i < boids.length; i++) {
    var d = p5.Vector.dist(this.position,boids[i].position);
    if ((d > 0) && (d < neighbordist)) {
      sum.add(boids[i].position); // Add location
      count++;
    }
  }
  if (count > 0) {
    sum.div(count);
    return this.seek(sum);  // Steer towards the location
  } else {
    return createVector(0,0);
  }
}
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: P5.js inside sBasic

Post by Operator »

:? :? :?
No luck here either..., screen stays "white"
unchanged... Checked the code in Javascript
Anywhere and in Coffee Script at Once and it
runs...
Attachments
image.jpg
image.jpg (54.68 KiB) Viewed 6275 times

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

I installed both of these apps, but can't seem to get the script to run in either one. Not exactly sure of the procedure. I pressed +, named the project Flocking, selected JS and pasted the code into the window. When I press RUN it says "Hello, Flocking"! :D
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: P5.js inside sBasic

Post by Operator »

For a test in Javascript Anywhere...
Start a New Project
Clear the content of all three sections:
JS HTML CSS

Paste the code below in the HTML section.
Paste the P5 test code in the JS section.
Adapt the canvasSize to your device..
(make sure to have an Internet connection
since an external P5lib will be loaded)
And it should work...

Code: Select all

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width" />
  <title>test</title>
  <link rel="stylesheet" href="style.css" />
</head>
<body>
  <div id="testt"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js">
</script>

<script src="script.js">
</script>


</body>
</html>

Attachments
image.jpg
image.jpg (53.03 KiB) Viewed 6267 times

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

Thanks, Operator. I have tried this solution on my iPad Air with JS Anywhere multiple times, just to make sure I have pasted the HTML and JS code exactly as described, but no luck! I also tried duplicating the setup in CS At Once, and got the same reaction, or lack of same. :?

The script doesn't run - just a blank window. It won't run in the P5.js app either.

I notice that you show an iPhone screen capture in your post. I can't see why this would make a difference, but I will try this solution on my iPhone later today.
Attachments
IMG_8459.PNG
IMG_8459.PNG (329.78 KiB) Viewed 6261 times
IMG_8458.PNG
IMG_8458.PNG (508.06 KiB) Viewed 6261 times
IMG_8457.PNG
IMG_8457.PNG (627.69 KiB) Viewed 6261 times
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: P5.js inside sBasic

Post by Operator »

I forgot to mention: :?
Comment the following line in the P5 code : 5th line
/* createP("Drag the mouse to generate new boids."); */

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

I just brought up the JS Anywhere console window, and it shows this error (see image).
Attachments
IMG_8460.PNG
IMG_8460.PNG (66.2 KiB) Viewed 6256 times
The only thing that gets me down is gravity...

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

Thanks - that must be what the error is referring to. :idea:

The comment example you give shows /* code */

I will also try // since it seems JS Anywhere accepts those.
The only thing that gets me down is gravity...

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: P5.js inside sBasic

Post by rbytes »

Yes, that fixed it. Thanks!

It is running in all of the apps now. Speed is quite slow. Is that the same on your device?
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: P5.js inside sBasic

Post by Operator »

:D Good to know it works now...
Yes also slow...I just reduced to 10 boids and then
it runs faster....

Post Reply