- Tendrils Test
- Animated Tendrils Test
var chart = null;
var timer = null;
function Tendril(x, y, width) {
this.x = x;
this.y = y;
this.width = width ? width : 15;
this.angle = Math.random() * 2 * Math.PI - Math.PI;
this.segments = [];
this.v = 0;
}
Tendril.prototype = { };
Tendril.prototype.grow = function(distance, curl, step) {
if (!distance) distance = 3;
if (!curl) curl = 1;
if (!step) step = .02;
this.x += Math.cos(this.angle) * distance;
this.y += Math.sin(this.angle) * distance;
this.v += Math.random() * step * 2 - step;
this.v *= 0.9 + curl * 0.1;
this.angle += this.v;
this.segments.push([this.x, this.y]);
}
Tendril.prototype.draw = function() {
var n = this.segments.length;
var r = null;
var segment = null;
var circle = Frozen.Chart.Constants.fullcircle;
for (var i = 0; i < n; i++) {
segment = this.segments[i];
r = (1 - i / n) * this.width;
chart.map.beginPath();
chart.map.arc(segment[0], segment[1], r, 0, circle, false);
chart.map.stroke();
}
}
function Plant(x, y, tendrils, width) {
this.x = x;
this.y = y;
this.tendrils = [];
if (!tendrils) tendrils = 30;
if (!width) width = 15;
for (var i = 0; i < tendrils; i++)
this.tendrils[i] = new Tendril(x, y, width);
}
Plant.prototype = { };
Plant.prototype.grow = function(distance, curl, step) {
if (!distance) distance = 3;
if (!curl) curl = 1;
if (!step) step = .02;
for (var i = 0; i < this.tendrils.length; i++)
this.tendrils[i].grow.call(this.tendrils[i], distance, curl, step);
}
Plant.prototype.draw = function() {
for (var i = 0; i < this.tendrils.length; i++)
this.tendrils[i].draw.call(this.tendrils[i]);
}
function drawTendrils() {
if (timer) clearTimeout(timer);
var num = parseInt(document.getElementById("tendrils").value) || 13;
var plant = new Plant(375, 275, num);
chart.clear();
for (var i = 0; i < 200; i++) {
plant.grow.call(plant, null, 1, 0.02);
}
plant.draw.call(plant);
}
function animateTendrils() {
if (timer) clearTimeout(timer);
var num = parseInt(document.getElementById("tendrils").value) || 13;
var plant = new Plant(375, 275, num);
var i = 0;
staggerLoop(
function() { return i++ < 200; },
function() {
chart.clear();
plant.grow.call(plant, null, 1, 0.02);
plant.draw.call(plant);
}
);
}
function staggerLoop(cond, func) {
if (cond()) func();
timer = setTimeout(function() { staggerLoop(cond, func); }, 5);
}