MCPcopy Create free account
hub / github.com/BloombergGraphics/whatiscode / initDraw

Function initDraw

modules/circuit/circuit.js:35–238  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33 ]
34
35 function initDraw(){
36
37
38 var x = d3.scale.linear().domain([0, cols - 1]).range([0, width])
39 var y = d3.scale.linear().domain([0, rows - 1]).range([0, height])
40
41 var gates = d3.range(cols*rows).map(function(n){
42 var rv = {
43 i: n % cols,
44 j: Math.floor(n / rows),
45 n: n,
46 type: types[Math.floor(Math.random()*types.length)],
47 on: false
48 }
49 rv.x = x(rv.i)
50 rv.y = y(rv.j)
51 rv.inputs = [{to: rv}, {to: rv}]
52 rv.outputs = []
53
54 return rv
55 })
56
57 //first col of gates are off or on, not actual gates
58 var onType = {str: 'ON', fn: function(){ return true }, paths: ["m -40,-40 h 80 v 80 h -80 z"] }
59 var offType = {str: 'OFF', fn: function(){ return false }, paths: ["m -40,-40 h 80 v 80 h -80 z"] }
60 var gatesByCol = d3.nest().key(ƒ('i')).entries(gates).sort(d3.ascendingKey('key'))
61 gatesByCol[0].values.forEach(function(d){
62 d.type = Math.random() < .5 ? onType : offType
63 })
64
65
66 //set up wires
67 var wires = _.flatten(gates.map(ƒ('inputs'))).filter(ƒ('to', 'i'))
68
69 var wireByCol = d3.nest().key(ƒ('to', 'i')).entries(wires)
70 wireByCol.forEach(function(wireCol){
71 wireCol.key = +wireCol.key
72
73 var prevGateCol = gatesByCol[wireCol.key - 1]
74
75 var gateSpace = x(1) - x(0) - gS
76 var vXscale = d3.scale.linear().domain([0, wireCol.values.length]).range([10, gateSpace - 10])
77
78 _.shuffle(wireCol.values).forEach(function(wire, i){
79 wire.colI = i
80 })
81
82 _.shuffle(wireCol.values).forEach(function(wire, i){
83 var gate = prevGateCol.values[Math.floor(i/2)]
84
85 wire.from = gate
86 gate.outputs.push(wire)
87
88 wire.vX = vXscale(i)
89 })
90 })
91
92 gates.forEach(function(gate){

Callers 1

circuit.jsFile · 0.70

Calls 3

updateFunction · 0.70
xFunction · 0.50
yFunction · 0.50

Tested by

no test coverage detected