dat.gui/demo/demo.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-01-26 03:14:15 +00:00
function DynamicText(message, width, height, textAscent) {
2011-01-26 01:36:47 +00:00
2011-01-26 21:33:54 +00:00
var _this = this;
this.growthSpeed = 0.1;
this.minSize = 0;
this.maxSize = 5;
2011-01-26 03:14:15 +00:00
var message = message;
2011-01-26 01:36:47 +00:00
2011-01-26 21:33:54 +00:00
this.width = width;
2011-01-26 01:36:47 +00:00
this.height = height;
this.textAscent = textAscent;
var r = document.createElement('canvas');
var s = r.getContext('2d');
var c = document.createElement('canvas');
var g = c.getContext('2d');
2011-01-26 02:30:05 +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);
document.getElementById('helvetica-demo').appendChild(c);
2011-01-26 02:30:05 +00:00
var pixels = [];
var particles = [];
2011-01-26 01:36:47 +00:00
2011-01-26 02:30:05 +00:00
s.font = "800 "+textAscent+"px helvetica, arial, sans-serif";
2011-01-26 21:33:54 +00:00
// 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-26 01:36:47 +00:00
2011-01-26 03:14:15 +00:00
s.fillStyle = "#222";
s.fillText(m, 0, 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-26 03:14:15 +00:00
2011-01-26 01:36:47 +00:00
};
2011-01-26 03:14:15 +00:00
var render = function() {
g.clearRect(0, 0, width, height);
2011-01-26 21:33:54 +00:00
for (var i = 0; i < particles.length; i++) {
2011-01-26 03:14:15 +00:00
particles[i].update();
2011-01-26 21:33:54 +00:00
particles[i].draw();
2011-01-26 02:30:05 +00:00
}
};
var getPosition = function(i) {
2011-01-26 03:14:15 +00:00
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-26 21:33:54 +00:00
var getColor = function(x, y) {
var base = (Math.floor(y) * width + Math.floor(x)) * 4;
var c = { r: pixels[base + 0], g: pixels[base + 1], b: pixels[base + 2], a: pixels[base + 3]};
return "rgb("+c.r+","+c.g+","+c.b+")";
2011-01-26 02:30:05 +00:00
};
2011-01-26 03:14:15 +00:00
this.__defineGetter__("message", function() {
return message;
});
this.__defineSetter__("message", function(m) {
message = m;
2011-01-26 21:33:54 +00:00
createBitmap(message);
2011-01-26 03:14:15 +00:00
});
2011-01-26 21:33:54 +00:00
createBitmap(message);
2011-01-26 03:14:15 +00:00
setInterval(render, 30);
2011-01-26 02:30:05 +00:00
2011-01-26 21:33:54 +00:00
function Particle(x, y, c) {
this.x = x;
this.y = y;
this.r = 0;
this.update = function() {
this.x += Math.random() - Math.random();
this.y += Math.random() - Math.random();
}
this.draw = function() {
var c = getColor(this.x, this.y);
if (c == "rgb(255,255,255)") {
this.r -= _this.growthSpeed;
} else {
this.r += _this.growthSpeed;
}
this.r = constrain(this.r, _this.minSize, _this.maxSize);
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;
}
}