布雷特·维克多的"发明原理"视频(http://vimeo.com/36579366)让我深受启发.
此外,我对使用Javascript绘制的树非常着迷.我没有做太多的图形编程.在我的职业生涯中,我一直是中层和数据库开发人员.但是以编程方式绘制树,我很有动力去学习.我已经开始学习Javascript了.我知道我最终会(在几周或几个月内,取决于我得到多少时间)能够自己编写这样的程序.
但是,我真的非常渴望获得一些在Javascript中进行类似绘制并使用它的源代码.你们可以提供的任何链接/指针都非常有用.
bri*_*ris 10
用画布绘制树很简单.请参阅下面的大约80行代码的解决方案.
有几个人已开始尝试从视频重新创建交互式环境.其中一次尝试是由github用户tnlogy完成的.他的环境允许您在代码中选择一个数字,并使用滑块即时更改正在运行的演示.我已经将他的代码分叉以包含一个树演示.
http://brianpeiris.github.com/live-coding/
http://jsfiddle.net/brianpeiris/v9zD6/show
var
  drawLeaf = function (cx, xx, yy) {
    var
      leafAlpha = 8/10,
      leafSize = 7;
    cx.beginPath();
    cx.fillStyle = (
      "rgba(" + 
      Math.round(220 + (Math.random() * 50)) + ", " + 
      Math.round(180 + (Math.random() * 50)) + ", " + 
      Math.round(220 + (Math.random() * 50)) + ", " + 
      leafAlpha + 
      ")"
    );  
    cx.arc(xx, yy, leafSize, 0, Math.PI * 2);
    cx.fill();
  },
  drawBranch = function (ii, cx, xx, yy, level, levels, angle, numBranches) {
    var
      branchLength = 44,
      subBranchWidthFactor = 2,
      sweep = Math.PI * 25/30,
      branchTweakMagnitude = 52/50,
      tt;
    cx.beginPath();
    // Draw thinner branches away from the trunk
    cx.lineWidth = (levels - level) * subBranchWidthFactor;
    // Calculate the angle of the branch, with some random tweaks
    tt = (
      sweep * ii / (numBranches - 1) + angle -
      sweep / 2 + Math.PI + 
      Math.random() * 0.5 * branchTweakMagnitude
    );
    cx.moveTo(xx, yy);
    newXX = xx + Math.sin(tt) * branchLength;
    newYY = yy + Math.cos(tt) * branchLength;
    cx.lineTo(newXX, newYY);
    cx.stroke();
    // Recursively draw more branches
    drawBranchesAndLeaves(cx, newXX, newYY, level + 1, levels, Math.PI + tt);
  },
  drawBranchesAndLeaves = function (cx, xx, yy, level, levels, angle) {
    var
      numBranches = 5,
      ii, newXY;
    // This function is called recursively. The recursion terminates when we
    // have reached the specified number of recursive levels.
    if (level === levels) { 
      drawLeaf(cx, xx, yy);
      return; 
    }
    else {
      for (ii = 0; ii < numBranches; ii++) {
        drawBranch(ii, cx, xx, yy, level, levels, angle, numBranches);
      }
    }
  },
  drawTree = function(cx, ww, hh) {
    var trunkX = ww / 2, trunkY = hh - 165;
    cx.strokeStyle = "black";
    cx.lineWidth = 13;
    cx.lineCap = "round";
    cx.moveTo(trunkX, hh);
    cx.lineTo(trunkX, trunkY);
    cx.stroke();
    drawBranchesAndLeaves(cx, trunkX, trunkY, 0, 3, 0);
  },
  width = 350,
  height = 350,
  canvas = $('<canvas width="' + width + '" height="' + height + '"></canvas>'),
  ctx = canvas[0].getContext("2d");
  $('body').append(canvas);
  drawTree(ctx, width, height);