"Inventing on Principle" 视频中出现的具有 JavaScript 树形结构的源代码

10

我非常受到Bret Victor的"Inventing on Principle"视频的启发(http://vimeo.com/36579366)。

此外,我对使用Javascript绘制的那棵树非常着迷。我并没有做过太多的图形编程。在我的整个职业生涯中,我一直是一名中间层和数据库开发人员。但是看着那个通过编程绘制的树,我被激发了学习的动力。我已经开始学习JavaScript。我知道我最终(在几周或几个月内,取决于我腾出时间的多少)能够自己从头开始编写这样的程序。

然而,我真的非常渴望得到一些用Javascript实现类似绘画的源代码,并与之交互。如果你们能提供任何链接/指针,将非常有用。


那确实是一幅迷人的图画。我做了很多像素绘图来创建图表,但没有像这样的艺术绘画。我很想看看源代码! - akirekadu
3个回答

11

使用canvas画树很简单。以下是大约80行代码的解决方案。

有几个人试图重新创建视频中的交互环境。其中之一是由GitHub用户tnlogy创建的这些尝试中的一个。他的环境允许您在代码中选择数字并使用滑块实时更改运行演示。我分支了他的代码以包含树演示。

交互式树演示:

https://brianpeiris.github.io/live-coding/client/

简单树演示:

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);

尽管不如 Bret 的代码树美观,但无论如何都是非常有用的代码。 - greppz

1
Credit to Ian Johnson (@enjalot) for sharing this with me, but here is a link to a d3js version of a tree. http://tributary.io/inlet/4b0a56692447fa75d8a1 which is adapted from this version by Peter Cook http://prcweb.co.uk/lab/d3-tree/ Uses a combination of paths:
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;
}

再次感谢Ian。点击此处查看实时演示。

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接