From 70c0161caf8fa0f856fb5a4dab2f276906085b0a Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Wed, 27 Dec 2017 16:43:55 -0500 Subject: [PATCH] Created bellows library --- .gitignore | 2 + .npmignore | 2 + README.md | 38 +++++ example/index.html | 181 +++++++++++++++++++++ example/index.js | 26 ++++ package-lock.json | 21 +++ package.json | 29 ++++ src/bellows.js | 381 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 680 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 README.md create mode 100644 example/index.html create mode 100644 example/index.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/bellows.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ebb9cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +example/bellows.png diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6ebb9cb --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +node_modules +example/bellows.png diff --git a/README.md b/README.md new file mode 100644 index 0000000..9af2760 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ + + +## bellows +**Example** +```js +const bellows = require('bellows') +``` + + +### bellows~bellows([options]) +Generate bellows pattern for cutting and folding. + +**Kind**: inner method of [bellows](#module_bellows) + +| Param | Type | Description | +| --- | --- | --- | +| [options] | Object | Bellows configuration options | +| [options.dpi] | Integer | DPI of the image | +| [options.pageW] | Integer | Page width in pixels | +| [options.pageH] | Integer | Page height in pixels | +| [options.frontIW] | Integer | Front inner width of bellows in pixels | +| [options.frontOW] | Integer | Front outer width of bellows in pixels | +| [options.frontIH] | Integer | Front inner height of bellows in pixels | +| [options.frontOH] | Integer | Front outer height of bellows in pixels | +| [options.backIW] | Integer | Back inner width of bellows in pixels | +| [options.backOW] | Integer | Back outer width of bellows in pixels | +| [options.backIH] | Integer | Back inner height of bellows in pixels | +| [options.backOH] | Integer | Back outer height of bellows in pixels | +| [options.maxLength] | Integer | Maximum length of bellows in pixels | +| [options.align] | Integer | Vertical alignment adjustment in pixels | +| [options.parts] | Integer | 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) +``` diff --git a/example/index.html b/example/index.html new file mode 100644 index 0000000..5f432fb --- /dev/null +++ b/example/index.html @@ -0,0 +1,181 @@ + + + +Bellows Browser Example + + + +

+ Bellows Pattern Generator +

+
+ View Source +
+
+
+
+ + mm +
+
+ + mm +
+
+ + mm +
+
+ + mm +
+
+
+
+ + mm +
+
+ + mm +
+
+ + mm +
+
+ + mm +
+
+
+
+ + in +
+
+ + in +
+
+ + +
+
+ + mm +
+
+ + 1, 2, or 4 +
+
+ + +
+
+
+
+ + +
+ + + + + { + 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) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b47b088 --- /dev/null +++ b/package-lock.json @@ -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=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..800b430 --- /dev/null +++ b/package.json @@ -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" +} diff --git a/src/bellows.js b/src/bellows.js new file mode 100644 index 0000000..6b59d91 --- /dev/null +++ b/src/bellows.js @@ -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 +} \ No newline at end of file