Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 29 additions & 42 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name: Build & deploy
name: CI

# Checks every push and PR. Deployment is done with pm2 (see ecosystem.config.cjs).

on:
push:
Expand All @@ -9,46 +11,31 @@ on:
- main

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Node.js
uses: actions/setup-node@v1
with:
node-version: 16.x

- name: Install NPM packages
run: npm ci

- name: Build project
run: npm run build

- name: Upload production-ready build files
uses: actions/upload-artifact@v2
with:
name: production-files
path: ./build

deploy:
name: Deploy
needs: build
check:
name: Typecheck, lint, test, build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'


steps:
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: production-files
path: ./build

- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
- name: Checkout code
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
cache: npm

- name: Install NPM packages
run: npm ci

- name: Typecheck
run: npm run typecheck

- name: Lint
run: npm run lint

- name: Test
run: npm test

- name: Build
run: npm run build
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# vite
/dist
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
build
coverage
package-lock.json
8 changes: 8 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"printWidth": 120,
"trailingComma": "all",
"overrides": [{ "files": ["*.json", "*.yaml", "*.yml", "*.md", "*.css", "*.html"], "options": { "tabWidth": 2 } }]
}
76 changes: 68 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,74 @@ See [the live app here](https://apps.josh.earth/boxbuilder/)

![screenshot](images/stl-box-generator-screenshot.png)

# Using it

Drag the sliders (or type into the number boxes) and the preview updates live. All sizes are in mm.
When it looks right, press **Download STL** to save `box.stl`.

| Setting | What it does |
| ---------------------- | ----------------------------------------------------------------------------------------------- |
| Width / Depth / Height | Outside size of the box. |
| Wall thickness | Floor thickness. Each side wall is half of this (the cavity is `width − wall thickness` wide). |
| Lid thickness | Total thickness of the lid: a flat cap plus a ring that drops into the box. |
| Lid tolerance | Gap between the lid's ring and the box walls. Increase it if the lid is too tight. |
| Corner radius | Rounds the four vertical corners of the box and lid. The rim stays flat so the lid seats flush. |

The current settings are saved in the page URL, so you can bookmark or share a box.

**Moving the preview:** drag to rotate, shift-drag to pan, scroll to zoom. On touch screens, drag
with one finger and pinch with two. With the preview focused (click it or Tab to it), arrow keys
rotate, shift+arrows pan, and `+` / `-` zoom.

# How it works

The core of the code is pretty simple.
* It's a React app with sliders made with [create-react-app](https://create-react-app.dev).
* The fancy math is done with [JSCad libraries](https://github.com/jscad/OpenJSCAD.org).
* Sliders modify a small data model of the box.
* When model changes it calls box_to_solids() to produce JSCad geometry solids.
* [jscad-react](https://github.com/kenianbei/jscad-react) does the interactive 3D rendering (presumably with ThreeJS underneath).
* export_stl() uses a [JSCad serializer](https://www.npmjs.com/package/@jscad/stl-serializer) to generate the STL as a binary blob
* force download the blob as box.stl.
The core of the code is pretty simple.

- It's a React + TypeScript app built with [Vite](https://vite.dev), styled with tooltheme (vendored in `vendor/`).
- The fancy math is done with [JSCad libraries](https://github.com/jscad/OpenJSCAD.org).
- A small [zod](https://zod.dev) schema describes the box; `src/autoform.tsx` turns it into sliders.
- When the model changes, `boxToSolids()` in `src/box.ts` produces JSCad geometry solids. Rounded corners are
extruded rounded rectangles; each inner outline is an inset of the outer one, so walls stay even around the curves.
- `src/viewer/` renders them with [@jscad/regl-renderer](https://www.npmjs.com/package/@jscad/regl-renderer) (orbit, pan, zoom by mouse, touch or keyboard).
- `solidsToStlBlob()` uses the [JSCad STL serializer](https://www.npmjs.com/package/@jscad/stl-serializer) to generate a binary STL, downloaded as box.stl.
- The current box is saved in the URL, so links and the back button restore it.

# Development

Needs Node 20.19+ or 22.12+.

```sh
npm install
npm run dev # http://localhost:5173/boxbuilder/
npm test # vitest
npm run lint # eslint
npm run typecheck
npm run build # outputs dist/
```

# tooltheme

The UI uses tooltheme, a classless CSS theme. Its GitHub repo is private, so the package is
vendored as `vendor/joshmarinacci-tooltheme-0.1.0.tgz` and installed from there (this also lets the
deploy server install it without GitHub credentials). To pick up a new version:

```sh
(cd ../tooltheme && npm pack --pack-destination ../boxbuilder/vendor)
npm install ./vendor/joshmarinacci-tooltheme-<version>.tgz
```

Once tooltheme is published to npm, replace the `file:` dependency with the registry version.

`src/App.css` copies tooltheme's app-shell layout rules onto `#root`, because tooltheme's shell
only matches elements directly inside `<body>` and React renders inside `<div id="root">`.

# Deploy

Deployed with pm2 to josh.earth (see `ecosystem.config.cjs`). It's a static site: pm2 checks out
the repo, runs `npm ci && npm run build`, and nginx serves `/projects/boxbuilder/current/dist`
at `/boxbuilder/`.

```sh
pm2 deploy ecosystem.config.cjs production setup # first time only
pm2 deploy ecosystem.config.cjs production
```
27 changes: 27 additions & 0 deletions ecosystem.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// pm2 deploy config for boxbuilder. The app is a static site, so no pm2 process runs; pm2 deploy
// checks out the repo on the server and builds it.
//
// pm2 deploy ecosystem.config.cjs production setup # first time: clone repo on the server
// pm2 deploy ecosystem.config.cjs production # every deploy after that
//
// A deploy checks out `ref` into <path>/source (reached through <path>/current), installs
// dependencies and runs the production build. Point the web server at <path>/current/dist,
// served under /boxbuilder/ (the base path is set in vite.config.ts). The server needs Node
// 20.19+ or 22.12+ (see "engines" in package.json).
// Override the defaults with the BOXBUILDER_DEPLOY_* variables.

module.exports = {
apps: [],

deploy: {
production: {
user: process.env.BOXBUILDER_DEPLOY_USER || 'deployman',
host: [process.env.BOXBUILDER_DEPLOY_HOST || 'josh.earth'],
key: process.env.BOXBUILDER_DEPLOY_KEY || '../deployman_private.key',
ref: process.env.BOXBUILDER_DEPLOY_REF || 'origin/main',
repo: 'https://github.com/joshmarinacci/boxbuilder.git',
path: process.env.BOXBUILDER_DEPLOY_PATH || '/projects/boxbuilder',
'post-deploy': 'npm ci && npm run build',
},
},
}
26 changes: 26 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import { defineConfig, globalIgnores } from 'eslint/config'

export default defineConfig([
globalIgnores(['dist', 'build', 'coverage']),
{
files: ['**/*.{ts,tsx}'],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
reactHooks.configs.flat['recommended-latest'],
reactRefresh.configs.vite,
],
languageOptions: {
globals: globals.browser,
},
},
{
files: ['*.config.{js,ts,cjs}'],
languageOptions: { globals: globals.node },
},
])
30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Generate 3D printable STL boxes with lids" />
<link rel="apple-touch-icon" href="/logo192.png" />
<link rel="manifest" href="/manifest.json" />
<title>BoxBuilder by Josh.earth</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script>
;(function () {
const endpoint = 'https://stats.josh.earth/api/track'
const payload = JSON.stringify({
url: window.location.href,
referrer: document.referrer,
timestamp: new Date().toISOString(),
})

navigator.sendBeacon(endpoint, payload)
})()
</script>
</body>
</html>
Loading
Loading