Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created April 30, 2012 12:51
Show Gist options
  • Save shinaisan/2558063 to your computer and use it in GitHub Desktop.
Save shinaisan/2558063 to your computer and use it in GitHub Desktop.
Comparison of random and noise in processing.js
<html>
<head>
<title>Comparison of random and noise in processing.js</title>
<script type="text/javascript" src="processing.js"></script>
</head>
<body>
<h1>random (upper) and noise (lower)</h1>
<script type="application/processing" src="test.processing"></script><canvas></canvas>
<br/>
Useless:)
</body>
</html>
/* -*- mode: java; indent-tabs-mode: nil -*- */
color plotColor = #c0c0c0;
color frameColor = #7cfba0;
Drawable[] drawables;
LinearSlider[] sliders;
PlotArea randomPlotArea;
PlotArea noisePlotArea;
LinearSlider randomSeedSlider;
LinearSlider noiseScaleSlider;
IntLabel randomSeedLabel;
IntLabel noiseScaleLabel;
void setup() {
size(500, 700);
frameRate(16);
randomSeedLabel = new IntLabel(40, 20, 400, 40, "Random Seed");
randomSeedSlider = new LinearSlider(40, 80, 400, 40);
randomPlotArea = new RandomPlot(40, 140, 400, 200, 4);
noisePlotArea = new NoisePlot(40, 360, 400, 200, 4);
noiseScaleLabel = new IntLabel(40, 580, 400, 40, "Noise Scale (x 100)");
noiseScaleSlider = new LinearSlider(40, 640, 400, 40);
randomSeedSlider.addListener(randomSeedLabel);
randomSeedSlider.addListener(randomPlotArea);
randomSeedSlider.setRange(0, 20);
noiseScaleSlider.addListener(noiseScaleLabel);
noiseScaleSlider.addListener(noisePlotArea);
noiseScaleSlider.setRange(1, 101);
drawables = {randomPlotArea,
noisePlotArea,
randomSeedSlider,
randomSeedLabel,
noiseScaleSlider,
noiseScaleLabel};
sliders = {randomSeedSlider,
noiseScaleSlider};
}
void draw() {
background(0);
for (int i = 0; i < sliders.length; i++) {
sliders[i].calc();
}
for (int i = 0; i < drawables.length; i++) {
drawables[i].draw();
}
}
void mousePressed() {
for (int i = 0; i < sliders.length; i++) {
sliders[i].mousePressed();
}
}
void mouseReleased() {
for (int i = 0; i < sliders.length; i++) {
sliders[i].mouseReleased();
}
}
interface Drawable {
void draw();
}
class PlotArea implements Drawable {
int left;
int top;
int width;
int height;
int pixelsPerGrid;
PlotArea(int x, int y, int w, int h, int p) {
this.left = x;
this.top = y;
this.width = w;
this.height = h;
this.pixelsPerGrid = p;
}
void draw() {
int lastX = -1;
int lastY = -1;
strokeWeight(3);
drawFrame();
stroke(plotColor);
for (int i = 0; i < floor(this.width / this.pixelsPerGrid); i++) {
int v = calc(i);
int x = this.left + i * pixelsPerGrid;
int y = this.top + this.height - v * pixelsPerGrid;
if (lastX >= 0) {
line(lastX, lastY, x, y);
}
lastX = x;
lastY = y;
}
}
void drawFrame() {
noFill();
stroke(frameColor);
rect(left, top, width, height);
}
int calc(int x) {
return (x);
}
}
class RandomPlot extends PlotArea implements IntListener {
int seed;
RandomPlot(int x, int y, int w, int h, int p) {
super(x, y, w, h, p);
this.seed = 0;
}
void draw() {
randomSeed(this.seed);
super.draw();
}
int calc(int x) {
int range = floor(this.height / pixelsPerGrid);
return (random(range));
}
void notifyInt(int v) {
this.seed = v;
}
}
class NoisePlot extends PlotArea implements IntListener {
float noiseScale;
NoisePlot(int x, int y, int w, int h, int p) {
super(x, y, w, h, p);
this.noiseScale = 0.01;
}
int calc(int x) {
return (noise(this.noiseScale * x) * floor(this.height / this.pixelsPerGrid));
}
void notifyInt(int v) {
this.noiseScale = (float)v / 100.0;
}
}
interface IntListener {
void notifyInt(int value);
}
class LinearSlider implements Drawable {
int left;
int top;
int length;
int thickness;
int minValue;
int maxValue;
int value;
int pixelsPerUnit;
boolean changing;
ArrayList listeners;
LinearSlider(int x, int y, int w, int h) {
this.left = x;
this.top = y;
this.length = w;
this.thickness = h;
this.pixelsPerUnit = 1;
this.changing = false;
this.listeners = new ArrayList();
setRange(0, 2);
}
void setRange(int minValue, int maxValue) {
int diff = maxValue - minValue;
if (diff == 0) {
maxValue = minValue + 1;
} else if (diff < 0) {
int tmp = minValue;
minValue = maxValue;
maxValue = tmp;
}
this.minValue = minValue;
this.maxValue = maxValue;
this.pixelsPerUnit = floor(this.length / (this.maxValue - this.minValue));
setValue(minValue);
}
void setValue(int v) {
if (v < minValue) v = minValue;
if (v > maxValue) v = maxValue;
this.value = v;
for (int i = 0; i < this.listeners.size(); i++) {
(IntListener)(listeners.get(i)).notifyInt(this.value);
}
}
void addListener(IntListener l) {
this.listeners.add(l);
}
boolean mouseOver() {
int x = mouseX;
int y = mouseY;
int tolerance = 40;
int left = this.left - tolerance;
int top = this.top;
int right = this.left + this.length + tolerance;
int bottom = this.top + this.thickness;
if (left < 0) left = 0;
if (right > width) right = width;
return ((x >= left) && (y >= top) && (x < right) && (y < bottom));
}
void mousePressed() {
this.changing = this.mouseOver();
}
void mouseReleased() {
this.changing = false;
}
void calc() {
if (!mouseOver()) {
this.changing = false;
}
int x = mouseX;
int p = this.pixelsPerUnit;
if (this.changing) {
setValue(this.minValue + floor((x - this.left + (p / 4)) / p));
}
}
void draw() {
strokeWeight(3);
stroke(frameColor);
noFill();
rect(this.left, this.top, this.length, this.thickness);
int fillWidth = (this.value - this.minValue) * this.pixelsPerUnit;
fill(frameColor);
rect(this.left, this.top, fillWidth, this.thickness);
}
}
class IntLabel implements Drawable, IntListener {
int left;
int top;
int width;
int height;
String caption;
int value;
IntLabel(int x, int y, int w, int h, String cap) {
this.left = x;
this.top = y;
this.width = w;
this.height = h;
this.caption = cap;
this.value = 0;
}
void draw() {
String str = this.caption + " = " + this.value;
textFont(loadFont(PFont.list()[0], this.height / 2));
textAlign(LEFT, BOTTOM);
text(str, this.left, this.top + this.height);
}
void notifyInt(int v) {
this.value = v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment