1 | var NeuroViewerView = (function( config ) |
---|
2 | { |
---|
3 | |
---|
4 | var objectsToDraw = []; |
---|
5 | var bounds = null; |
---|
6 | |
---|
7 | var container = document.createElement( 'div' ); |
---|
8 | container.className = "neuro-viewer-container"; |
---|
9 | var canvas = document.createElement( 'canvas' ); |
---|
10 | canvas.id = config.canvas.id; |
---|
11 | var context = canvas.getContext( '2d' ); |
---|
12 | container.appendChild( canvas ); |
---|
13 | |
---|
14 | var screenWidth = canvas.width; |
---|
15 | var screenHeight = canvas.height; |
---|
16 | var devicePixelRatio = (window.devicePixelRatio || 1); |
---|
17 | var backingStoreRatio = context.webkitBackingStorePixelRatio || |
---|
18 | context.mozBackingStorePixelRatio || |
---|
19 | context.msBackingStorePixelRatio || |
---|
20 | context.oBackingStorePixelRatio || |
---|
21 | context.backingStorePixelRatio || 1; |
---|
22 | |
---|
23 | var screenRatio = devicePixelRatio / backingStoreRatio; |
---|
24 | |
---|
25 | var mesh = create3DContainer(); |
---|
26 | |
---|
27 | function resize( width, height ) { |
---|
28 | screenWidth = width; |
---|
29 | screenHeight = height; |
---|
30 | |
---|
31 | canvas.width = width * screenRatio; |
---|
32 | canvas.height = height * screenRatio; |
---|
33 | |
---|
34 | canvas.style.width = width + 'px'; |
---|
35 | canvas.style.height = height + 'px'; |
---|
36 | |
---|
37 | context.scale(screenRatio, screenRatio); |
---|
38 | |
---|
39 | render(); |
---|
40 | } |
---|
41 | function make2D(){ |
---|
42 | container.id = "neuro-viewer-2d"; |
---|
43 | document.body.appendChild(container); |
---|
44 | } |
---|
45 | |
---|
46 | function make3D(){ |
---|
47 | container.id = "neuro-viewer-3d"; |
---|
48 | } |
---|
49 | |
---|
50 | function render() { |
---|
51 | clear(); |
---|
52 | context.fillStyle = "transparent"; |
---|
53 | context.fillRect(0, 0, canvas.width, canvas.height); |
---|
54 | |
---|
55 | if( objectsToDraw.length === 0 ) { |
---|
56 | context.font = config.canvas.font; |
---|
57 | context.fillStyle = config.canvas.strokeStyle; |
---|
58 | context.fillText( config.emptyLabel, 12, 22 ); |
---|
59 | } |
---|
60 | |
---|
61 | if( bounds != null ) { |
---|
62 | for( var i=0; i<objectsToDraw.length; ++i ) { |
---|
63 | objectsToDraw[i].update( screenWidth, screenHeight, bounds ); |
---|
64 | objectsToDraw[i].draw( context ); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | function getNeuroLayoutBounds( data ) { |
---|
70 | |
---|
71 | var result = { minX: null, maxX: null, minY: null, maxY: null }; |
---|
72 | |
---|
73 | for( var i=0; i<data.getElements(); i++ ) { |
---|
74 | |
---|
75 | var xywh=data.getValueXYWH(i); |
---|
76 | if( (result.minX == null) || (result.minX > xywh.get_x()) ) { |
---|
77 | result.minX = xywh.get_x(); |
---|
78 | } |
---|
79 | var xw = xywh.get_x() + xywh.get_w(); |
---|
80 | if( (result.maxX == null) || (result.maxX < xw) ) { |
---|
81 | result.maxX = xw; |
---|
82 | } |
---|
83 | if( (result.minY == null) || (result.minY > xywh.get_y()) ) { |
---|
84 | result.minY = xywh.get_y(); |
---|
85 | } |
---|
86 | var yh = xywh.get_y() + xywh.get_h(); |
---|
87 | if( (result.maxY == null) || (result.maxY < yh) ) { |
---|
88 | result.maxY = yh; |
---|
89 | } |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | result.width = Math.abs( result.maxX - result.minX ); |
---|
94 | result.height = Math.abs( result.maxY - result.minY ); |
---|
95 | return result; |
---|
96 | } |
---|
97 | |
---|
98 | function updateBrain( data ) { |
---|
99 | |
---|
100 | objectsToDraw = []; |
---|
101 | bounds = getNeuroLayoutBounds( data ); |
---|
102 | |
---|
103 | var neuroFactory = new NeuroFactory( config.canvas ); |
---|
104 | for( var i=0; i<data.getElements(); i++ ) { |
---|
105 | |
---|
106 | var xywh=data.getValueXYWH(i); |
---|
107 | var neuro = neuroFactory.create( xywh.get_x(), xywh.get_y(), xywh.get_w(), xywh.get_h(), data.getNeuro(i) ); |
---|
108 | objectsToDraw.push( neuro ); |
---|
109 | |
---|
110 | for( var j=0; j<data.getNeuro(i).getInputCount(); j++ ) { |
---|
111 | var inputRefNo = data.getNeuro(i).getInput(j).get_refno(); |
---|
112 | var inputCoords = data.getValueXYWH(inputRefNo); |
---|
113 | |
---|
114 | neuro.inputsLayoutPositions.push( { |
---|
115 | x: inputCoords.get_x(), |
---|
116 | y: inputCoords.get_y(), |
---|
117 | w: inputCoords.get_w(), |
---|
118 | h: inputCoords.get_h() |
---|
119 | } ); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | render(); |
---|
124 | } |
---|
125 | |
---|
126 | function create3DContainer(){ |
---|
127 | var neuro3d = new CSS3DObject(container); |
---|
128 | neuro3d.position.set(0,0,0); |
---|
129 | neuro3d.rotation.set(0,0,0); |
---|
130 | return neuro3d; |
---|
131 | } |
---|
132 | |
---|
133 | function move(position) { |
---|
134 | let tweenPlank = new TWEEN.Tween(mesh.position).to(position, Config.Table.GenotypePlank.Tween.Move.SPEED); |
---|
135 | tweenPlank.easing(Config.Table.GenotypePlank.Tween.Move.EASING); |
---|
136 | tweenPlank.start(); |
---|
137 | } |
---|
138 | |
---|
139 | function rotate(angle) { |
---|
140 | let tweenPlank = new TWEEN.Tween(mesh.rotation).to(angle, Config.Table.GenotypePlank.Tween.Rotate.SPEED); |
---|
141 | tweenPlank.easing(Config.Table.GenotypePlank.Tween.Rotate.EASING); |
---|
142 | tweenPlank.start(); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | function clear(){ |
---|
147 | context.clearRect(0, 0, canvas.width, canvas.height); |
---|
148 | } |
---|
149 | |
---|
150 | function get3DContainer(){ |
---|
151 | return mesh; |
---|
152 | } |
---|
153 | |
---|
154 | return { |
---|
155 | resize: resize, |
---|
156 | clear: clear, |
---|
157 | updateBrain: updateBrain, |
---|
158 | get3DContainer: get3DContainer, |
---|
159 | make2D: make2D, |
---|
160 | make3D: make3D, |
---|
161 | move: move, |
---|
162 | rotate: rotate, |
---|
163 | } |
---|
164 | |
---|
165 | }); |
---|