Skip to content

Commit f246f83

Browse files
committed
added interactive demo
1 parent 3780ee0 commit f246f83

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

tests/index.html

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@
1111
padding: 50px 50px 200px 50px;
1212
}
1313

14-
pre, code {
14+
pre, code, textarea {
1515
font: 12px/20px Monaco, monospace;
1616
border: 1px solid #CCC;
1717
border-radius: 3px;
1818
background: #F9F9F9;
1919
padding: 0 3px;
2020
color: #555;
2121
}
22-
pre { padding: 10px; }
22+
pre, textarea {
23+
padding: 10px;
24+
width: 100%;
25+
}
26+
textarea {
27+
height: 200px;
28+
}
29+
textarea:focus {
30+
outline: none;
31+
}
2332

2433
h1, h2 { font: bold 50px/50px 'Helvetica Neue', Helvetica, Arial; }
2534
h2 { font-size: 30px; margin: 100px 0 0 0; }
@@ -82,9 +91,24 @@ <h2>Combined CSG Example</h2>
8291
<p>The solids above were generated with the following code:</p>
8392
<pre>var a = CSG.cube();
8493
var b = CSG.sphere({ radius: 1.35, stacks: 12 });
85-
var c = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(-1, 0, 0), end: new CSG.Vector(1, 0, 0) });
86-
var d = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(0, -1, 0), end: new CSG.Vector(0, 1, 0) });
87-
var e = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(0, 0, -1), end: new CSG.Vector(0, 0, 1) });</pre>
94+
var c = CSG.cylinder({ radius: 0.7, start: [-1, 0, 0], end: [1, 0, 0] });
95+
var d = CSG.cylinder({ radius: 0.7, start: [0, -1, 0], end: [0, 1, 0] });
96+
var e = CSG.cylinder({ radius: 0.7, start: [0, 0, -1], end: [0, 0, 1] });</pre>
97+
98+
<h2>Try it!</h2>
99+
<table>
100+
<tr>
101+
<td><div id="11" class="viewer" style="background-image:none;width:400px;height:400px;"></div></td>
102+
</tr>
103+
</table>
104+
<p>Edit the code below to construct your own solids. A browser with WebGL is required to view the results. The <code>setColor(r, g, b)</code> function sets the color of the solid using values between 0 and 1 (just for display, not part of csg.js):</p>
105+
<textarea id="tryit">var a = CSG.cube();
106+
var b = CSG.sphere({ radius: 1.35 });
107+
a.setColor(1, 0, 0);
108+
b.setColor(0, 0, 1);
109+
return a.subtract(b);
110+
</textarea>
111+
<p id="error"></p>
88112

89113
<script>
90114

@@ -93,6 +117,7 @@ <h2>Combined CSG Example</h2>
93117
this.toPolygons().map(function(polygon) {
94118
polygon.shared = [r, g, b];
95119
});
120+
return this;
96121
};
97122

98123
// Convert from CSG solid to GL.Mesh object
@@ -101,7 +126,7 @@ <h2>Combined CSG Example</h2>
101126
var indexer = new GL.Indexer();
102127
this.toPolygons().map(function(polygon) {
103128
var indices = polygon.vertices.map(function(vertex) {
104-
vertex.color = polygon.shared;
129+
vertex.color = polygon.shared || [1, 1, 1];
105130
return indexer.add(vertex);
106131
});
107132
for (var i = 2; i < indices.length; i++) {
@@ -238,9 +263,9 @@ <h2>Combined CSG Example</h2>
238263
// Generate example from wikipedia
239264
var a = CSG.cube();
240265
var b = CSG.sphere({ radius: 1.35, stacks: 12 });
241-
var c = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(-1, 0, 0), end: new CSG.Vector(1, 0, 0) });
242-
var d = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(0, -1, 0), end: new CSG.Vector(0, 1, 0) });
243-
var e = CSG.cylinder({ radius: 0.7, start: new CSG.Vector(0, 0, -1), end: new CSG.Vector(0, 0, 1) });
266+
var c = CSG.cylinder({ radius: 0.7, start: [-1, 0, 0], end: [1, 0, 0] });
267+
var d = CSG.cylinder({ radius: 0.7, start: [0, -1, 0], end: [0, 1, 0] });
268+
var e = CSG.cylinder({ radius: 0.7, start: [0, 0, -1], end: [0, 0, 1] });
244269
a.setColor(1, 0, 0);
245270
b.setColor(0, 0, 1);
246271
c.setColor(0, 1, 0);
@@ -259,5 +284,27 @@ <h2>Combined CSG Example</h2>
259284
addViewer(new Viewer(operations[i], size, size, 5));
260285
}
261286

287+
// Set up interactive demo
288+
var tryit = document.getElementById('tryit');
289+
var timeout = null;
290+
var viewer = new Viewer(new CSG(), 400, 400, 5);
291+
addViewer(viewer);
292+
function rebuild() {
293+
var error = document.getElementById('error');
294+
try {
295+
var solid = new Function(tryit.value)();
296+
error.innerHTML = '';
297+
} catch (e) {
298+
error.innerHTML = 'Error: <code>' + e.toString().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + '</code>';
299+
}
300+
viewer.mesh = solid.toMesh();
301+
viewer.gl.ondraw();
302+
}
303+
tryit.onkeydown = function() {
304+
if (timeout) clearTimeout(timeout);
305+
timeout = setTimeout(rebuild, 100);
306+
};
307+
rebuild();
308+
262309
</script>
263310
</body></html>

0 commit comments

Comments
 (0)