Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
  FLUID HEATING

  Simple particle-fluid heating model.
//-->

<script type="text/processing" data-processing-target="canvas">
// model params
  /** @var maximal speed of particles - numerical corretion */ 
  float maxV = 2.5;
  
  /** @var number of particles in the model */
  int   particlesQuantity = 180;
  
  /** @var speed coeficient in collision */
  float spring = 1;
  
  /** @var gravity force, pushing particelsdown to the heating side */
  float gravity = 0.05;

  /** @var temperature of the system */
  float t = 0.5;
  
  
// UI: model and model panel  
  int modelWidth  = 600;
  int modelHeight = 468;
    
  int panelHeight = 100;
  
  int canvasWidth  = modelWidth;
  int canvasHeight = modelHeight + panelHeight;


// init objects
  Particle[] Particles = new Particle[particlesQuantity];
  Panel control = new Panel();

  
// setup UI
  void setup() {  
	  size(canvasWidth, canvasHeight);
	  frameRate(50);
	
	  noStroke();
	  smooth();

	  for (int i = 0; i < particlesQuantity; i++) {        
		  Particles[i] = new Particle(random(modelWidth), random(modelHeight), 40, i, Particles);
	  }
  }

// draw model and model panel
  void draw() {
	  background(0);

	  for (int i = 0; i < particlesQuantity; i++) {
		  Particles[i].collide();
		  Particles[i].move();
		  Particles[i].display();
		  Particles[i].structure(-5);
	  }
  
    control.display();
  }


// model panel
  class Panel {
    /** draw panel */
	  void display() {
		  fill(255, 255, 255, 200);
		  rect(0, modelHeight, modelWidth, panelHeight);
  
      // control info 
      PFont fontInfo = loadFont("Arial");   
	    textFont(fontInfo, 11);   
	    fill(0);
  
      text("temperature: "+(int)round(t * 10), 15, 490);
      text("number of particles: "+particlesQuantity, 15, 505);
  
      text("You can change temperature by UP/DOWN arrows. Works only on focused canvas.", 15, 535);
      text("By mouse click, you can create new particle in the model.", 15, 550);	
	  }
  }
  
  
// objects
  class Particle {
	  float x, y;
	  float diameter;
	  float vx = 0;
	  float vy = 0;
	  int id;
	  Particle[] others;

    /** constructor */
	  Particle(float xin, float yin, float din, int idin, Particle[] oin) {
		  x = xin;
		  y = yin;
		  diameter = din;
		  id = idin;
		  others = oin;
	  }

    /** show clusters in the model */
	  void structure(float dz) {
		  for (int i = id + 1; i < particlesQuantity; i++) {
			  float dx = others[i].x - x;
			  float dy = others[i].y - y;

			  float distance = sqrt(dx*dx + dy*dy);
			  float minDist = others[i].diameter/2 + diameter/2;

			  if (distance < (minDist + dz)) {
				  float v = 100 * min(maxV, sqrt(vx*vx + vy*vy));
				  stroke(v, 255 - v, 0, 100);
			
				  line(others[i].x, others[i].y, x, y);
			  }
		  }   
		
		  noStroke();
	  }

    /** collision solution */
	  void collide() {
		  for (int i = id + 1; i < particlesQuantity; i++) {
			  float dx = others[i].x - x;
			  float dy = others[i].y - y;

			  float distance = sqrt(dx*dx + dy*dy);
			  float minDist = others[i].diameter/2 + diameter/2;

			  if (distance < minDist) {
				  float angle = atan2(dy, dx);
  				float targetX = x + cos(angle) * minDist;
	  			float targetY = y + sin(angle) * minDist;
		  		float ax = (targetX - others[i].x) * spring;
			  	float ay = (targetY - others[i].y) * spring;

  				vx -= ax;
	  			vy -= ay;
			  	others[i].vx += ax;
		  		others[i].vy += ay;

		  		vx *= 0.9;
			  	vy *= 0.9;
			  }
		  }
	  }

    /** move solution */
	  void move() {
		  vy += gravity;
		  x += vx * t;
		  y += vy * t;

		  if (x + diameter/2 > modelWidth) {
			  x = modelWidth - diameter/2;
			  vx *= -0.8;
		  }
		  else if (x - diameter/2 < 0) {
			  x = diameter/2;
			  vx *= -0.8;
		  }
		
      if (y + diameter/2 > modelHeight) {
			  y = modelHeight - diameter/2;
			  vy = -25*t;
		  }
		  else if (y - diameter/2 < 0) {
			  y = diameter/2;
			  vy *= -0.8;
		  }
	  }

    /** draw particle */
	  void display() {
		  noFill();
		  float v = 100*min(maxV, sqrt(vx*vx + vy*vy));

		  fill(v, 255 - v, 0);
		  ellipse(x, y, 5, 5);
	  }
  }

  
// user controls
  /** change temperature by UP/DOWN arrows */
  void keyPressed() {
	  if (key == CODED) {
		  if (keyCode == UP) {
			  if (t < 0.95) {
				  t += 0.1;
			  }
		  } 
		  else if (keyCode == DOWN) {
			  if (t >= 0.01) {
				  t -= 0.1;
			  }
		  }
	  }
  }

  /** add particle by mouse click */
  void mouseClicked() {
    if (mouseY <= modelHeight) {
	    particlesQuantity++;
      Particles[particlesQuantity - 1] = new Particle(mouseX, mouseY, 40, particlesQuantity - 1, Particles);
    }
  }
</script>

<canvas id="canvas"></canvas>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console