dat.gui/demo/demo.js

124 lines
3.1 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 03:14:15 +00:00
var message = message;
2011-01-26 01:36:47 +00:00
this.width = width;
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);
2011-01-26 02:30:05 +00:00
// document.getElementById('helvetica-demo').appendChild(r);
2011-01-26 01:36:47 +00:00
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 03:14:15 +00:00
var createParticles = 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 02:30:05 +00:00
// Set reference onto particles
2011-01-26 01:36:47 +00:00
for(var i = 0; i < pixels.length; i+=4) {
2011-01-26 02:30:05 +00:00
if(pixels[i] < 255) {
var p = getPosition(i);
2011-01-26 03:14:15 +00:00
var c = getPixel(p.x, p.y);
particles.push( new Particle(p.x, p.y, c) );
2011-01-26 02:30:05 +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 02:30:05 +00:00
for(var i = 0; i < particles.length; i++) {
2011-01-26 03:14:15 +00:00
particles[i].update();
2011-01-26 02:30:05 +00:00
var p = particles[i].position;
2011-01-26 03:14:15 +00:00
g.fillStyle = particles[i].color;
2011-01-26 02:30:05 +00:00
g.fillRect(p.x, p.y, 1, 1);
}
};
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
};
var getPixel = function(x, y) {
2011-01-26 03:14:15 +00:00
var base = (y * width + x) * 4;
return { r: pixels[base + 0], g: pixels[base + 1], b: pixels[base + 2], a: pixels[base + 3]};
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;
particles = [];
createParticles(message);
render();
});
createParticles(message);
setInterval(render, 30);
2011-01-26 02:30:05 +00:00
}
2011-01-26 03:14:15 +00:00
function Particle(x, y, c) {
2011-01-26 02:30:05 +00:00
var x = x;
var y = y;
2011-01-26 03:14:15 +00:00
this.color = "rgb("+c.r+", "+c.g+", "+c.b+")";
this.update = function() {
// x+=Math.random() - Math.random();
};
2011-01-26 02:30:05 +00:00
this.__defineGetter__("x", function() {
return x;
});
this.__defineGetter__("y", function() {
return y;
});
this.__defineGetter__("position", function() {
return { x: x, y: y };
});
this.__defineSetter__("x", function(n) {
x = n;
});
this.__defineSetter__("y", function(n) {
y = n;
});
// Dependent on Vector format
this.__defineSetter__("position", function(p) {
if(p.x && p.y) {
x = p.x;
y = p.y;
}
});
2011-01-26 01:36:47 +00:00
}