Skip to content

Commit

Permalink
Merge pull request #14 from hackwin/master
Browse files Browse the repository at this point in the history
Improvements to wasm usage example in readme
  • Loading branch information
DSchroer authored Apr 27, 2024
2 parents e24b0b6 + 063531a commit fb7d65c
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,47 @@ make test
The project is an ES6 module. Simply import the module:

```ts
<html>
<head></head>
<body>

<script type="module">

import OpenSCAD from "./openscad.js";

// OPTIONAL: add fonts to the FS
import { addFonts } from "./openscad.fonts.js";
// OPTIONAL: add MCAD liibrary to the FS

// OPTIONAL: add MCAD library to the FS
import { addMCAD } from "./openscad.mcad.js";

const filename = "cube.stl";

// Instantiate the application
const instance = await OpenSCAD();
const instance = await OpenSCAD({noInitialRun: true});

// OPTIONAL: add fonts to the FS
addFonts(instance);
// Write a file to the filesystem
instance.FS.writeFile("/input.scad", `cube(10);`); // OpenSCAD script to generate a 10mm cube

// OPTIONAL: add MCAD liibrary to the FS
addMCAD(instance);
// Run like a command-line program with arguments
instance.callMain(["/input.scad", "--enable=manifold", "-o", filename]); // manifold is faster at rendering

// Write a file to the filesystem
instance.FS.writeFile("/input.scad", `cube(10);`);
// Read the output 3D-model into a JS byte-array
const output = instance.FS.readFile("/"+filename);

// Generate a link to output 3D-model and download the output STL file
const link = document.createElement("a");
link.href = URL.createObjectURL(
new Blob([output], { type: "application/octet-stream" }), null);
link.download = filename;
document.body.append(link);
link.click();
link.remove();

// Run OpenSCAD with the arguments "/input.scad -o cube.stl"
instance.callMain(["/input.scad", "-o", "cube.stl"]);
</script>

// Read the data from cube.stl
const output = instance.FS.readFile("/cube.stl");
</body>
</html>
```

For more information on reading and writing files check out the [Emscripten File System API](https://emscripten.org/docs/api_reference/Filesystem-API.html).

0 comments on commit fb7d65c

Please sign in to comment.