master
mian 2 years ago
commit ff23d95d0c
  1. 66
      README.txt
  2. 12268
      addons/p5.sound.js
  3. 3
      addons/p5.sound.min.js
  4. 24
      diag_spring/index.html
  5. 33
      diag_spring/sketch.js
  6. 24
      empty-example/index.html
  7. 7
      empty-example/sketch.js
  8. 24
      fractal_antenna/index.html
  9. 38
      fractal_antenna/sketch.js
  10. 111165
      p5.js
  11. 3
      p5.min.js
  12. 25
      particles/index.html
  13. 84
      particles/sketch.js
  14. 37
      pendulum/ball.js
  15. 25
      pendulum/index.html
  16. 17
      pendulum/sketch.js
  17. 24
      sandpile/index.html
  18. 94
      sandpile/sketch.js
  19. 24
      sunflower-seeds/index.html
  20. 29
      sunflower-seeds/sketch.js
  21. 25
      tentacle/index.html
  22. 43
      tentacle/segment.js
  23. 34
      tentacle/sketch.js

@ -0,0 +1,66 @@
# Welcome to p5.js
You have downloaded the complete p5.js library ZIP file, yay!
# Contents of the p5 folder
* p5.js file
* p5.min.js file
* addons folder
* p5.sound.js
* p5.sound.min.js
* empty-example folder
* index.html
* p5.js
* p5.sound.js
* sketch.js
## p5.js
This file stores the complete p5.js library. It is easy to read by humans, so feel free to open it and explore its contents. It also has a friendly error system, which helps new programmers with common user errors.
## p5.min.js
This file is a minified version of the p5.js file. It is a lighter version, with the same functionalities, but smaller file size. This minified version is harder to read for humans, and does not include the friendly error system.
## addons folder
The addons folder includes additional p5.js related libraries, in both original versions and minified versions.
### p5.sound.js, p5.sound.min.js
p5.sound extends p5.js with Web Audio functionality including audio input, playback, analysis, and synthesis.
## empty-example folder
This is an empty example of a website. The folder includes the file for the website, index.html, the p5.js library, other related p5.js libraries, and a template starting point for your p5.js sketch, called sketch.js.
### index.html
index.html is a template for an HTML file. This index.html first imports the libraries included in the folder (p5.js, p5.sound.js) then loads and executes the file sketch.js which is where you can write your own code.
### sketch.js
The sketch.js is a template for the p5.js sketch, with the functions setup() and draw() that you can complete.
## README.txt
This README file formatted with Markdown :)
# What's next?
If you need more information to help get you started, please refer to our website:
https://p5js.org/get-started/ and https://p5js.org/learn/
An online reference to the p5.js library is available here:
https://p5js.org/reference/
In order to run your website (including the empty-example), you need to enable a local server, please see this tutorial in our wiki:
https://github.com/processing/p5.js/wiki/Local-server
p5.js is a community and p5.js is built by contributions. If you want to learn more about us, visit:
https://p5js.org/community/
# License
The p5.js library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,33 @@
ball = {
x: 100,
y: 100,
dx: -10,
dy: 10,
r : 80,
};
ball.update = function(){
this.y += this.dy;
this.x += ball.dx;
}
function setup() {
// put setup code here
createCanvas(400, 400);
frameRate(30);
}
function draw() {
// update code here
ball.dy -= (ball.y - height/2)/20;
ball.dx -= (ball.x - width/2)/20;
ball.update();
// put drawing code here
background(220);
stroke(0,0,255);
strokeWeight(4);
line(width/2, height/2, ball.x, ball.y);
noStroke();
ellipse(ball.x,ball.y,ball.r);
}

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,7 @@
function setup() {
// put setup code here
}
function draw() {
// put drawing code here
}

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,38 @@
param_array = [];
ratio = Math.sqrt(2);
function setup() {
// put setup code here
createCanvas(512,512)
param_array.push({
startx: width/4,
starty: height/2,
endx: width/2,
endy: height/2,
});
}
function draw() {
// put drawing code here
if (param_array.length > 0){
param = param_array.pop();
line(param.startx, param.starty, param.endx, param.endy);
xlen = (param.endx - param.startx)/ratio;
ylen = (param.endy - param.starty)/ratio;
if ( Math.max( Math.abs(xlen), Math.abs(ylen)) > 1 ){
param_array.splice(0,0,{
startx: param.endx,
starty: param.endy,
endx: param.endx-ylen,
endy: param.endy+xlen,
});
param_array.splice(0,0,{
startx: param.endx,
starty: param.endy,
endx: param.endx+ylen,
endy: param.endy-xlen,
});
}
}
else noLoop();
}

111165
p5.js

File diff suppressed because one or more lines are too long

3
p5.min.js vendored

File diff suppressed because one or more lines are too long

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 30px;
margin: 20px;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,84 @@
const Vect = p5.Vector
const width = 256;
const height = 256;
const nParticles = 15;
const initialSpeed = 2;
const particleRadius = 10;
const gap = 5;
const wallRadius = width/2-gap-particleRadius;
const sq = x => x*x;
function Particle(position, speed, color){
this.pos = position;
this.speed = speed;
this.color = color;
this.collidePart = function(other){
otherPos = other.pos
partPos = this.pos
dif = Vect.sub(partPos, otherPos);
if( dif.magSq() > 0
&& dif.magSq() < 4*sq(particleRadius)){
force = Vect.mult(
dif, (2*particleRadius - dif.mag())/dif.mag())
this.pos.add(Vect.div(force,2));
other.pos.sub(Vect.div(force,2));
let aux = this.speed;
this.speed = other.speed;
other.speed = aux;
}
}
}
let particles = [];
function setup() {
createCanvas(width,height);
colorMode(HSB);
noStroke();
frameRate(30);
for(let i = 0; i < nParticles; i += step) {
for(let overlap = true; overlap;){
pos = new Vect(
random(-width/2, width/2),
random(-height/2, height/2)
);
if(pos.mag() >= wallRadius)
continue;
overlap = particles.find(p => Vect.sub(p.pos, pos).mag() < particleRadius*2)
}
direction = new Vect(
random(-width/2, width/2),
random(-height/2, height/2)
);
direction.normalize();
speed = Vect.mult(direction, initialSpeed);
aParticle = new Particle(pos, speed, 360*i/nParticles);
particles.push(aParticle);
step = 1;
}
}
function draw() {
// update code
particles.forEach(p => p.pos.add(p.speed));
particles.forEach(function(particle,i){
if(particle.pos.mag() > wallRadius){
particle.pos.normalize()
particle.pos.mult(wallRadius)
particle.speed.reflect(particle.pos.copy())
}
particles.slice(0,i).forEach(p => particle.collidePart(p))
})
// drawing code
background(0);
fill(255);
translate(height/2, width/2);
circle(0,0,width-gap);
particles.forEach(function(particle){
fill(particle.color,255,255);
circle(particle.pos.x, particle.pos.y, 2*particleRadius);
})
}

@ -0,0 +1,37 @@
function BallSwing(fulcrumx, fulcrumy, ballx, bally, r){
x = ballx - fulcrumx;
y = bally - fulcrumy;
this.poleLen = Math.sqrt(x*x + y*y);
this.fx = fulcrumx;
this.fy = fulcrumy;
this.bx = ballx;
this.by = bally;
this.dx = 0;
this.dy = 0;
this.update = function(){
x = this.bx - this.fx;
y = this.by - this.fy;
realLen = Math.sqrt(x*x + y*y);
nx = x/realLen;
ny = y/realLen;
this.dy += 1;
s = this.dx * nx + this.dy * ny;
this.dx -= s*nx;
this.dy -= s*ny;
dlen = this.poleLen - realLen;
this.dx += dlen * nx;
this.dy += dlen * ny;
this.bx += this.dx;
this.by += this.dy;
};
this.draw = function(){
line(this.fx, this.fy, this.bx, this.by)
circle(this.bx, this.by, r)
};
}

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="ball.js"></script>
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,17 @@
ball = new BallSwing(150, 0, 300, 150, 10)
function setup() {
// put setup code here
createCanvas(300,300);
//frameRate(60);
}
function draw() {
// put drawing code here
background(32);
fill(128);
stroke(128);
strokeWeight(2);
ball.draw();
ball.update();
}

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,94 @@
const height = 512;
const width = 512;
const gridWidth = 64;
const gridHeight = 64;
const tileWidth = width/gridWidth;
const tileHeight = height/gridHeight;
let colorMap =
[[0,0,0],
[0,0,255],
[0,255,0],
[255,0,0],
[0,255,255],
[255,0,255],
[255,255,0],
[255,255,255]];
let sandpiles = Array(gridHeight);
let oldpiles = new Array(sandpiles.length);
function setup() {
createCanvas(width, height);
for(let i = 0; i < gridHeight; i++){
sandpiles[i] = Array(gridWidth);
for(let j = 0; j < gridWidth; j++)
sandpiles[i][j] = 0;
}
sandpiles[gridHeight/2][gridWidth/2] = 10000;
//sandpiles[gridHeight/2][gridWidth/2+1] = 4;
for(let i = 0; i < gridHeight; i++)
oldpiles[i] = new Array(sandpiles[i].legnth);
// for(let k = 0; k< 10000; k++)
// update_sandpiles_in();
noStroke();
frameRate(5);
}
function update_sandpiles() {
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++)
oldpiles[i][j] = sandpiles[i][j];
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++){
if (oldpiles[i][j] > 4){
sandpiles[i][j] -= 4;
if(i > 0)
sandpiles[i-1][j]++;
if(j > 0)
sandpiles[i][j-1]++;
if(i < gridHeight-1)
sandpiles[i+1][j]++;
if(j < gridWidth-1)
sandpiles[i][j+1]++;
}
}
}
function update_sandpiles_in() {
for(let k = 0; k < 2; k++)
for(let i = 0; i < gridHeight; i++){
for(let j = (i+k)%2; j < gridWidth; j+= 2){
//sandpiles[i][j] = 3+k;
if (sandpiles[i][j] > 4){
sandpiles[i][j] -= 4;
if(i > 0)
sandpiles[i-1][j]++;
if(j > 0)
sandpiles[i][j-1]++;
if(i < gridHeight-1)
sandpiles[i+1][j]++;
if(j < gridWidth-1)
sandpiles[i][j+1]++;
}
}
}
}
function draw() {
update_sandpiles_in();
// put drawing code her
background(0);
for(let i = 0; i < gridHeight; i++)
for(let j = 0; j < gridWidth; j++){
let color = colorMap[constrain(sandpiles[i][j],0,7)];
fill(color[0],color[1], color[2]);
rect( j*tileWidth, i*tileHeight,tileWidth,tileWidth);
}
//noLoop();
}

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,29 @@
const deg = Math.PI/180;
const ma = (1+Math.sqrt(5))/2;
const c = 2;
let n = 1;
function setup() {
createCanvas(360,360);
noStroke();
frameRate(32);
colorMode(HSB);
}
function draw() {
background(64);
translate(width/2, height/2);
scale(width/5/sqrt(n), height/5/sqrt(n));
for (i = 0; i<=n; i++){
let a = i*ma;
let r = c * sqrt(i);
let x = r * cos(a);
let y = r * sin(a);
fill(floor(i/4), 255, 255);
circle(x, y, 2);
}
if(n >= 1024)
noLoop();
n++;
}

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js example</title>
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="../p5.js"></script>
<!-- <script src="../addons/p5.sound.js"></script> -->
<script src="segment.js"></script>
<script src="sketch.js"></script>
</head>
<body>
<main>
</main>
</body>
</html>

@ -0,0 +1,43 @@
const Vector = p5.Vector;
function Segment(startx, starty, len, heading){
this.start = new Vector(startx, starty);
this.len = len;
this.reference = 0;
this.heading = heading;
this.child = null;
};
Segment.prototype.end = function(){
angle = this.heading + this.reference;
let x = this.len * sin(angle) + this.start.x;
let y = this.len * cos(angle) + this.start.y;
return new Vector(x,y);
};
Segment.prototype.update = function(){
if(this.child){
this.child.start = this.end();
this.child.reference = this.heading+this.reference;
}
};
Segment.prototype.setHeading = function(angle){
this.heading = angle;
this.update();
};
Segment.prototype.draw = function(){
push();
stroke(255);
strokeWeight(2);
line(
this.start.x,
this.start.y,
this.end().x,
this.end().y);
pop();
};

@ -0,0 +1,34 @@
const firstLen = 8;
const nSegments = 16;
let firstSegment = null;
let frame = 0;
function setup() {
createCanvas(300, 300);
firstSegment = new Segment(0, 0, firstLen, 0);
let len = firstLen;
for(let i=1, seg = firstSegment; i < nSegments; i++, seg=seg.child){
let next = new Segment(
seg.end().x,
seg.end().y,
len,
0);
seg.child = next;
}
}
function draw() {
background(0);
frame = (frame+1);
for(let i=0, seg = firstSegment; seg; i++, seg=seg.child){
let delay = i*PI/nSegments+PI/4;
let coef = 3*PI/nSegments*sin(PI/256*frame-delay);
seg.setHeading(coef);
}
translate(width/2, height/2);
for(let i=0; i < 5; i++){
rotate(PI/2);
for(let seg = firstSegment; seg; seg=seg.child)
seg.draw();
}
}
Loading…
Cancel
Save