dat.gui/demo/demo.js

169 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-01-27 02:45:34 +00:00
function FizzyText(message) {
2011-01-26 21:33:54 +00:00
var _this = this;
2011-01-27 02:45:34 +00:00
var width = 550;
var height = 200;
var textAscent = 82;
var textLeft = 80;
2011-01-26 23:25:40 +00:00
this.growthSpeed = 0.5;
2011-01-26 21:33:54 +00:00
this.minSize = 0;
2011-01-27 02:45:34 +00:00
this.maxSize = 3.2;
2011-01-26 23:25:40 +00:00
this.noiseScale = 300;
this.noiseStrength = 10;
2011-01-27 02:45:34 +00:00
this.speed = 0.4;
2011-01-26 23:25:40 +00:00
this.displayOutline = false;
2011-01-27 02:45:34 +00:00
2011-01-26 01:36:47 +00:00
this.textAscent = textAscent;
2011-01-27 02:45:34 +00:00
var colors = ["#00aeff", "#0fa954", "#54396e", "#e61d5f"];
2011-01-26 01:36:47 +00:00
var r = document.createElement('canvas');
var s = r.getContext('2d');
2011-01-27 02:45:34 +00:00
2011-01-26 01:36:47 +00:00
var c = document.createElement('canvas');
var g = c.getContext('2d');
2011-01-27 02:45:34 +00:00
r.setAttribute('width', width);
c.setAttribute('width', width);
2011-01-26 01:36:47 +00:00
r.setAttribute('height', height);
c.setAttribute('height', height);
2011-01-27 02:45:34 +00:00
2011-01-26 01:36:47 +00:00
document.getElementById('helvetica-demo').appendChild(c);
2011-01-27 02:45:34 +00:00
var pixels = [];
2011-01-26 02:30:05 +00:00
var particles = [];
2011-01-27 02:45:34 +00:00
s.font = g.font = "800 " + textAscent + "px helvetica, arial, sans-serif";
// Set reference onto particles
for (var i = 0; i < 1000; i++) {
particles.push(new Particle(Math.random() * width, Math.random() * height));
}
var createBitmap = function (m) {
2011-01-26 01:36:47 +00:00
s.fillStyle = "#fff";
2011-01-26 03:14:15 +00:00
s.fillRect(0, 0, width, height);
2011-01-27 02:45:34 +00:00
2011-01-26 03:14:15 +00:00
s.fillStyle = "#222";
2011-01-27 02:45:34 +00:00
s.fillText(m, textLeft, textAscent);
2011-01-26 01:36:47 +00:00
// Pull reference
2011-01-26 02:30:05 +00:00
var imageData = s.getImageData(0, 0, width, height);
pixels = imageData.data;
2011-01-27 02:45:34 +00:00
2011-01-26 01:36:47 +00:00
};
2011-01-27 02:45:34 +00:00
var render = function () {
2011-01-26 03:14:15 +00:00
g.clearRect(0, 0, width, height);
2011-01-27 02:45:34 +00:00
if (_this.displayOutline) {
2011-01-26 23:25:40 +00:00
g.globalCompositeOperation = "source-over";
g.strokeStyle = "#000";
2011-01-27 02:45:34 +00:00
g.lineWidth = .5;
g.strokeText(message, textLeft, textAscent);
2011-01-26 23:25:40 +00:00
}
2011-01-27 02:45:34 +00:00
2011-01-26 23:25:40 +00:00
g.globalCompositeOperation = "darker";
2011-01-27 02:45:34 +00:00
2011-01-26 21:33:54 +00:00
for (var i = 0; i < particles.length; i++) {
2011-01-27 02:45:34 +00:00
g.fillStyle = colors[i % colors.length];
2011-01-26 23:25:40 +00:00
particles[i].render();
2011-01-26 02:30:05 +00:00
}
2011-01-26 23:25:40 +00:00
2011-01-26 02:30:05 +00:00
};
2011-01-27 02:45:34 +00:00
var getPosition = function (i) {
return {
x: (i - (width * 4) * Math.floor(i / (width * 4))) / 4,
y: Math.floor(i / (width * 4))
};
2011-01-26 01:36:47 +00:00
};
2011-01-27 02:45:34 +00:00
var getColor = function (x, y) {
2011-01-26 21:33:54 +00:00
var base = (Math.floor(y) * width + Math.floor(x)) * 4;
2011-01-27 02:45:34 +00:00
var c = {
r: pixels[base + 0],
g: pixels[base + 1],
b: pixels[base + 2],
a: pixels[base + 3]
};
2011-01-26 21:33:54 +00:00
2011-01-27 02:45:34 +00:00
return "rgb(" + c.r + "," + c.g + "," + c.b + ")";
2011-01-26 02:30:05 +00:00
};
2011-01-27 02:45:34 +00:00
this.__defineGetter__("message", function () {
2011-01-26 03:14:15 +00:00
return message;
});
2011-01-27 02:45:34 +00:00
this.__defineSetter__("message", function (m) {
2011-01-26 03:14:15 +00:00
message = m;
2011-01-26 21:33:54 +00:00
createBitmap(message);
2011-01-26 03:14:15 +00:00
});
2011-01-27 02:45:34 +00:00
this.explode = function() {
var mag = Math.random()*30+30;
for (var i in particles) {
var angle= Math.random()*Math.PI*2;
particles[i].vx = Math.cos(angle)*mag;
particles[i].vy = Math.sin(angle)*mag;
2011-01-26 21:33:54 +00:00
}
2011-01-27 02:45:34 +00:00
};
2011-01-26 21:33:54 +00:00
2011-01-27 02:45:34 +00:00
this.message = message;
2011-01-26 21:33:54 +00:00
2011-01-27 02:45:34 +00:00
setInterval(render, 30);
function Particle(x, y, c) {
this.x = x;
this.y = y;
2011-01-26 21:33:54 +00:00
2011-01-27 02:45:34 +00:00
this.vx = 0;
this.vy = 0;
this.r = 0;
this.render = function () {
var c = getColor(this.x, this.y);
var angle = noise(this.x / _this.noiseScale, this.y / _this.noiseScale) * _this.noiseStrength;
var onScreen = this.x > 0 && this.x < width &&
this.y > 0 && this.y < height;
var isBlack = c != "rgb(255,255,255)" &&
onScreen;
if (isBlack) {
this.r += _this.growthSpeed;
} else {
this.r -= _this.growthSpeed;
}
this.vx *= 0.5;
this.vy *= 0.5;
this.x += Math.cos(angle) * _this.speed + this.vx;
this.y += -Math.sin(angle) * _this.speed + this.vy;
this.r = constrain(this.r, _this.minSize, _this.maxSize);
if (this.r <= _this.minSize) {
this.x = Math.random() * width;
this.y = Math.random() * height;
}
if (this.r <= 0) {
return;
}
g.beginPath();
g.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
g.fill();
}
}
var constrain = function (v, o1, o2) {
if (v < o1) v = o1;
else if (v > o2) v = o2;
return v;
}
}