Particle Hydrodynamic Simulator

by Kyle Bezio

HTML

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>

JavaScript

//Current Version

//http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_13.txt
//http://www.petercollingridge.co.uk/sites/files/peter/PyParticles3_0.txt
//access the canvas
var canvas = document.getElementById('myCanvas');
//access drawing environment
var context = canvas.getContext("2d");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;

var canvasWM = canvas.width / 500; //Canvas width multiplier
var canvasHM = canvas.height / 500; //Canvas height multiplier

var particles = [];
var mouseX;
var mouseY;
var clicking = false;
var time = 0;

init();

//Game Variables
var maxParticles = 1000;
var gravity = 0;
var drawSpeed = 30;
var speed = drawSpeed / 30;
var flow = 4;
var border = "on"; //"on" -- present boundries //"off" -- will leave screen //"return" -- returns to mouse //"loop" -- loops to opposite side of screen //"kill" -- deletes objects off screen **To Be Tested**


function init() {
		thread = setInterval(draw, drawSpeed);
}

//rounded rectangle function
function roundedRect(x, y, width, height, radius) {
  context.beginPath();
  context.moveTo(x, y+radius);
  context.lineTo(x, y+height-radius);
  context.arcTo(x, y+height, x+radius, y+height, radius);
  context.lineTo(x+width-radius, y+height);
  context.arcTo(x+width, y+height, x+width, y+height-radius, radius);
  context.lineTo(x+width, y+radius);
  context.arcTo(x+width, y, x+width-radius, y, radius);
 	context.lineTo(x+radius, y);
  context.arcTo(x, y, x, y+radius, radius);
  context.fill();
}

function rectHitTest(x, y, obx, oby, w, h, obw, obh) {
	if (x <= obx + obw && x + w >= obx && y <= oby + obh && h + y >= oby) {
  	return true;
  }
}

/**
Define particle Objects here
**/
function particle(x, y, size, color){
	//assign the values
  this.x = x;
  this.y = y;
  this.size = size;
  this.color = color;
	this.vX = Math.random() - .5;
  this.vY = Math.random() - .5;
  this.elasticity = .5;
  this.friction = .999;
 ...