p5bezier 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/p5.bezier.min.js +3 -1
- package/lib/p5.bezier.min.js.LICENSE.txt +10 -0
- package/lib/p5.bezier.min.js.map +1 -0
- package/package.json +13 -9
- package/.eslintrc.json +0 -16
- package/.github/workflows/npm-publish.yml +0 -33
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
- package/.vscode/settings.json +0 -3
- package/examples/basic/index.html +0 -13
- package/examples/basic/sketch.js +0 -23
- package/examples/basic/style.css +0 -17
- package/examples/basic_object/index.html +0 -13
- package/examples/basic_object/sketch.js +0 -27
- package/examples/basic_object/style.css +0 -17
- package/examples/control_points/index.html +0 -13
- package/examples/control_points/sketch.js +0 -92
- package/examples/control_points/style.css +0 -17
- package/examples/fidelity/index.html +0 -13
- package/examples/fidelity/sketch.js +0 -48
- package/examples/fidelity/style.css +0 -17
- package/examples/package.json +0 -14
- package/examples/server.js +0 -16
- package/examples/shortest_point/index.html +0 -13
- package/examples/shortest_point/sketch.js +0 -41
- package/examples/shortest_point/style.css +0 -17
- package/img/cover.jpg +0 -0
- package/img/p5bezier.jpg +0 -0
- package/src/p5.bezier.js +0 -395
- package/webpack.config.js +0 -11
package/lib/p5.bezier.min.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
/*! For license information please see p5.bezier.min.js.LICENSE.txt */
|
|
2
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.p5bezier=e():t.p5bezier=e()}(self,(function(){return(()=>{"use strict";var t={d:(e,i)=>{for(var r in i)t.o(i,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:i[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{initBezier:()=>h,newBezier:()=>l,newBezierObj:()=>a});const i=[.2,.1,.05,.04,.02,.01,.008,.002,.001,5e-4,1e-4];let r,s,o,n;function h(t,e=!1){r=t,s=r.drawingContext,o=t.isP3D?3:2,n=e}function l(t,e="OPEN",r=7){if(n&&!Array.isArray(t))throw typeof t;const h=i[r];if(0===o);else{if(n)for(let e of t)if(!Array.isArray(t)||e.length!==o)throw"One or more points in the array are not input correctly.";"CLOSE"===e&&t.push(t[0]);let i=t.length-1;if(s.beginPath(),s.moveTo(...t[0]),2===o){let e,r,o,n;for(o=0;o<=1;o+=h){for(e=0,r=0,n=0;n<=i;n++)e+=f(i)/(f(n)*f(i-n))*Math.pow(1-o,i-n)*Math.pow(o,n)*t[n][0],r+=f(i)/(f(n)*f(i-n))*Math.pow(1-o,i-n)*Math.pow(o,n)*t[n][1];s.lineTo(e,r)}s.lineTo(...t.slice(-1)[0])}else if(3===o){let e,r,o,n=[0,0,0];for(e=0;e<=1;e+=h){for(n=[0,0,0],r=0;r<=i;r++)for(o=0;o<3;o++)n[o]+=f(i)/(f(r)*f(i-r))*Math.pow(1-e,i-r)*Math.pow(e,r)*t[r][o];s.lineTo(...n)}s.lineTo(...t.slice(-1)[0])}if("CLOSE"===e)s.closePath();else if(n&&"OPEN"!==e)throw"Close Type Error. A bezier curve can only be either OPEN or CLOSE.";p()}}function a(t,e="OPEN",r=7){const s=i[r];if(n&&!Array.isArray(t))throw typeof t;if(n)for(let e of t)if(!Array.isArray(t)||e.length!==o)throw"One or more points in the array are not input correctly.";return new d(t,e,s,o)}function f(t){return t>1?t*f(t-1):1}function c(){return 4===arguments.length?Math.hypot(arguments[0]-arguments[2],arguments[1]-arguments[3]):6===arguments.length?Math.hypot(arguments[0]-arguments[3],arguments[1]-arguments[4],arguments[2]-arguments[5]):0}function p(){r._doFill&&s.fill(),r._doStroke&&s.stroke()}class d{constructor(t,e,i,r,s=null){if(n&&2!==r&&3!==r)throw r;if(this.controlPoints=t,"CLOSE"===e)this.controlPoints.push(t[0]),this.closeType="CLOSE";else{if("OPEN"!==e)throw"Close Type Error. A bezier curve can only be either OPEN or CLOSE.";this.closeType="OPEN"}this.dimension=r,this.increment=i,this.vertexList=[],this.vertexListLen=0,this.p=this.controlPoints.length,this.n=this.p-1,null===s?this._buildVertexList():(this.vertexList=s,this.vertexListLen=this.vertexList.length)}_buildVertexList(){if(this.vertexList=[],2===this.dimension){let t,e,i,r;for(i=0;i<=1;i+=this.increment){for(t=0,e=0,r=0;r<=this.n;r++)t+=f(this.n)/(f(r)*f(this.n-r))*Math.pow(1-i,this.n-r)*Math.pow(i,r)*this.controlPoints[r][0],e+=f(this.n)/(f(r)*f(this.n-r))*Math.pow(1-i,this.n-r)*Math.pow(i,r)*this.controlPoints[r][1];this.vertexList.push([t,e])}}else if(3===this.dimension){let t,e,i,r=[0,0,0];for(t=0;t<=1;t+=this.increment){for(r[0]=0,r[1]=0,r[2]=0,e=0;e<=this.n;e++)for(i=0;i<3;i++)r[i]+=f(this.n)/(f(e)*f(this.n-e))*Math.pow(1-t,this.n-e)*Math.pow(t,e)*this.controlPoints[e][i];this.vertexList.push(r)}}return this._addVertex(this.controlPoints.slice(-1)[0]),this.dimension=this.vertexList[0].length,this.vertexListLen=this.vertexList.length,this.vertexList}_addVertex(t){if(2!==this.dimension&&3!==this.dimension)throw"Vertices can only be in 2D or 3D space.";s.lineTo(...t)}_distVertex(t,e){return 2===this.dimension?c(t[0],t[1],e[0],e[1]):3===this.dimension?c(t[0],t[1],t[2],e[0],e[1],e[2]):void 0}draw(t){if(t){if(!(Array.isArray(t)&&2===t.length&&this.increment<=.008))throw this.increment>.008?"Fidelity is too low for a dash line. It should be at least 6.":"Your dash array input is not valid. Make sure it's an array of two numbers.";{let e=Math.abs(t[0]),i=e+Math.abs(t[1]),r=0,o=0,n=this.vertexList[0],h=!0;s.save(),s.fillStyle="rgba(0, 0, 0, 0)",s.beginPath(),s.moveTo(...this.vertexList[0]);for(let t=1;t<this.vertexListLen;t++)r+=this._distVertex(n,this.vertexList[t]),o=r%i,o<=e&&h?this._addVertex(this.vertexList[t]):o>e&&o<=i&&h?h=!1:o<=e&&!h&&(s.moveTo(...this.vertexList[t]),h=!0),n=this.vertexList[t];p(),s.restore()}}else{s.beginPath();for(let t of this.vertexList)this._addVertex(t);"CLOSE"===this.closeType&&s.closePath(),p()}}update(t){if(t.length!==this.controlPoints.length)throw"The number of points changed. (Keep the point array length the same.)";u(this.controlPoints,t)||(this.controlPoints=t,this._buildVertexList())}move(t,e,i=null,r=!0,s=0){if(null===i&&3===this.dimension)throw"To move a 3D curve, please specify (x, y, z).";{let o=[t,e];null!==i&&o.push(i);let n=[];for(let t=0;t<this.vertexListLen;t++)n.push(this.vertexList[t].slice());let h=new d(this.controlPoints,this.closeType,this.increment,this.dimension,n);for(let t=0;t<h.vertexListLen;t++)for(let e=0;e<h.dimension;e++)h.vertexList[t][e]+=o[e];return r&&h.draw(s),h}}shortest(t,e,i=0){let r,s=-1,o=0;for(let n of this.vertexList)-1===s?(s=this._distVertex(n,[t,e,i]),r=n):(o=this._distVertex(n,[t,e,i]),s>o&&(s=o,r=n));return r}}const u=(t,e)=>t.length===e.length&&t.every(((t,i)=>t===e[i]));return e})()}));
|
|
3
|
+
//# sourceMappingURL=p5.bezier.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"p5.bezier.min.js","mappings":";CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAkB,SAAID,IAEtBD,EAAe,SAAIC,IARrB,CASGK,MAAM,WACT,yBCTA,IAAIC,EAAsB,CCA1BA,EAAwB,CAACL,EAASM,KACjC,IAAI,IAAIC,K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AnB9B,KAAKE,UAErB,KAAM,gDACD,CACL,IAAI8B,EAAS,CAAC7D,EAAGC,GACP,OAAN0D,GAAYE,EAAOjE,KAAK+D,GAE5B,IAAIG,EAAY,GAChB,IAAK,IAAI3D,EAAI,EAAGA,EAAI0B,KAAKK,cAAe/B,IACtC2D,EAAUlE,KAAKiC,KAAKI,WAAW9B,GAAGK,SACpC,IAAIuD,EAAc,IAAIjD,EACpBe,KAAKC,cACLD,KAAKxC,UACLwC,KAAKG,UACLH,KAAKE,UACL+B,GAGF,IAAK,IAAI3D,EAAI,EAAGA,EAAI4D,EAAY7B,cAAe/B,IAC7C,IAAK,IAAI6D,EAAI,EAAGA,EAAID,EAAYhC,UAAWiC,IACzCD,EAAY9B,WAAW9B,GAAG6D,IAAMH,EAAOG,GAM3C,OAHIJ,GACFG,EAAYrB,KAAKC,GAEZoB,GAIXE,SAASC,EAAIC,EAAIC,EAAK,GAIpB,IAEIC,EAFAC,GAAQ,EACVC,EAAS,EAEX,IAAK,IAAIlB,KAAKxB,KAAKI,YACH,IAAVqC,GACFA,EAAOzC,KAAKU,YAAYc,EAAG,CAACa,EAAIC,EAAIC,IACpCC,EAAYhB,IAEZkB,EAAS1C,KAAKU,YAAYc,EAAG,CAACa,EAAIC,EAAIC,IAClCE,EAAOC,IACTD,EAAOC,EACPF,EAAYhB,IAIlB,OAAOgB,GAOX,MAAMZ,EAAc,CAAC1C,EAAGyD,IACtBzD,EAAEpB,SAAW6E,EAAE7E,QAAUoB,EAAE0D,OAAM,CAACpB,EAAGlD,IAAMkD,IAAMmB,EAAErE,eLhYrD","sources":["webpack://p5bezier/webpack/universalModuleDefinition","webpack://p5bezier/webpack/bootstrap","webpack://p5bezier/webpack/runtime/define property getters","webpack://p5bezier/webpack/runtime/hasOwnProperty shorthand","webpack://p5bezier/webpack/runtime/make namespace object","webpack://p5bezier/./src/p5.bezier.js"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"p5bezier\"] = factory();\n\telse\n\t\troot[\"p5bezier\"] = factory();\n})(self, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\np5.bezier library by Peiling Jiang\n2020\n*/\n\nconst p5bezierAccuracyListAll = [\n 0.2,\n 0.1,\n 0.05,\n 0.04,\n 0.02,\n 0.01,\n 0.008,\n 0.002,\n 0.001,\n 0.0005,\n 0.0001,\n]\n\nlet _canvas, _ctx, _dimension, _strict\n\nexport function initBezier(canvas, strictMode = false) {\n _canvas = canvas\n _ctx = _canvas.drawingContext\n _dimension = canvas.isP3D ? 3 : 2\n _strict = strictMode // Always check and throw errors or not\n}\n\nexport function newBezier(pointList, closeType = 'OPEN', accuracy = 7) {\n if (_strict && !Array.isArray(pointList))\n throw ('newBezier() function expects an array, got %s.', typeof pointList)\n\n // Define the increment of t based on accuracy\n const tIncrement = p5bezierAccuracyListAll[accuracy]\n\n if (_dimension !== 0) {\n // Check if all points are valid\n if (_strict)\n for (let point of pointList)\n if (!Array.isArray(pointList) || point.length !== _dimension)\n throw 'One or more points in the array are not input correctly.'\n\n // Add the first point as the last point to close the curve\n if (closeType === 'CLOSE') pointList.push(pointList[0])\n let p = pointList.length // pointList has p points for (p - 1) degree curves\n let n = p - 1\n\n _ctx.beginPath()\n _ctx.moveTo(...pointList[0])\n // Are we drawing 2D or 3D curves\n if (_dimension === 2) {\n // 2-Dimensional bezier curve\n let x, y, t, i\n for (t = 0; t <= 1; t += tIncrement) {\n x = 0\n y = 0\n for (i = 0; i <= n; i++) {\n // i point in pointList\n x +=\n (_helper_factorial(n) /\n (_helper_factorial(i) * _helper_factorial(n - i))) *\n Math.pow(1 - t, n - i) *\n Math.pow(t, i) *\n pointList[i][0]\n y +=\n (_helper_factorial(n) /\n (_helper_factorial(i) * _helper_factorial(n - i))) *\n Math.pow(1 - t, n - i) *\n Math.pow(t, i) *\n pointList[i][1]\n }\n _ctx.lineTo(x, y)\n }\n _ctx.lineTo(...pointList.slice(-1)[0])\n } else if (_dimension === 3) {\n // 3-Dimensional bezier curve\n let xyz = [0, 0, 0],\n t,\n i,\n d\n for (t = 0; t <= 1; t += tIncrement) {\n xyz = [0, 0, 0]\n for (i = 0; i <= n; i++) {\n for (d = 0; d < 3; d++) {\n xyz[d] +=\n (_helper_factorial(n) /\n (_helper_factorial(i) * _helper_factorial(n - i))) *\n Math.pow(1 - t, n - i) *\n Math.pow(t, i) *\n pointList[i][d]\n }\n }\n _ctx.lineTo(...xyz)\n }\n _ctx.lineTo(...pointList.slice(-1)[0])\n }\n\n if (closeType === 'CLOSE') _ctx.closePath()\n else if (_strict && closeType !== 'OPEN')\n throw 'Close Type Error. A bezier curve can only be either OPEN or CLOSE.'\n\n _helper_style()\n\n return\n }\n}\n\nexport function newBezierObj(pointList, closeType = 'OPEN', accuracy = 7) {\n // Define the increment of t based on accuracy\n const tIncrement = p5bezierAccuracyListAll[accuracy]\n\n if (_strict && !Array.isArray(pointList))\n throw ('newBezier() function expects an array, got %s.', typeof pointList)\n\n // Check if all points are valid\n if (_strict)\n for (let point of pointList)\n if (!Array.isArray(pointList) || point.length !== _dimension)\n throw 'One or more points in the array are not input correctly.'\n\n // All checks done\n let bObj = new BezierCurve(pointList, closeType, tIncrement, _dimension)\n return bObj\n}\n\nfunction _helper_factorial(a) {\n // Factorial function for binomial coefficient calculation\n return a > 1 ? a * _helper_factorial(a - 1) : 1\n}\n\nfunction _helper_dist() {\n if (arguments.length === 4)\n return Math.hypot(arguments[0] - arguments[2], arguments[1] - arguments[3])\n else if (arguments.length === 6)\n return Math.hypot(\n arguments[0] - arguments[3],\n arguments[1] - arguments[4],\n arguments[2] - arguments[5]\n )\n return 0\n}\n\nfunction _helper_style() {\n if (_canvas._doFill) _ctx.fill()\n if (_canvas._doStroke) _ctx.stroke()\n}\n\nclass BezierCurve {\n // Take pointList, closeType, tIncrement, bezierDimension into constructor\n constructor(pL, closeT, tI, bD, vL = null) {\n if (_strict && bD !== 2 && bD !== 3) {\n throw (\n (\"Dimension Error. The bezier curve is %d-dimensional and doesn't belong to our world.\",\n bD)\n )\n }\n this.controlPoints = pL\n\n if (closeT === 'CLOSE') {\n this.controlPoints.push(pL[0])\n this.closeType = 'CLOSE'\n } else if (closeT === 'OPEN') {\n this.closeType = 'OPEN'\n } else {\n throw 'Close Type Error. A bezier curve can only be either OPEN or CLOSE.'\n }\n\n this.dimension = bD\n this.increment = tI\n this.vertexList = []\n this.vertexListLen = 0\n this.p = this.controlPoints.length // Has p points for (p - 1) degree curves\n this.n = this.p - 1 // Degree\n // Calculate thr vertex\n if (vL === null) {\n this._buildVertexList()\n } else {\n this.vertexList = vL\n this.vertexListLen = this.vertexList.length\n }\n }\n\n _buildVertexList() {\n /*\n Return vertexList\n */\n this.vertexList = []\n if (this.dimension === 2) {\n // 2-Dimensional bezier curve\n let x, y, t, i\n for (t = 0; t <= 1; t += this.increment) {\n x = 0\n y = 0\n for (i = 0; i <= this.n; i++) {\n // i point in pointList\n x +=\n (_helper_factorial(this.n) /\n (_helper_factorial(i) * _helper_factorial(this.n - i))) *\n Math.pow(1 - t, this.n - i) *\n Math.pow(t, i) *\n this.controlPoints[i][0]\n y +=\n (_helper_factorial(this.n) /\n (_helper_factorial(i) * _helper_factorial(this.n - i))) *\n Math.pow(1 - t, this.n - i) *\n Math.pow(t, i) *\n this.controlPoints[i][1]\n }\n this.vertexList.push([x, y])\n }\n } else if (this.dimension === 3) {\n // 3-Dimensional bezier curve\n let xyz = [0, 0, 0],\n t,\n i,\n d\n for (t = 0; t <= 1; t += this.increment) {\n xyz[0] = 0\n xyz[1] = 0\n xyz[2] = 0\n for (i = 0; i <= this.n; i++) {\n for (d = 0; d < 3; d++) {\n xyz[d] +=\n (_helper_factorial(this.n) /\n (_helper_factorial(i) * _helper_factorial(this.n - i))) *\n Math.pow(1 - t, this.n - i) *\n Math.pow(t, i) *\n this.controlPoints[i][d]\n }\n }\n this.vertexList.push(xyz)\n }\n }\n // Ending fix\n this._addVertex(this.controlPoints.slice(-1)[0])\n\n this.dimension = this.vertexList[0].length // Update dimension\n this.vertexListLen = this.vertexList.length // Update vertexListLen\n return this.vertexList\n }\n\n _addVertex(vArray) {\n // vArray is an array of [x, y] position or [x, y, z] position\n if (this.dimension === 2 || this.dimension === 3) _ctx.lineTo(...vArray)\n else throw 'Vertices can only be in 2D or 3D space.'\n }\n\n _distVertex(vArray1, vArray2) {\n // Calculate the distance between\n // vertex_array_1 and vertex_array_2\n if (this.dimension === 2) {\n return _helper_dist(vArray1[0], vArray1[1], vArray2[0], vArray2[1])\n } else if (this.dimension === 3) {\n return _helper_dist(\n vArray1[0],\n vArray1[1],\n vArray1[2],\n vArray2[0],\n vArray2[1],\n vArray2[2]\n )\n }\n }\n\n draw(dash) {\n if (!dash) {\n _ctx.beginPath()\n for (let v of this.vertexList) {\n this._addVertex(v)\n }\n\n if (this.closeType === 'CLOSE') _ctx.closePath()\n _helper_style()\n } else if (\n Array.isArray(dash) &&\n dash.length === 2 &&\n this.increment <= 0.008\n ) {\n // Draw a dash curve\n let solidPart = Math.abs(dash[0]) // Length of one solid part\n let onePart = solidPart + Math.abs(dash[1]),\n nowLen = 0,\n modOnePart = 0\n let lastVertex = this.vertexList[0]\n let solid = true // true draw, false break\n\n _ctx.save() // push\n _ctx.fillStyle = 'rgba(0, 0, 0, 0)' // TODO: Enable fill\n _ctx.beginPath()\n _ctx.moveTo(...this.vertexList[0])\n for (let v = 1; v < this.vertexListLen; v++) {\n nowLen += this._distVertex(lastVertex, this.vertexList[v])\n modOnePart = nowLen % onePart\n if (modOnePart <= solidPart && solid) {\n this._addVertex(this.vertexList[v])\n } else if (modOnePart > solidPart && modOnePart <= onePart && solid) {\n // endShape()\n solid = false\n } else if (modOnePart <= solidPart && !solid) {\n _ctx.moveTo(...this.vertexList[v])\n solid = true\n }\n lastVertex = this.vertexList[v]\n }\n // if (solid) {\n // // Shape didn't end\n // endShape()\n // }\n _helper_style()\n _ctx.restore()\n } else if (this.increment > 0.008) {\n throw 'Fidelity is too low for a dash line. It should be at least 6.'\n } else {\n throw \"Your dash array input is not valid. Make sure it's an array of two numbers.\"\n }\n }\n\n update(newControlList) {\n /*\n Update the vertexList when control points change\n */\n if (newControlList.length !== this.controlPoints.length) {\n throw 'The number of points changed. (Keep the point array length the same.)'\n } else if (equalArrays(this.controlPoints, newControlList)) {\n // Do we really need to update? No.\n // return ;\n } else {\n this.controlPoints = newControlList\n this._buildVertexList()\n }\n }\n\n move(x, y, z = null, toDraw = true, dash = 0) {\n /*\n Move the curve to another place\n Return a new object\n */\n if (z === null && this.dimension === 3) {\n // A 3D curve treated as 2D error\n throw 'To move a 3D curve, please specify (x, y, z).'\n } else {\n let toMove = [x, y]\n if (z !== null) toMove.push(z)\n // Copy to a new object\n let newCurveV = []\n for (let i = 0; i < this.vertexListLen; i++)\n newCurveV.push(this.vertexList[i].slice())\n let newCurveObj = new BezierCurve(\n this.controlPoints,\n this.closeType,\n this.increment,\n this.dimension,\n newCurveV\n )\n // Move\n for (let i = 0; i < newCurveObj.vertexListLen; i++) {\n for (let j = 0; j < newCurveObj.dimension; j++) {\n newCurveObj.vertexList[i][j] += toMove[j]\n }\n }\n if (toDraw) {\n newCurveObj.draw(dash)\n }\n return newCurveObj\n }\n }\n\n shortest(pX, pY, pZ = 0) {\n // Return the point on curve that is closest to the point outside\n // Always return array length of 3\n // Last position (z) be 0 for all 2D calculation\n let dMin = -1,\n nowMin = 0\n let minVertex\n for (let v of this.vertexList) {\n if (dMin === -1) {\n dMin = this._distVertex(v, [pX, pY, pZ])\n minVertex = v\n } else {\n nowMin = this._distVertex(v, [pX, pY, pZ])\n if (dMin > nowMin) {\n dMin = nowMin\n minVertex = v\n }\n }\n }\n return minVertex // An array of vertex position\n }\n}\n\n/* --------------------------------- HELPERS -------------------------------- */\n\n// https://www.30secondsofcode.org/blog/s/javascript-array-comparison\nconst equalArrays = (a, b) =>\n a.length === b.length && a.every((v, i) => v === b[i])\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","p5bezierAccuracyListAll","_canvas","_ctx","_dimension","_strict","initBezier","canvas","strictMode","drawingContext","isP3D","newBezier","pointList","closeType","accuracy","Array","isArray","tIncrement","point","length","push","n","beginPath","moveTo","x","y","t","i","_helper_factorial","Math","pow","lineTo","slice","d","xyz","closePath","_helper_style","newBezierObj","BezierCurve","a","_helper_dist","arguments","hypot","_doFill","fill","_doStroke","stroke","constructor","pL","closeT","tI","bD","vL","this","controlPoints","dimension","increment","vertexList","vertexListLen","p","_buildVertexList","_addVertex","vArray","_distVertex","vArray1","vArray2","draw","dash","solidPart","abs","onePart","nowLen","modOnePart","lastVertex","solid","save","fillStyle","v","restore","update","newControlList","equalArrays","move","z","toDraw","toMove","newCurveV","newCurveObj","j","shortest","pX","pY","pZ","minVertex","dMin","nowMin","b","every"],"sourceRoot":""}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "p5bezier",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.8",
|
|
4
|
+
"description": "Bezier library for canvas-based graphics on the web, built to work with p5.js.",
|
|
5
5
|
"main": "lib/p5.bezier.min.js",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"p5.js",
|
|
@@ -19,12 +19,13 @@
|
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/peilingjiang/p5.bezier#readme",
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"eslint": "^
|
|
23
|
-
"husky": "^7.0.
|
|
24
|
-
"lint-staged": "^
|
|
25
|
-
"prettier": "^2.1
|
|
26
|
-
"webpack": "^5.
|
|
27
|
-
"webpack
|
|
22
|
+
"eslint": "^8.9.0",
|
|
23
|
+
"husky": "^7.0.4",
|
|
24
|
+
"lint-staged": "^12.3.4",
|
|
25
|
+
"prettier": "^2.5.1",
|
|
26
|
+
"terser-webpack-plugin": "^5.3.1",
|
|
27
|
+
"webpack": "^5.69.1",
|
|
28
|
+
"webpack-cli": "^4.9.2"
|
|
28
29
|
},
|
|
29
30
|
"scripts": {
|
|
30
31
|
"format": "prettier --write \"**/*.{js,json,md,html,css}\"",
|
|
@@ -34,5 +35,8 @@
|
|
|
34
35
|
"lint-staged": {
|
|
35
36
|
"*.js": "eslint --cache --fix",
|
|
36
37
|
"*.{js,json,md,html,css}": "prettier --write"
|
|
37
|
-
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"lib"
|
|
41
|
+
]
|
|
38
42
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es2021": true
|
|
5
|
-
},
|
|
6
|
-
"extends": "eslint:recommended",
|
|
7
|
-
"parserOptions": {
|
|
8
|
-
"ecmaVersion": 12,
|
|
9
|
-
"sourceType": "module"
|
|
10
|
-
},
|
|
11
|
-
"rules": {
|
|
12
|
-
"eqeqeq": "warn",
|
|
13
|
-
"no-unused-vars": "warn"
|
|
14
|
-
},
|
|
15
|
-
"ignorePatterns": ["examples/*", "*.min.js", "*.config.js"]
|
|
16
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Publish
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
release:
|
|
8
|
-
types: [created]
|
|
9
|
-
|
|
10
|
-
jobs:
|
|
11
|
-
build:
|
|
12
|
-
runs-on: ubuntu-latest
|
|
13
|
-
steps:
|
|
14
|
-
- uses: actions/checkout@v2
|
|
15
|
-
- uses: actions/setup-node@v1
|
|
16
|
-
with:
|
|
17
|
-
node-version: 12
|
|
18
|
-
- run: npm ci
|
|
19
|
-
- run: npm test
|
|
20
|
-
|
|
21
|
-
publish-npm:
|
|
22
|
-
needs: build
|
|
23
|
-
runs-on: ubuntu-latest
|
|
24
|
-
steps:
|
|
25
|
-
- uses: actions/checkout@v2
|
|
26
|
-
- uses: actions/setup-node@v1
|
|
27
|
-
with:
|
|
28
|
-
node-version: 12
|
|
29
|
-
registry-url: https://registry.npmjs.org/
|
|
30
|
-
- run: npm ci
|
|
31
|
-
- run: npm publish
|
|
32
|
-
env:
|
|
33
|
-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/.husky/pre-commit
DELETED
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/.vscode/settings.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/p5bezier@latest/lib/p5.bezier.min.js"></script>
|
|
6
|
-
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
7
|
-
<meta charset="utf-8" />
|
|
8
|
-
</head>
|
|
9
|
-
|
|
10
|
-
<body>
|
|
11
|
-
<script src="sketch.js"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
package/examples/basic/sketch.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier.js basic example
|
|
3
|
-
Peiling Jiang
|
|
4
|
-
NYU ITP/IMA 2020
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
function setup() {
|
|
8
|
-
let c = createCanvas(500, 500)
|
|
9
|
-
p5bezier.initBezier(c)
|
|
10
|
-
noFill()
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function draw() {
|
|
14
|
-
background(235)
|
|
15
|
-
strokeWeight(2)
|
|
16
|
-
p5bezier.newBezier([
|
|
17
|
-
[10, 10],
|
|
18
|
-
[100, 700],
|
|
19
|
-
[500, -800],
|
|
20
|
-
[800, 1000],
|
|
21
|
-
[10, 300],
|
|
22
|
-
])
|
|
23
|
-
}
|
package/examples/basic/style.css
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* style.css for all examples */
|
|
2
|
-
|
|
3
|
-
html,
|
|
4
|
-
body {
|
|
5
|
-
height: 100%;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
body {
|
|
9
|
-
margin: 0;
|
|
10
|
-
display: flex;
|
|
11
|
-
|
|
12
|
-
/* This centers our sketch horizontally. */
|
|
13
|
-
justify-content: center;
|
|
14
|
-
|
|
15
|
-
/* This centers our sketch vertically. */
|
|
16
|
-
align-items: center;
|
|
17
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/p5bezier@latest/lib/p5.bezier.min.js"></script>
|
|
6
|
-
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
7
|
-
<meta charset="utf-8" />
|
|
8
|
-
</head>
|
|
9
|
-
|
|
10
|
-
<body>
|
|
11
|
-
<script src="sketch.js"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier.js basic_object example
|
|
3
|
-
Peiling Jiang
|
|
4
|
-
NYU ITP/IMA 2020
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
let bezierObject
|
|
8
|
-
|
|
9
|
-
function setup() {
|
|
10
|
-
let c = createCanvas(500, 500)
|
|
11
|
-
p5bezier.initBezier(c)
|
|
12
|
-
bezierObject = p5bezier.newBezierObj([
|
|
13
|
-
[10, 10],
|
|
14
|
-
[100, 700],
|
|
15
|
-
[500, -800],
|
|
16
|
-
[800, 1000],
|
|
17
|
-
[10, 300],
|
|
18
|
-
])
|
|
19
|
-
console.log(bezierObject)
|
|
20
|
-
noFill()
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function draw() {
|
|
24
|
-
background(235)
|
|
25
|
-
strokeWeight(2)
|
|
26
|
-
bezierObject.draw()
|
|
27
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* style.css for all examples */
|
|
2
|
-
|
|
3
|
-
html,
|
|
4
|
-
body {
|
|
5
|
-
height: 100%;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
body {
|
|
9
|
-
margin: 0;
|
|
10
|
-
display: flex;
|
|
11
|
-
|
|
12
|
-
/* This centers our sketch horizontally. */
|
|
13
|
-
justify-content: center;
|
|
14
|
-
|
|
15
|
-
/* This centers our sketch vertically. */
|
|
16
|
-
align-items: center;
|
|
17
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/p5bezier@latest/lib/p5.bezier.min.js"></script>
|
|
6
|
-
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
7
|
-
<meta charset="utf-8" />
|
|
8
|
-
</head>
|
|
9
|
-
|
|
10
|
-
<body>
|
|
11
|
-
<script src="sketch.js"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier.js control_points example
|
|
3
|
-
Peiling Jiang
|
|
4
|
-
NYU ITP/IMA 2020
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// DRAG THE POINTS AROUND!
|
|
8
|
-
|
|
9
|
-
let pa // a PointArray object
|
|
10
|
-
|
|
11
|
-
function setup() {
|
|
12
|
-
let c = createCanvas(windowWidth, windowHeight)
|
|
13
|
-
p5bezier.initBezier(c)
|
|
14
|
-
noFill()
|
|
15
|
-
|
|
16
|
-
pa = new PointArray()
|
|
17
|
-
pa.add(100, 100)
|
|
18
|
-
pa.add(400, 600)
|
|
19
|
-
pa.add(500, 400)
|
|
20
|
-
pa.add(600, 600)
|
|
21
|
-
pa.add(900, 100)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function draw() {
|
|
25
|
-
background(255)
|
|
26
|
-
stroke(color('#FD5E53'))
|
|
27
|
-
strokeWeight(3)
|
|
28
|
-
// Draw an open Bezier curve with fidelity of 9 (highest)
|
|
29
|
-
p5bezier.newBezier(pa.get(), 'OPEN', 9)
|
|
30
|
-
pa.display()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function mouseDragged() {
|
|
34
|
-
pa.update(mouseX, mouseY)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
class PointArray {
|
|
38
|
-
constructor() {
|
|
39
|
-
this.pointArray = []
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
add(pX, pY) {
|
|
43
|
-
this.pointArray.push(new Point(pX, pY))
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
display() {
|
|
47
|
-
for (let point of this.pointArray) point.display()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
update(mX, mY) {
|
|
51
|
-
for (let point of this.pointArray) {
|
|
52
|
-
if (dist(mX, mY, point.position[0], point.position[1]) < 60)
|
|
53
|
-
point.update(mX, mY)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
get() {
|
|
58
|
-
// Return an array of array
|
|
59
|
-
// for p5bezier.newBezier function to use
|
|
60
|
-
let positionArray = []
|
|
61
|
-
// point is a Point object
|
|
62
|
-
for (let point of this.pointArray) positionArray.push(point.position)
|
|
63
|
-
return positionArray
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class Point {
|
|
68
|
-
constructor(x, y) {
|
|
69
|
-
this.position = [x, y]
|
|
70
|
-
this.r = 10
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
display() {
|
|
74
|
-
push()
|
|
75
|
-
fill(255)
|
|
76
|
-
stroke(0)
|
|
77
|
-
strokeWeight(2)
|
|
78
|
-
ellipse(this.position[0], this.position[1], 2 * this.r, 2 * this.r)
|
|
79
|
-
fill(0)
|
|
80
|
-
noStroke()
|
|
81
|
-
text(
|
|
82
|
-
'(' + this.position[0] + ', ' + this.position[1] + ')',
|
|
83
|
-
this.position[0] + 10,
|
|
84
|
-
this.position[1] - 10
|
|
85
|
-
)
|
|
86
|
-
pop()
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
update(mX, mY) {
|
|
90
|
-
this.position = [mX, mY]
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* style.css for all examples */
|
|
2
|
-
|
|
3
|
-
html,
|
|
4
|
-
body {
|
|
5
|
-
height: 100%;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
body {
|
|
9
|
-
margin: 0;
|
|
10
|
-
display: flex;
|
|
11
|
-
|
|
12
|
-
/* This centers our sketch horizontally. */
|
|
13
|
-
justify-content: center;
|
|
14
|
-
|
|
15
|
-
/* This centers our sketch vertically. */
|
|
16
|
-
align-items: center;
|
|
17
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/p5bezier@latest/lib/p5.bezier.min.js"></script>
|
|
6
|
-
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
7
|
-
<meta charset="utf-8" />
|
|
8
|
-
</head>
|
|
9
|
-
|
|
10
|
-
<body>
|
|
11
|
-
<script src="sketch.js"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier.js fidelity example
|
|
3
|
-
Peiling Jiang
|
|
4
|
-
NYU ITP/IMA 2020
|
|
5
|
-
|
|
6
|
-
Color templet from colorhunt.co
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
let points = [
|
|
10
|
-
[0, 0],
|
|
11
|
-
[100, 1100],
|
|
12
|
-
[1500, -2200],
|
|
13
|
-
[1500, 2800],
|
|
14
|
-
[100, -500],
|
|
15
|
-
[0, 600],
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
function setup() {
|
|
19
|
-
let c = createCanvas(1000, 600)
|
|
20
|
-
p5bezier.initBezier(c)
|
|
21
|
-
noFill()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function draw() {
|
|
25
|
-
background(235)
|
|
26
|
-
strokeWeight(2)
|
|
27
|
-
stroke(color('#FFB99A'))
|
|
28
|
-
p5bezier.newBezier(points, 'OPEN', 0)
|
|
29
|
-
stroke(color('#FF6464'))
|
|
30
|
-
p5bezier.newBezier(points, 'OPEN', 1)
|
|
31
|
-
stroke(color('#DB3056'))
|
|
32
|
-
p5bezier.newBezier(points, 'OPEN', 3)
|
|
33
|
-
stroke(color('#851D41'))
|
|
34
|
-
p5bezier.newBezier(points, 'OPEN', 9)
|
|
35
|
-
|
|
36
|
-
// Caption
|
|
37
|
-
push()
|
|
38
|
-
noStroke()
|
|
39
|
-
fill(color('#FFB99A'))
|
|
40
|
-
text('Fidelity 0', 935, 540)
|
|
41
|
-
fill(color('#FF6464'))
|
|
42
|
-
text('Fidelity 1', 935, 555)
|
|
43
|
-
fill(color('#DB3056'))
|
|
44
|
-
text('Fidelity 3', 935, 570)
|
|
45
|
-
fill(color('#851D41'))
|
|
46
|
-
text('Fidelity 9', 935, 585)
|
|
47
|
-
pop()
|
|
48
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* style.css for all examples */
|
|
2
|
-
|
|
3
|
-
html,
|
|
4
|
-
body {
|
|
5
|
-
height: 100%;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
body {
|
|
9
|
-
margin: 0;
|
|
10
|
-
display: flex;
|
|
11
|
-
|
|
12
|
-
/* This centers our sketch horizontally. */
|
|
13
|
-
justify-content: center;
|
|
14
|
-
|
|
15
|
-
/* This centers our sketch vertically. */
|
|
16
|
-
align-items: center;
|
|
17
|
-
}
|
package/examples/package.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "examples",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "Examples made to demonstrate p5.bezier library.",
|
|
5
|
-
"main": "server.js",
|
|
6
|
-
"keywords": [
|
|
7
|
-
"p5.js"
|
|
8
|
-
],
|
|
9
|
-
"author": "Peiling Jiang",
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"express": "^4.17.1"
|
|
13
|
-
}
|
|
14
|
-
}
|
package/examples/server.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// Peiling Jiang
|
|
2
|
-
// NYU ITP/IMA 2020
|
|
3
|
-
|
|
4
|
-
// Create server
|
|
5
|
-
let port = process.env.PORT || 8000
|
|
6
|
-
let express = require('express')
|
|
7
|
-
let app = express()
|
|
8
|
-
let server = require('http')
|
|
9
|
-
.createServer(app)
|
|
10
|
-
.listen(port, function () {
|
|
11
|
-
console.log('Server listening at port: ', port)
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
// Take argv and process into address
|
|
15
|
-
var myArgs = process.argv.slice(2)
|
|
16
|
-
app.use(express.static(myArgs[0]))
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
|
|
5
|
-
<script src="https://cdn.jsdelivr.net/npm/p5bezier@latest/lib/p5.bezier.min.js"></script>
|
|
6
|
-
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
7
|
-
<meta charset="utf-8" />
|
|
8
|
-
</head>
|
|
9
|
-
|
|
10
|
-
<body>
|
|
11
|
-
<script src="sketch.js"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier.js shortest_point example
|
|
3
|
-
Peiling Jiang
|
|
4
|
-
NYU ITP/IMA 2020
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
let points = [
|
|
8
|
-
[0, 0],
|
|
9
|
-
[100, 1100],
|
|
10
|
-
[1500, -2200],
|
|
11
|
-
[1500, 2800],
|
|
12
|
-
[100, -500],
|
|
13
|
-
[0, 600],
|
|
14
|
-
]
|
|
15
|
-
let bezierObject
|
|
16
|
-
|
|
17
|
-
function setup() {
|
|
18
|
-
let c = createCanvas(1000, 600)
|
|
19
|
-
p5bezier.initBezier(c)
|
|
20
|
-
noFill()
|
|
21
|
-
strokeWeight(2)
|
|
22
|
-
|
|
23
|
-
bezierObject = new p5bezier.newBezierObj(points, 'OPEN', 9)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function draw() {
|
|
27
|
-
background(235)
|
|
28
|
-
// Draw the dash Bezier curve
|
|
29
|
-
bezierObject.draw([20, 5])
|
|
30
|
-
// Find the closest point and the shortest distance
|
|
31
|
-
let pointOnCurve = bezierObject.shortest(mouseX, mouseY)
|
|
32
|
-
let r = dist(mouseX, mouseY, pointOnCurve[0], pointOnCurve[1])
|
|
33
|
-
|
|
34
|
-
// Draw the line and ellipse from mouse to point
|
|
35
|
-
line(mouseX, mouseY, pointOnCurve[0], pointOnCurve[1])
|
|
36
|
-
push()
|
|
37
|
-
strokeWeight(1)
|
|
38
|
-
stroke(color('#F0134D'))
|
|
39
|
-
ellipse(mouseX, mouseY, 2 * r, 2 * r)
|
|
40
|
-
pop()
|
|
41
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* style.css for all examples */
|
|
2
|
-
|
|
3
|
-
html,
|
|
4
|
-
body {
|
|
5
|
-
height: 100%;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
body {
|
|
9
|
-
margin: 0;
|
|
10
|
-
display: flex;
|
|
11
|
-
|
|
12
|
-
/* This centers our sketch horizontally. */
|
|
13
|
-
justify-content: center;
|
|
14
|
-
|
|
15
|
-
/* This centers our sketch vertically. */
|
|
16
|
-
align-items: center;
|
|
17
|
-
}
|
package/img/cover.jpg
DELETED
|
Binary file
|
package/img/p5bezier.jpg
DELETED
|
Binary file
|
package/src/p5.bezier.js
DELETED
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
p5.bezier library by Peiling Jiang
|
|
3
|
-
2020
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const p5bezierAccuracyListAll = [
|
|
7
|
-
0.2,
|
|
8
|
-
0.1,
|
|
9
|
-
0.05,
|
|
10
|
-
0.04,
|
|
11
|
-
0.02,
|
|
12
|
-
0.01,
|
|
13
|
-
0.008,
|
|
14
|
-
0.002,
|
|
15
|
-
0.001,
|
|
16
|
-
0.0005,
|
|
17
|
-
0.0001,
|
|
18
|
-
]
|
|
19
|
-
|
|
20
|
-
let _canvas, _ctx, _dimension, _strict
|
|
21
|
-
|
|
22
|
-
export function initBezier(canvas, strictMode = false) {
|
|
23
|
-
_canvas = canvas
|
|
24
|
-
_ctx = _canvas.drawingContext
|
|
25
|
-
_dimension = canvas.isP3D ? 3 : 2
|
|
26
|
-
_strict = strictMode // Always check and throw errors or not
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function newBezier(pointList, closeType = 'OPEN', accuracy = 7) {
|
|
30
|
-
if (_strict && !Array.isArray(pointList))
|
|
31
|
-
throw ('newBezier() function expects an array, got %s.', typeof pointList)
|
|
32
|
-
|
|
33
|
-
// Define the increment of t based on accuracy
|
|
34
|
-
const tIncrement = p5bezierAccuracyListAll[accuracy]
|
|
35
|
-
|
|
36
|
-
if (_dimension !== 0) {
|
|
37
|
-
// Check if all points are valid
|
|
38
|
-
if (_strict)
|
|
39
|
-
for (let point of pointList)
|
|
40
|
-
if (!Array.isArray(pointList) || point.length !== _dimension)
|
|
41
|
-
throw 'One or more points in the array are not input correctly.'
|
|
42
|
-
|
|
43
|
-
// Add the first point as the last point to close the curve
|
|
44
|
-
if (closeType === 'CLOSE') pointList.push(pointList[0])
|
|
45
|
-
let p = pointList.length // pointList has p points for (p - 1) degree curves
|
|
46
|
-
let n = p - 1
|
|
47
|
-
|
|
48
|
-
_ctx.beginPath()
|
|
49
|
-
_ctx.moveTo(...pointList[0])
|
|
50
|
-
// Are we drawing 2D or 3D curves
|
|
51
|
-
if (_dimension === 2) {
|
|
52
|
-
// 2-Dimensional bezier curve
|
|
53
|
-
let x, y, t, i
|
|
54
|
-
for (t = 0; t <= 1; t += tIncrement) {
|
|
55
|
-
x = 0
|
|
56
|
-
y = 0
|
|
57
|
-
for (i = 0; i <= n; i++) {
|
|
58
|
-
// i point in pointList
|
|
59
|
-
x +=
|
|
60
|
-
(_helper_factorial(n) /
|
|
61
|
-
(_helper_factorial(i) * _helper_factorial(n - i))) *
|
|
62
|
-
Math.pow(1 - t, n - i) *
|
|
63
|
-
Math.pow(t, i) *
|
|
64
|
-
pointList[i][0]
|
|
65
|
-
y +=
|
|
66
|
-
(_helper_factorial(n) /
|
|
67
|
-
(_helper_factorial(i) * _helper_factorial(n - i))) *
|
|
68
|
-
Math.pow(1 - t, n - i) *
|
|
69
|
-
Math.pow(t, i) *
|
|
70
|
-
pointList[i][1]
|
|
71
|
-
}
|
|
72
|
-
_ctx.lineTo(x, y)
|
|
73
|
-
}
|
|
74
|
-
_ctx.lineTo(...pointList.slice(-1)[0])
|
|
75
|
-
} else if (_dimension === 3) {
|
|
76
|
-
// 3-Dimensional bezier curve
|
|
77
|
-
let xyz = [0, 0, 0],
|
|
78
|
-
t,
|
|
79
|
-
i,
|
|
80
|
-
d
|
|
81
|
-
for (t = 0; t <= 1; t += tIncrement) {
|
|
82
|
-
xyz = [0, 0, 0]
|
|
83
|
-
for (i = 0; i <= n; i++) {
|
|
84
|
-
for (d = 0; d < 3; d++) {
|
|
85
|
-
xyz[d] +=
|
|
86
|
-
(_helper_factorial(n) /
|
|
87
|
-
(_helper_factorial(i) * _helper_factorial(n - i))) *
|
|
88
|
-
Math.pow(1 - t, n - i) *
|
|
89
|
-
Math.pow(t, i) *
|
|
90
|
-
pointList[i][d]
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
_ctx.lineTo(...xyz)
|
|
94
|
-
}
|
|
95
|
-
_ctx.lineTo(...pointList.slice(-1)[0])
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (closeType === 'CLOSE') _ctx.closePath()
|
|
99
|
-
else if (_strict && closeType !== 'OPEN')
|
|
100
|
-
throw 'Close Type Error. A bezier curve can only be either OPEN or CLOSE.'
|
|
101
|
-
|
|
102
|
-
_helper_style()
|
|
103
|
-
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function newBezierObj(pointList, closeType = 'OPEN', accuracy = 7) {
|
|
109
|
-
// Define the increment of t based on accuracy
|
|
110
|
-
const tIncrement = p5bezierAccuracyListAll[accuracy]
|
|
111
|
-
|
|
112
|
-
if (_strict && !Array.isArray(pointList))
|
|
113
|
-
throw ('newBezier() function expects an array, got %s.', typeof pointList)
|
|
114
|
-
|
|
115
|
-
// Check if all points are valid
|
|
116
|
-
if (_strict)
|
|
117
|
-
for (let point of pointList)
|
|
118
|
-
if (!Array.isArray(pointList) || point.length !== _dimension)
|
|
119
|
-
throw 'One or more points in the array are not input correctly.'
|
|
120
|
-
|
|
121
|
-
// All checks done
|
|
122
|
-
let bObj = new BezierCurve(pointList, closeType, tIncrement, _dimension)
|
|
123
|
-
return bObj
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function _helper_factorial(a) {
|
|
127
|
-
// Factorial function for binomial coefficient calculation
|
|
128
|
-
return a > 1 ? a * _helper_factorial(a - 1) : 1
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function _helper_dist() {
|
|
132
|
-
if (arguments.length === 4)
|
|
133
|
-
return Math.hypot(arguments[0] - arguments[2], arguments[1] - arguments[3])
|
|
134
|
-
else if (arguments.length === 6)
|
|
135
|
-
return Math.hypot(
|
|
136
|
-
arguments[0] - arguments[3],
|
|
137
|
-
arguments[1] - arguments[4],
|
|
138
|
-
arguments[2] - arguments[5]
|
|
139
|
-
)
|
|
140
|
-
return 0
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function _helper_style() {
|
|
144
|
-
if (_canvas._doFill) _ctx.fill()
|
|
145
|
-
if (_canvas._doStroke) _ctx.stroke()
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
class BezierCurve {
|
|
149
|
-
// Take pointList, closeType, tIncrement, bezierDimension into constructor
|
|
150
|
-
constructor(pL, closeT, tI, bD, vL = null) {
|
|
151
|
-
if (_strict && bD !== 2 && bD !== 3) {
|
|
152
|
-
throw (
|
|
153
|
-
("Dimension Error. The bezier curve is %d-dimensional and doesn't belong to our world.",
|
|
154
|
-
bD)
|
|
155
|
-
)
|
|
156
|
-
}
|
|
157
|
-
this.controlPoints = pL
|
|
158
|
-
|
|
159
|
-
if (closeT === 'CLOSE') {
|
|
160
|
-
this.controlPoints.push(pL[0])
|
|
161
|
-
this.closeType = 'CLOSE'
|
|
162
|
-
} else if (closeT === 'OPEN') {
|
|
163
|
-
this.closeType = 'OPEN'
|
|
164
|
-
} else {
|
|
165
|
-
throw 'Close Type Error. A bezier curve can only be either OPEN or CLOSE.'
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
this.dimension = bD
|
|
169
|
-
this.increment = tI
|
|
170
|
-
this.vertexList = []
|
|
171
|
-
this.vertexListLen = 0
|
|
172
|
-
this.p = this.controlPoints.length // Has p points for (p - 1) degree curves
|
|
173
|
-
this.n = this.p - 1 // Degree
|
|
174
|
-
// Calculate thr vertex
|
|
175
|
-
if (vL === null) {
|
|
176
|
-
this._buildVertexList()
|
|
177
|
-
} else {
|
|
178
|
-
this.vertexList = vL
|
|
179
|
-
this.vertexListLen = this.vertexList.length
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
_buildVertexList() {
|
|
184
|
-
/*
|
|
185
|
-
Return vertexList
|
|
186
|
-
*/
|
|
187
|
-
this.vertexList = []
|
|
188
|
-
if (this.dimension === 2) {
|
|
189
|
-
// 2-Dimensional bezier curve
|
|
190
|
-
let x, y, t, i
|
|
191
|
-
for (t = 0; t <= 1; t += this.increment) {
|
|
192
|
-
x = 0
|
|
193
|
-
y = 0
|
|
194
|
-
for (i = 0; i <= this.n; i++) {
|
|
195
|
-
// i point in pointList
|
|
196
|
-
x +=
|
|
197
|
-
(_helper_factorial(this.n) /
|
|
198
|
-
(_helper_factorial(i) * _helper_factorial(this.n - i))) *
|
|
199
|
-
Math.pow(1 - t, this.n - i) *
|
|
200
|
-
Math.pow(t, i) *
|
|
201
|
-
this.controlPoints[i][0]
|
|
202
|
-
y +=
|
|
203
|
-
(_helper_factorial(this.n) /
|
|
204
|
-
(_helper_factorial(i) * _helper_factorial(this.n - i))) *
|
|
205
|
-
Math.pow(1 - t, this.n - i) *
|
|
206
|
-
Math.pow(t, i) *
|
|
207
|
-
this.controlPoints[i][1]
|
|
208
|
-
}
|
|
209
|
-
this.vertexList.push([x, y])
|
|
210
|
-
}
|
|
211
|
-
} else if (this.dimension === 3) {
|
|
212
|
-
// 3-Dimensional bezier curve
|
|
213
|
-
let xyz = [0, 0, 0],
|
|
214
|
-
t,
|
|
215
|
-
i,
|
|
216
|
-
d
|
|
217
|
-
for (t = 0; t <= 1; t += this.increment) {
|
|
218
|
-
xyz[0] = 0
|
|
219
|
-
xyz[1] = 0
|
|
220
|
-
xyz[2] = 0
|
|
221
|
-
for (i = 0; i <= this.n; i++) {
|
|
222
|
-
for (d = 0; d < 3; d++) {
|
|
223
|
-
xyz[d] +=
|
|
224
|
-
(_helper_factorial(this.n) /
|
|
225
|
-
(_helper_factorial(i) * _helper_factorial(this.n - i))) *
|
|
226
|
-
Math.pow(1 - t, this.n - i) *
|
|
227
|
-
Math.pow(t, i) *
|
|
228
|
-
this.controlPoints[i][d]
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
this.vertexList.push(xyz)
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
// Ending fix
|
|
235
|
-
this._addVertex(this.controlPoints.slice(-1)[0])
|
|
236
|
-
|
|
237
|
-
this.dimension = this.vertexList[0].length // Update dimension
|
|
238
|
-
this.vertexListLen = this.vertexList.length // Update vertexListLen
|
|
239
|
-
return this.vertexList
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
_addVertex(vArray) {
|
|
243
|
-
// vArray is an array of [x, y] position or [x, y, z] position
|
|
244
|
-
if (this.dimension === 2 || this.dimension === 3) _ctx.lineTo(...vArray)
|
|
245
|
-
else throw 'Vertices can only be in 2D or 3D space.'
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
_distVertex(vArray1, vArray2) {
|
|
249
|
-
// Calculate the distance between
|
|
250
|
-
// vertex_array_1 and vertex_array_2
|
|
251
|
-
if (this.dimension === 2) {
|
|
252
|
-
return _helper_dist(vArray1[0], vArray1[1], vArray2[0], vArray2[1])
|
|
253
|
-
} else if (this.dimension === 3) {
|
|
254
|
-
return _helper_dist(
|
|
255
|
-
vArray1[0],
|
|
256
|
-
vArray1[1],
|
|
257
|
-
vArray1[2],
|
|
258
|
-
vArray2[0],
|
|
259
|
-
vArray2[1],
|
|
260
|
-
vArray2[2]
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
draw(dash) {
|
|
266
|
-
if (!dash) {
|
|
267
|
-
_ctx.beginPath()
|
|
268
|
-
for (let v of this.vertexList) {
|
|
269
|
-
this._addVertex(v)
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (this.closeType === 'CLOSE') _ctx.closePath()
|
|
273
|
-
_helper_style()
|
|
274
|
-
} else if (
|
|
275
|
-
Array.isArray(dash) &&
|
|
276
|
-
dash.length === 2 &&
|
|
277
|
-
this.increment <= 0.008
|
|
278
|
-
) {
|
|
279
|
-
// Draw a dash curve
|
|
280
|
-
let solidPart = Math.abs(dash[0]) // Length of one solid part
|
|
281
|
-
let onePart = solidPart + Math.abs(dash[1]),
|
|
282
|
-
nowLen = 0,
|
|
283
|
-
modOnePart = 0
|
|
284
|
-
let lastVertex = this.vertexList[0]
|
|
285
|
-
let solid = true // true draw, false break
|
|
286
|
-
|
|
287
|
-
_ctx.save() // push
|
|
288
|
-
_ctx.fillStyle = 'rgba(0, 0, 0, 0)' // TODO: Enable fill
|
|
289
|
-
_ctx.beginPath()
|
|
290
|
-
_ctx.moveTo(...this.vertexList[0])
|
|
291
|
-
for (let v = 1; v < this.vertexListLen; v++) {
|
|
292
|
-
nowLen += this._distVertex(lastVertex, this.vertexList[v])
|
|
293
|
-
modOnePart = nowLen % onePart
|
|
294
|
-
if (modOnePart <= solidPart && solid) {
|
|
295
|
-
this._addVertex(this.vertexList[v])
|
|
296
|
-
} else if (modOnePart > solidPart && modOnePart <= onePart && solid) {
|
|
297
|
-
// endShape()
|
|
298
|
-
solid = false
|
|
299
|
-
} else if (modOnePart <= solidPart && !solid) {
|
|
300
|
-
_ctx.moveTo(...this.vertexList[v])
|
|
301
|
-
solid = true
|
|
302
|
-
}
|
|
303
|
-
lastVertex = this.vertexList[v]
|
|
304
|
-
}
|
|
305
|
-
// if (solid) {
|
|
306
|
-
// // Shape didn't end
|
|
307
|
-
// endShape()
|
|
308
|
-
// }
|
|
309
|
-
_helper_style()
|
|
310
|
-
_ctx.restore()
|
|
311
|
-
} else if (this.increment > 0.008) {
|
|
312
|
-
throw 'Fidelity is too low for a dash line. It should be at least 6.'
|
|
313
|
-
} else {
|
|
314
|
-
throw "Your dash array input is not valid. Make sure it's an array of two numbers."
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
update(newControlList) {
|
|
319
|
-
/*
|
|
320
|
-
Update the vertexList when control points change
|
|
321
|
-
*/
|
|
322
|
-
if (newControlList.length !== this.controlPoints.length) {
|
|
323
|
-
throw 'The number of points changed. (Keep the point array length the same.)'
|
|
324
|
-
} else if (equalArrays(this.controlPoints, newControlList)) {
|
|
325
|
-
// Do we really need to update? No.
|
|
326
|
-
// return ;
|
|
327
|
-
} else {
|
|
328
|
-
this.controlPoints = newControlList
|
|
329
|
-
this._buildVertexList()
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
move(x, y, z = null, toDraw = true, dash = 0) {
|
|
334
|
-
/*
|
|
335
|
-
Move the curve to another place
|
|
336
|
-
Return a new object
|
|
337
|
-
*/
|
|
338
|
-
if (z === null && this.dimension === 3) {
|
|
339
|
-
// A 3D curve treated as 2D error
|
|
340
|
-
throw 'To move a 3D curve, please specify (x, y, z).'
|
|
341
|
-
} else {
|
|
342
|
-
let toMove = [x, y]
|
|
343
|
-
if (z !== null) toMove.push(z)
|
|
344
|
-
// Copy to a new object
|
|
345
|
-
let newCurveV = []
|
|
346
|
-
for (let i = 0; i < this.vertexListLen; i++)
|
|
347
|
-
newCurveV.push(this.vertexList[i].slice())
|
|
348
|
-
let newCurveObj = new BezierCurve(
|
|
349
|
-
this.controlPoints,
|
|
350
|
-
this.closeType,
|
|
351
|
-
this.increment,
|
|
352
|
-
this.dimension,
|
|
353
|
-
newCurveV
|
|
354
|
-
)
|
|
355
|
-
// Move
|
|
356
|
-
for (let i = 0; i < newCurveObj.vertexListLen; i++) {
|
|
357
|
-
for (let j = 0; j < newCurveObj.dimension; j++) {
|
|
358
|
-
newCurveObj.vertexList[i][j] += toMove[j]
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
if (toDraw) {
|
|
362
|
-
newCurveObj.draw(dash)
|
|
363
|
-
}
|
|
364
|
-
return newCurveObj
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
shortest(pX, pY, pZ = 0) {
|
|
369
|
-
// Return the point on curve that is closest to the point outside
|
|
370
|
-
// Always return array length of 3
|
|
371
|
-
// Last position (z) be 0 for all 2D calculation
|
|
372
|
-
let dMin = -1,
|
|
373
|
-
nowMin = 0
|
|
374
|
-
let minVertex
|
|
375
|
-
for (let v of this.vertexList) {
|
|
376
|
-
if (dMin === -1) {
|
|
377
|
-
dMin = this._distVertex(v, [pX, pY, pZ])
|
|
378
|
-
minVertex = v
|
|
379
|
-
} else {
|
|
380
|
-
nowMin = this._distVertex(v, [pX, pY, pZ])
|
|
381
|
-
if (dMin > nowMin) {
|
|
382
|
-
dMin = nowMin
|
|
383
|
-
minVertex = v
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return minVertex // An array of vertex position
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/* --------------------------------- HELPERS -------------------------------- */
|
|
392
|
-
|
|
393
|
-
// https://www.30secondsofcode.org/blog/s/javascript-array-comparison
|
|
394
|
-
const equalArrays = (a, b) =>
|
|
395
|
-
a.length === b.length && a.every((v, i) => v === b[i])
|