Difference between revisions of "Template:Jscad"

From BITPlan Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 
<noinclude>
 
<noinclude>
 
= Usage =
 
= Usage =
Put your JSCAD code inside a source tag and add a call of this template
 
 
<pre>
 
<pre>
 +
{{jscad|title=OpenJSCAD example|code=function main()
 +
{
 +
  var resolution = 24; // increase to get smoother corners (will get slow!)
 +
 
 +
  var cube1 = CSG.roundedCube({center: [0,0,0], radius: [10,10,10], roundradius: 2, resolution: resolution});
 +
  var sphere1 = CSG.sphere({center: [5, 5, 5], radius: 10, resolution: resolution });
 +
  var sphere2 = sphere1.translate([12, 5, 0]);
 +
  var sphere3 = CSG.sphere({center: [20, 0, 0], radius: 30, resolution: resolution });
 +
 
 +
  var result = cube1;
 +
  result = result.union(sphere1);
 +
  result = result.subtract(sphere2);
 +
  result = result.intersect(sphere3);
 +
  return result;
 +
}
 +
}}
 +
</pre>
 +
= Example =
 
{{jscad|title=OpenJSCAD example|code=function main()
 
{{jscad|title=OpenJSCAD example|code=function main()
 
{
 
{
Line 21: Line 38:
 
[[Category:Template]]
 
[[Category:Template]]
 
</noinclude><includeonly>
 
</noinclude><includeonly>
 +
={{{title|}}}=
 
{{#tag:html|
 
{{#tag:html|
 
   <script src="/extensions/OpenJsCad/lightgl.js"></script>
 
   <script src="/extensions/OpenJsCad/lightgl.js"></script>
Line 26: Line 44:
 
   <script src="/extensions/OpenJsCad/openjscad.js"></script>
 
   <script src="/extensions/OpenJsCad/openjscad.js"></script>
 
   <style>
 
   <style>
pre, code, textarea {
+
pre {
 
   font: 12px/20px Monaco, monospace;
 
   font: 12px/20px Monaco, monospace;
 
   border: 1px solid #CCC;
 
   border: 1px solid #CCC;
Line 33: Line 51:
 
   padding: 0 3px;
 
   padding: 0 3px;
 
   color: #555;
 
   color: #555;
}
 
pre, textarea {
 
  padding: 10px;
 
 
   width: 100%;
 
   width: 100%;
}
 
textarea {
 
  height: 200px;
 
}
 
textarea:focus {
 
  outline: none;
 
 
}
 
}
 
canvas { cursor: move; }
 
canvas { cursor: move; }
Line 64: Line 73:
 
   }
 
   }
 
</script>
 
</script>
<h1>{{{title|}}}</h1>
 
 
<div id="jscadviewer"></div>
 
<div id="jscadviewer"></div>
<h2>Source code</h2>
+
<h2>Playground</h2>
Below is the OpenJsCad script.
+
<pre id='jscadcode'>{{{code|}}}</pre>
<textarea id='jscadcode' style="height: 500px">{{{code|}}}</textarea>
 
 
<input type="submit" value="Update" onclick="updateSolid(); return false;">
 
<input type="submit" value="Update" onclick="updateSolid(); return false;">
 
<script>
 
<script>
Line 76: Line 83:
 
</script>
 
</script>
 
}}
 
}}
= Syntaxh highlighted source code =
+
= Source Code =
 
{{#tag:source|{{{code|}}}|lang=javascript}}
 
{{#tag:source|{{{code|}}}|lang=javascript}}
 
</includeonly>
 
</includeonly>

Revision as of 15:39, 10 June 2019

Usage

{{jscad|title=OpenJSCAD example|code=function main()
{
  var resolution = 24; // increase to get smoother corners (will get slow!)
  
  var cube1 = CSG.roundedCube({center: [0,0,0], radius: [10,10,10], roundradius: 2, resolution: resolution});
  var sphere1 = CSG.sphere({center: [5, 5, 5], radius: 10, resolution: resolution });
  var sphere2 = sphere1.translate([12, 5, 0]);
  var sphere3 = CSG.sphere({center: [20, 0, 0], radius: 30, resolution: resolution });
  
  var result = cube1;
  result = result.union(sphere1);
  result = result.subtract(sphere2);
  result = result.intersect(sphere3);
  return result;
}
}}

Example

OpenJSCAD example

Playground

{{{code|}}}

Source Code

function main()
{
  var resolution = 24; // increase to get smoother corners (will get slow!)
  
  var cube1 = CSG.roundedCube({center: [0,0,0], radius: [10,10,10], roundradius: 2, resolution: resolution});
  var sphere1 = CSG.sphere({center: [5, 5, 5], radius: 10, resolution: resolution });
  var sphere2 = sphere1.translate([12, 5, 0]);
  var sphere3 = CSG.sphere({center: [20, 0, 0], radius: 30, resolution: resolution });
  
  var result = cube1;
  result = result.union(sphere1);
  result = result.subtract(sphere2);
  result = result.intersect(sphere3);
  return result;
}