Created bellows library

This commit is contained in:
mmcw-dev 2017-12-27 16:43:55 -05:00
commit 70c0161caf
8 changed files with 680 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
example/bellows.png

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
example/bellows.png

38
README.md Normal file
View File

@ -0,0 +1,38 @@
<a name="module_bellows"></a>
## bellows
**Example**
```js
const bellows = require('bellows')
```
<a name="module_bellows..bellows"></a>
### bellows~bellows([options])
Generate bellows pattern for cutting and folding.
**Kind**: inner method of [<code>bellows</code>](#module_bellows)
| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>Object</code> | Bellows configuration options |
| [options.dpi] | <code>Integer</code> | DPI of the image |
| [options.pageW] | <code>Integer</code> | Page width in pixels |
| [options.pageH] | <code>Integer</code> | Page height in pixels |
| [options.frontIW] | <code>Integer</code> | Front inner width of bellows in pixels |
| [options.frontOW] | <code>Integer</code> | Front outer width of bellows in pixels |
| [options.frontIH] | <code>Integer</code> | Front inner height of bellows in pixels |
| [options.frontOH] | <code>Integer</code> | Front outer height of bellows in pixels |
| [options.backIW] | <code>Integer</code> | Back inner width of bellows in pixels |
| [options.backOW] | <code>Integer</code> | Back outer width of bellows in pixels |
| [options.backIH] | <code>Integer</code> | Back inner height of bellows in pixels |
| [options.backOH] | <code>Integer</code> | Back outer height of bellows in pixels |
| [options.maxLength] | <code>Integer</code> | Maximum length of bellows in pixels |
| [options.align] | <code>Integer</code> | Vertical alignment adjustment in pixels |
| [options.parts] | <code>Integer</code> | Number of parts to split pattern into: 1, 2, or 4 returns {String} Base64 encoded png data |
**Example**
Generate the default square bellows in 2 parts and print the string to the console.
```
const b = bellows({ parts : 2 })
console.log(b)
```

181
example/index.html Normal file
View File

@ -0,0 +1,181 @@
<!doctype html>
<html>
<head>
<title>Bellows Browser Example</title>
<style>
form{
clear: both;
}
form:after{
display: block;
content: " ";
}
label{
width: 150px;
display: inline-block;
}
form > div {
width: 400px;
float: left;
margin: 20px 0;
}
img{
width: 100%;
height: auto;
border: 1px solid #000;
}
button{
margin: 10px 20px;
}
input{
margin-top: 10px;
}
</style>
</head>
<body>
<h1>
Bellows Pattern Generator
</h1>
<div>
<a href="https://github.com/sixteenmillimeter/bellows">View Source</a>
</div>
<form onsubmit="return false;">
<div>
<div>
<label for="backOW">Back Outer Width</label>
<input name="backOW" type="number" value="50" /> mm
</div>
<div>
<label for="backIW">Back Inner Width</label>
<input name="backIW" type="number" value="40" /> mm
</div>
<div>
<label for="backOH">Back Outer Height</label>
<input name="backOH" type="number" value="50" /> mm
</div>
<div>
<label for="backIH">Back Inner Height</label>
<input name="backIH" type="number" value="40" /> mm
</div>
</div>
<div>
<div>
<label for="frontOW">Front Outer Width</label>
<input name="frontOW" type="number" value="50" /> mm
</div>
<div>
<label for="frontIW">Front Inner Width</label>
<input name="frontIW" type="number" value="40" /> mm
</div>
<div>
<label for="frontOH">Front Outer Height</label>
<input name="frontOH" type="number" value="50" /> mm
</div>
<div>
<label for="frontIH">Front Inner Height</label>
<input name="frontIH" type="number" value="40" /> mm
</div>
</div>
<div>
<div>
<label for="pageW">Sheet Width</label>
<input name="pageW" type="number" value="11" /> in
</div>
<div>
<label for="pageH">Sheet Height</label>
<input name="pageH" type="number" value="11" /> in
</div>
<div>
<label for="dpi">DPI</label>
<input name="dpi" type="number" value="300" />
</div>
<div>
<label for="maxLength">Maximum Length</label>
<input name="maxLength" type="number" value="200" /> mm
</div>
<div>
<label for="parts">Seperate Parts</label>
<input name="parts" type="number" value="1" min="1" max="4" /> 1, 2, or 4
</div>
<div>
<label for="align">Adjust Alignment</label>
<input name="align" type="number" value="0" min="0" />
</div>
</div>
</form>
<div>
<button id="refresh" onclick="refresh();">Refresh</button>
<button id="download" onclick="download();">Download</button>
</div>
<img id="img" />
<script src="../src/bellows.js"> </script>
<script>
'use script'
const $ = function (select) {
return document.querySelector(`[name="${select}"]`)
}
let B
function refresh () {
const IN = 25.4
const DPI = $('dpi').value
const MM = DPI / IN
const pageW = Math.round($('pageW').value * DPI);
const pageH = Math.round($('pageH').value * DPI);
const backOW = Math.round($('backOW').value * MM);
const backIW = Math.round($('backIW').value * MM);
const backOH = Math.round($('backOH').value * MM);
const backIH = Math.round($('backIH').value * MM);
const frontOW = Math.round($('frontOW').value * MM);
const frontIW = Math.round($('frontIW').value * MM);
const frontOH = Math.round($('frontOH').value * MM);
const frontIH = Math.round($('frontIH').value * MM);
const maxLength = Math.round($('maxLength').value * MM);
const align = Math.round($('align').value);
let parts = Math.round($('parts').value);
if (parts === 3 || parts > 4) {
parts = 1
}
B = bellows(
{
dpi : DPI,
pageW : pageW,
pageH : pageH,
backOW : backOW,
backIW : backIW,
backOH : backOH,
backIH : backIH,
frontOW : frontOW,
frontIW : frontIW,
frontOH : frontOH,
frontIH : frontIH,
maxLength : maxLength,
parts : parts,
align : align
}
);
document.getElementById('img').setAttribute('src', B);
}
function download () {
const a = document.createElement('a');
a.href = B;
a.download = 'bellows.png';
a.click();
}
refresh()
</script>
</body>
</html

26
example/index.js Normal file
View File

@ -0,0 +1,26 @@
'use strict'
const fs = require('fs')
const bellows = require('../src/bellows.js')
let base64Data
let binaryData
let options = {
//use defaults
}
const B = bellows(options)
base64Data = B.replace(/^data:image\/png;base64,/, '')
base64Data += base64Data.replace('+', ' ')
binaryData = new Buffer(base64Data, 'base64').toString('binary')
fs.writeFile('./example/bellows.png', binaryData, 'binary', (err) => {
if (err) {
console.error(err) // writes out file without error, but it's not a valid image
}
console.log('Wrote file ./example/bellows.png')
})
//console.log(B)

21
package-lock.json generated Normal file
View File

@ -0,0 +1,21 @@
{
"name": "bellows",
"version": "0.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"canvas": {
"version": "2.0.0-alpha.9",
"resolved": "https://registry.npmjs.org/canvas/-/canvas-2.0.0-alpha.9.tgz",
"integrity": "sha512-H0wOexRlHg1bE6/JYIaNRcDj5B2RpuCDqJrO/J8UTPMjM19T1FbXfqjLYI5sZ1oua2E1Bpl4FL8bqzx0pPBalQ==",
"requires": {
"nan": "2.8.0"
}
},
"nan": {
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.8.0.tgz",
"integrity": "sha1-7XFfP+neArV6XmJS2QqWZ14fCFo="
}
}
}

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "bellows",
"version": "0.0.1",
"description": "Generate bellows folding and cutting patterns.",
"main": "./src/bellows.js",
"dependencies": {
"canvas": "^2.0.0-alpha.9"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sixteenmillimeter/bellows.git"
},
"keywords": [
"bellows",
"folding",
"paper",
"craft"
],
"author": "sixteenmillimeter",
"license": "MIT",
"bugs": {
"url": "https://github.com/sixteenmillimeter/bellows/issues"
},
"homepage": "https://github.com/sixteenmillimeter/bellows#readme"
}

381
src/bellows.js Normal file
View File

@ -0,0 +1,381 @@
'use strict'
/**
* @module bellows
* @example
* const bellows = require('bellows')
*/
const IN = 25.4
const BLACK = '0,0,0,1.0'
let browser = (typeof module === 'undefined')
let createCanvas
if (!browser) {
createCanvas = require('canvas').createCanvas
}
function line (ctx, rgba, start, end) {
ctx.strokeStyle = `rgba(${rgba})`
ctx.beginPath()
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
ctx.stroke()
}
function dash (ctx, rgba, dpi, start, end) {
ctx.strokeStyle = `rgba(${rgba})`
ctx.setLineDash([(dpi / IN) * 1, (dpi / IN) * 0.5])
ctx.beginPath()
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
ctx.stroke()
ctx.setLineDash([0, 0])
}
function basicShape (ctx, offset, front, back, length) {
const frontOffset = Math.round((back - front) / 2)
const X = -back / 2 + offset[0]
const Y = -length / 2 + offset[1]
//sides
line(ctx, BLACK, [X, Y], [X + frontOffset, Y + length])
line(ctx, BLACK, [X + back, Y], [X + front + frontOffset, Y + length])
//top+bottom
line(ctx, BLACK, [X, Y], [X + back, Y])
line(ctx, BLACK, [X + frontOffset, Y + length], [X + front + frontOffset, Y + length])
}
function taper (back, front, length, pos) {
const diff = (back - front) / 2
//0, 0 -> length, diff
return Math.round((diff / length) * pos)
}
/**
* Generate bellows pattern for cutting and folding.
*
* @param {Object} [options] Bellows configuration options
* @param {Integer} [options.dpi] DPI of the image
* @param {Integer} [options.pageW] Page width in pixels
* @param {Integer} [options.pageH] Page height in pixels
* @param {Integer} [options.frontIW] Front inner width of bellows in pixels
* @param {Integer} [options.frontOW] Front outer width of bellows in pixels
* @param {Integer} [options.frontIH] Front inner height of bellows in pixels
* @param {Integer} [options.frontOH] Front outer height of bellows in pixels
* @param {Integer} [options.backIW] Back inner width of bellows in pixels
* @param {Integer} [options.backOW] Back outer width of bellows in pixels
* @param {Integer} [options.backIH] Back inner height of bellows in pixels
* @param {Integer} [options.backOH] Back outer height of bellows in pixels
* @param {Integer} [options.maxLength] Maximum length of bellows in pixels
* @param {Integer} [options.align] Vertical alignment adjustment in pixels
* @param {Integer} [options.parts] Number of parts to split pattern into: 1, 2, or 4
*
* returns {String} Base64 encoded png data
*
* @example
* Generate the default square bellows in 2 parts and print the string to the console.
* ```
* const b = bellows({ parts : 2 })
* console.log(b)
* ```
*/
function bellows (options = {}) {
let canvas
let ctx
let dpi = options.dpi || 300
const MM = dpi / IN
let pageW = options.pageW || dpi * 11
let pageH = options.pageH || dpi * 11
let frontIW = options.frontIW || Math.round(MM * 40)
let frontOW = options.frontOW || Math.round(MM * 50)
let frontIH = options.frontIH || Math.round(MM * 40)
let frontOH = options.frontOH || Math.round(MM * 50)
let backIW = options.backIW || Math.round(MM * 40)
let backOW = options.backOW || Math.round(MM * 50)
let backIH = options.backIH || Math.round(MM * 40)
let backOH = options.backOH || Math.round(MM * 50)
let align = options.align || 0 //adjust the alignment (find formula)
let maxLength = options.maxLength || MM * 280
let parts = options.parts || 1
let centerX = Math.round(pageW / 2)
let centerY = Math.round(pageH / 2)
let fold = (frontOW - frontIW) / 2
let folds = Math.floor(maxLength / fold)
let length = folds * fold
let angleW
let angleH
//console.dir(options)
if (!browser) {
canvas = createCanvas(pageW, pageH)
} else {
canvas = document.createElement('canvas')
canvas.width = pageW
canvas.height = pageH
}
ctx = canvas.getContext('2d')
ctx.antialias = 'subpixel'
//fill background white
ctx.fillStyle = 'rgba(255, 255, 255, 1.0)'
ctx.fillRect(0, 0, pageW, pageH)
//key
ctx.font = '15px Arial';
ctx.textAlign= 'start';
ctx.fillText('Cut lines' ,150, 60);
line(ctx, BLACK, [IN * dpi, IN * dpi], [(2 * IN) * dpi, IN * dpi])
function sideW (side) {
const frontOffset = Math.round((backOW - frontOW) / 2)
const X = -backOW / 2
const Y = -length / 2
let foldIN = X + ((backOW - backIW) / 2) //inside
let foldOUT = X //outside
let t
let t2
//sides
if (side === 1 || parts === 4 || (parts === 2 && side === 3)) {
line(ctx, BLACK, [X, Y], [X + frontOffset, Y + length])
}
if (parts === 4) {
line(ctx, BLACK, [X + backOW, Y], [X + backOW - frontOffset, Y + length])
}
//inner
//line(ctx, BLACK, [X + ((backOW - backIW) / 2), Y], [X + ((backOW - backIW) / 2) + frontOffset, Y + length])
//line(ctx, BLACK, [X + backOW - ((backOW - backIW) / 2), Y], [X + ((backOW - backIW) / 2) + frontIW + frontOffset, Y + length])
//top
line(ctx, BLACK, [X, Y], [X + backOW, Y])
//bottom
line(ctx, BLACK, [X + frontOffset, Y + length], [X + frontOW + frontOffset, Y + length])
//fold lines
for (let i = 1; i < folds; i++) {
if (i % 2 === 0) {
if (backIW !== frontIW) {
t = taper(backIW, frontIW, length, i * fold)
} else {
t = 0
}
//fold out
dash(ctx, '255,0,0,1.0', dpi, [foldIN + t, Y + (i * fold)], [foldIN + backIW - t, Y + (i * fold)])
} else {
if (backOW !== frontOW) {
t = taper(backOW, frontOW, length, i * fold)
} else {
t = 0
}
//fold in
dash(ctx, '0,0,255,1.0', dpi, [foldOUT + t, Y + (i * fold)], [foldOUT + backOW - t, Y + (i * fold)])
}
}
//fold corners, left
for (let i = 0; i < folds; i++) {
if (i % 2 === 0) {
if (backIW !== frontIW) {
t = taper(backIW, frontIW, length, i * fold)
t2 = taper(backIW, frontIW, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldIN + t, Y + i * fold ], [foldIN - ((backOW - backIW) / 2) + t2, (Y + (i + 1) * fold) ])
} else {
if (backIW !== frontIW) {
t = taper(backIW, frontIW, length, i * fold)
t2 = taper(backIW, frontIW, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldOUT + t, Y + i * fold ], [foldIN + t2, (Y + (i + 1) * fold) ])
}
//
}
//fold corners, right
for (let i = 0; i < folds; i++) {
if (i % 2 === 0) {
if (backIW !== frontIW) {
t = taper(backIW, frontIW, length, i * fold)
t2 = taper(backIW, frontIW, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldIN + backIW - t, Y + i * fold ], [foldOUT + backOW - t2, (Y + (i + 1) * fold) ])
} else {
if (backIW !== frontIW) {
t = taper(backIW, frontIW, length, i * fold)
t2 = taper(backIW, frontIW, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldOUT + backOW - t, Y + i * fold ], [foldIN + backIW - t2, (Y + (i + 1) * fold) ])
}
//
}
}
function sideH (side) {
const frontOffset = Math.round((backOH - frontOH) / 2)
const X = -backOH / 2
const Y = -length / 2
let foldIN = X + ((backOH - backIH) / 2) //inside
let foldOUT = X //outside
let t
let t2
//sides
if (parts === 4) {
line(ctx, BLACK, [X, Y], [X + frontOffset, Y + length])
}
if (parts !== 1 || side === 4) {
line(ctx, BLACK, [X + backOH, Y], [X + backOH - frontOffset, Y + length])
}
//inner
//line(ctx, BLACK, [X + ((backOH - backIH) / 2), Y], [X + ((backOH - backIH) / 2) + frontOffset, Y + length])
//line(ctx, BLACK, [X + backOH - ((backOH - backIH) / 2), Y], [X + ((backOH - backIH) / 2) + frontIH + frontOffset, Y + length])
//top
line(ctx, BLACK, [X, Y], [X + backOH, Y])
//bottom
line(ctx, BLACK, [X + frontOffset, Y + length], [X + frontOH + frontOffset, Y + length])
//fold lines
for (let i = 1; i < folds; i++) {
if (i % 2 !== 0) {
if (backIH !== frontIH) {
t = taper(backIH, frontIH, length, i * fold)
} else {
t = 0
}
//fold out
dash(ctx, '255,0,0,1.0', dpi, [foldIN + t, Y + (i * fold)], [foldIN + backIH - t, Y + (i * fold)])
} else {
if (backOH !== frontOH) {
t = taper(backOH, frontOH, length, i * fold)
} else {
t = 0
}
//fold in
dash(ctx, '0,0,255,1.0', dpi, [foldOUT + t, Y + (i * fold)], [foldOUT + backOH - t, Y + (i * fold)])
}
}
//fold corners, left
for (let i = 0; i < folds; i++) {
if (i % 2 !== 0) {
if (backIH !== frontIH) {
t = taper(backIH, frontIH, length, i * fold)
t2 = taper(backIH, frontIH, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldIN + t, Y + i * fold ], [foldIN - ((backOH - backIH) / 2) + t2, (Y + (i + 1) * fold) ])
} else {
if (backIH !== frontIH) {
t = taper(backIH, frontIH, length, i * fold)
t2 = taper(backIH, frontIH, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldOUT + t, Y + i * fold ], [foldIN + t2, (Y + (i + 1) * fold) ])
}
//
}
//fold corners, right
for (let i = 0; i < folds; i++) {
if (i % 2 !== 0) {
if (backIH !== frontIH) {
t = taper(backIH, frontIH, length, i * fold)
t2 = taper(backIH, frontIH, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldIN + backIH - t, Y + i * fold ], [foldOUT + backOH - t2, (Y + (i + 1) * fold) ])
} else {
if (backIH !== frontIH) {
t = taper(backIH, frontIH, length, i * fold)
t2 = taper(backIH, frontIH, length, (i + 1) * fold)
} else {
t = 0
t2 = 0
}
dash(ctx, '255,0,0,1.0', dpi, [foldOUT + backOH - t, Y + i * fold ], [foldIN + backIH - t2, (Y + (i + 1) * fold) ])
}
}
}
function overlap () {
}
ctx.translate(centerX, centerY)
angleW = Math.atan( (backOW - frontOW) / length) // / (Math.PI / 180)
angleH = Math.atan( (backOH - frontOH) / length)
//SIDES
ctx.translate(-backOW - (backOH / 2), 0)
ctx.rotate((angleW + angleH) / 2)
sideW(1)
if (parts === 4) {
ctx.translate(backOH - backIH, 0)
}
ctx.translate((backOW / 2) + (backOH / 2) - (((backOW - frontIW) + (backOH - frontIH)) / 4), align)
ctx.rotate((angleW + angleH) / 2)
sideH(2)
if (parts === 4 || parts === 2) {
ctx.translate(backOW - backIW, 0)
}
ctx.translate((backOW / 2) + (backOH / 2) - (((backOW - frontIW) + (backOH - frontIH)) / 4), align)
ctx.rotate((angleW + angleH) / 2)
sideW(3)
if (parts === 4) {
ctx.translate(backOH - backIH, 0)
}
ctx.translate((backOW / 2) + (backOH / 2) - (((backOW - frontIW) + (backOH - frontIH)) / 4), align)
ctx.rotate((angleW + angleH) / 2)
sideH(4)
return canvas.toDataURL()
}
if (!browser) {
module.exports = bellows
}