Brock J. Stearn
Task 08
Programming for Artists
Rain
The code
/*
Programming for Artists - Assignment #8
Brock J. Stearn
"Rain"
*/
// Wave formula:
// wavelength = amplitude * periodicFunction(frequency * time + phaseShift)
// (From Pete) define a function to perform updates to the stage
function circleAnimationstep(whichCircle, bounds) {
// (From Pete) bounds[0] = left edge of cell
// (From Pete) bounds[1] = top edge of cell
// (From Pete) bounds[2] = right edge of cell
// (From Pete) bounds[3] = bottom edge of cell
var period = 7*1000;
var milliseconds = (new Date()).getTime();
var radians = (2*Math.PI)*((milliseconds%period)/period);
var norm = whichCircle/circles.length;
var frequency = 2;
var phaseShift = norm*(2*Math.PI);
var ranOpac = Math.random()*100;
var rany = Math.random()*10;
// Use implemented Square and Saw functions to create Rain look
if (rany <= 2) {
// Saw gives a wave like effect
circles[whichCircle]._x = rangedSaw(radians, frequency, phaseShift, bounds[0], bounds[2]);
}
else {
// Square creates more straight drops
circles[whichCircle]._x = rangedSquare(radians, frequency, phaseShift, bounds[0], bounds[2]);
}
circles[whichCircle]._y = rangedSaw(radians, frequency, phaseShift, bounds[3], bounds[1]);
// Add some alpha to create a misty fog like effect
circles[whichCircle]._alpha = ranOpac;
if (circles[whichCircle]._y < 350) {
// Make raindrops ... x of 50 and y of 100 (no scale)
circles[whichCircle]._xscale = 50;
circles[whichCircle]._yscale = 100;
} else {
// Make Splash ... x of 200 and y of 50
// Lower y value made the blue disappear ... looked too much like shadows
circles[whichCircle]._xscale = 200;
circles[whichCircle]._yscale = 50;
// Make puddles lighter
circles[whichCircle]._alpha = ranOpac*.5;
}
}
//
//----------------------------------------------------------------------------------
// constants:
var numCols = 5;
var numRows = 5;
var col = Stage.width/numCols;
var row = Stage.height/numRows;
// Array of all circle objects (movies)
var circles = new Array(circle01, circle02, circle03, circle04, circle05,
circle06, circle07, circle08, circle09, circle10,
circle11, circle12, circle13, circle14, circle15,
circle16, circle17, circle18, circle19, circle20,
circle21, circle22, circle23, circle24, circle25);
// (from Pete)
function updateFrame() {
var curRow = 0;
var curCol = 0;
for (var j = 0; j < circles.length; j++) {
var lo_x = (col*curCol)+(.5*circles[j]._width);
var hi_x = (col*(curCol+1))-(.5*circles[j]._width);
var lo_y = (row*curRow)+(.5*circles[j]._height);
var hi_y = (row*(curRow+1))-(.5*circles[j]._height);
//
curCol++;
if (curCol == numCols) {
curRow++;
curCol = 0;
}
//
circleAnimationstep(j, new Array(lo_x, hi_y, hi_x, lo_y));
}
}
// (from Pete) - Range Sine function
function rangedSin(t, f, p, lo, hi) {
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*Math.sin(f*t+p));
}
// (from Pete) - Range Cosine function
function rangedCos(t, f, p, lo, hi) {
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*Math.cos(f*t+p));
}
// ------ My Helpers ------
// Sawtooth
function sawtoothWave(value) {
// t should be a fractional number > 0
// period is 2
// output is [-1,1]
return ((value%1.0)*2)-1.0;
}
// SquareWave
function squareWave(value) {
// t should be a fractional number > 0
// period is 2
// output is [-1,1]
var v = -1;
if (value%1.0>=.5) {
v = 1;
}
return v;
}
// Range the Saw Function from above
function rangedSaw(t, f, p, lo, hi) {
// t = time (varying value, should range [0,2PI])
// f = frequency (waves per time, >= 1.0)
// p = phase shift (affects timing of wave, should range [0,2PI])
// lo = lower bound of arbitrary range
// hi = upper bound of arbitrary range
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*sawtoothWave(f*t+p));
}
// Range the Square Function from above
function rangedSquare(t, f, p, lo, hi) {
// t = time (varying value, should range [0,2PI])
// f = frequency (waves per time, >= 1.0)
// p = phase shift (affects timing of wave, should range [0,2PI])
// lo = lower bound of arbitrary range
// hi = upper bound of arbitrary range
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*squareWave(f*t+p));
}
//
//
// associate a function with the enterFrame event
this.onEnterFrame = updateFrame;