“基于原理的发明”视频中的Javascript树的源代码

时间:2012-03-20 19:36:38

标签: javascript 2d

布雷特·维克多的“发明原理”视频(http://vimeo.com/36579366)让我深受启发。

此外,我对使用Javascript绘制的树非常着迷。我没有做太多的图形编程。我所有的职业生涯都是中层和数据库开发人员。但是以编程方式绘制树,我很有动力去学习。我已经开始学习Javascript了。我知道我最终会(几周或几个月,取决于我得到多少时间)能够自己编写这样的程序。

但是,我真的非常渴望获得一些在Javascript中使用类似绘图并使用它的源代码。你们可以提供的任何链接/指针都非常有用。

3 个答案:

答案 0 :(得分:10)

使用画布绘制树很简单。请参阅下面的大约80行代码中的解决方案。

有些人已开始尝试从视频重新创建交互式环境。 One of those attemptsgithub user 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);

答案 1 :(得分:1)

如果您对Javascript中的2D图形非常陌生,那么这是一个很好的起点。

https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas

至于树源代码,我也有兴趣看一下它!

答案 2 :(得分:1)

感谢Ian Johnson(@enjalot)与我分享此内容,但这里是d3js版本树的链接。 http://tributary.io/inlet/4b0a56692447fa75d8a1由Peter Cook http://prcweb.co.uk/lab/d3-tree/

改编自此版本

使用路径组合:

var pathGenerator = d3.svg.line()
.x(function(d) {
  return d.x;
})
.y(function(d) {
  return d.y;
})

获取父母,点和路径的函数:

function getPoints(branches) {
  var points = [];
  branches.forEach(function(branch) {
    points.push( {x: x1(branch), y: y1(branch) });
    points.push( {x: x2(branch), y: y2(branch) });
  });
  return points;
}

function getParent(branch, p, branches) {
  if(!branch.parent) return;
  var b = branches[branch.parent];
  p.push({x: b.x, y: b.y})
  getParent(b, p, branches);

}


function getPaths(branches) {
  var paths = [];

  var i = 0;
  branches.forEach(function(branch) {
    if(branch.d < maxDepth) return;
    var p = [{x: branch.x, y: branch.y}];
    getParent(branch, p, branches);
    p.push(seed);
    paths.push(p);
  });
  return paths;
}

再次,HT到伊恩。 Live demo here.