diff --git a/package.json b/package.json index c50d171..ea8956b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A collection of snippets for creative coding", "main": "utils.js", "scripts": { - "rollup": "rollup --config src/rollup.config.js", + "rollup": "rollup --config src/rollup.config.mjs", "docs": "jsdoc2md --template src/README.hbs --files src/utils.js --separators --param-list-format list --property-list-format list --helper src/year.js> README.md", "build": "npm run rollup && npm run docs", "pretest": "npm run build", @@ -15,8 +15,7 @@ "node": ">=16.0.0 <20.0.0" }, "files": [ - "utils.js", - "README.md" + "utils.js" ], "banner": "'/*!*/'", "homepage": "https://github.com/panGenerator/utils#readme", @@ -33,6 +32,7 @@ }, "license": "MIT", "devDependencies": { + "@rollup/plugin-terser": "^0.4.3", "eslint": "^7.0.0", "eslint-config-standard": "^14.1.1", "eslint-plugin-import": "^2.20.2", @@ -42,7 +42,6 @@ "eslint-plugin-standard": "^4.0.1", "jsdoc-to-markdown": "^6.0.1", "mocha": "^7.1.2", - "rollup": "^2.26.11", - "rollup-plugin-terser": "^7.0.2" + "rollup": "^3.25.1" } } diff --git a/src/banner.js b/src/banner.mjs similarity index 58% rename from src/banner.js rename to src/banner.mjs index 0e75fd9..dee54b0 100644 --- a/src/banner.js +++ b/src/banner.mjs @@ -1,7 +1,8 @@ -const pkg = require('../package.json') +//const pkg = require('../package.json') +import pkg from '../package.json' assert { type: 'json' }; const year = new Date().getFullYear() -module.exports = (pluginFilename) => { +export default (pluginFilename) => { return `/*! * @license ${pkg.name} v${pkg.version}, Copyright © ${year} ${pkg.author} * Released under ${pkg.license} license diff --git a/src/rollup.config.js b/src/rollup.config.mjs similarity index 55% rename from src/rollup.config.js rename to src/rollup.config.mjs index 2016443..3091d8a 100644 --- a/src/rollup.config.js +++ b/src/rollup.config.mjs @@ -1,7 +1,7 @@ -const { terser } = require('rollup-plugin-terser') -const banner = require('./banner.js') +import terser from '@rollup/plugin-terser' +import banner from './banner.mjs' -module.exports = { +export default { input: 'src/main.js', output: { banner, @@ -10,4 +10,4 @@ module.exports = { format: 'umd' }, plugins: [terser()] -} +} \ No newline at end of file diff --git a/utils.js b/utils.js index 803494b..f06eb0f 100644 --- a/utils.js +++ b/utils.js @@ -1,6 +1,6 @@ /*! - * @license @pangenerator/utils v2.7.1, Copyright © 2023 panGenerator + * @license @pangenerator/utils v2.8.0, Copyright © 2023 panGenerator * Released under MIT license * https://github.com/panGenerator/utils#readme */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).utils=t()}(this,(function(){"use strict";const e=(e,t,s)=>e+(t-e)*s,t=(t,s,n)=>({x:e(t.x,s.x,n),y:e(t.y,s.y,n),z:e(t.z,s.z,n)}),s=e=>e*e,n=e=>{const t=Array(e.length);for(let s=0;se.replace(/([ĄĆĘŁŃÓŚŹŻąćęłńóśźż])/g,(function(e){return o[e]})),a=(e,t)=>s(t.x-e.x)+s(t.y-e.y)+(void 0!==e.z&&void 0!==t.z?s(t.z-e.z):0),l=(e,t,s)=>{const n=a(t,s);if(0===n)return a(e,t);const o=((e.x-t.x)*(s.x-t.x)+(e.y-t.y)*(s.y-t.y))/n;return a(e,o<0?t:o>1?s:{x:t.x+o*(s.x-t.x),y:t.y+o*(s.y-t.y)})},i=(e,t="-")=>r(e).replace(/[A-Z]/g,((e,s)=>{const n=e.toLowerCase();return s?t+n:n})).replace(/([-_ ]){1,}/g,t),c=e=>i(e,"_"),d=(e,t)=>-1!==t.indexOf(e),g=e=>{const t=[];t.push("#"+e.id);for(let s=0;s{var s=document.createElement("style");s.setAttribute("type","text/css"),s.innerHTML=e;var n=t.hasChildNodes()?t.children[0]:null;t.insertBefore(s,n)},u=(e,t)=>{const{[t]:s,...n}=e;return n};var h=Object.freeze({__proto__:null,map:(e,t,s,n,o)=>n+(o-n)*(e-t)/(s-t),clamp:(e,t,s)=>e>s?s:e(void 0===t&&(t=e,e=0),e+Math.random()*(t-e)),randomDir:()=>Math.random()>.5?1:-1,lerp:e,lerp3:t,lerpedPoints:(e,s,n)=>{const o=[],r=1/(n+1);for(let a=0;aMath.sqrt(a(e,t)),norm:(e,t,s)=>(e-t)/(s-t),degrees:e=>180*e/Math.PI,radians:e=>e*Math.PI/180,intersection:(e,t)=>{const s=t.x-e.x,n=t.y-e.y,o=Math.sqrt(n*n+s*s);if(o>e.r+t.r)return!1;if(o(Math.random().toString(36)+"00000000000000000").slice(2,e+2),timestampName:()=>{var e=6e4*(new Date).getTimezoneOffset();let t=new Date(Date.now()-e).toISOString().replace(/z|t/gi," ").trim().replace(/:/gi,"-");return t=t.substring(0,t.indexOf(".")),t},randomIndex:e=>Math.floor(Math.random()*e),copyArray:n,shuffleArray:e=>{const t=n(e);for(let e=t.length-1;e>0;e--){const s=Math.floor(Math.random()*(e+1));[t[e],t[s]]=[t[s],t[e]]}return t},filterUnique:e=>[...new Set(e)],lerpColor:(e,t,s)=>{var n=parseInt(e.replace(/#/g,""),16),o=n>>16,r=n>>8&255,a=255&n,l=parseInt(t.replace(/#/g,""),16);return"#"+((1<<24)+(o+s*((l>>16)-o)<<16)+(r+s*((l>>8&255)-r)<<8)+(a+s*((255&l)-a))|0).toString(16).slice(1)},precision:(e,t)=>Math.round(e*Math.pow(10,t))/Math.pow(10,t),loadJSON:(e,t)=>{const s=new XMLHttpRequest;s.overrideMimeType("application/json"),s.open("GET",e,!0),s.onreadystatechange=()=>{4===s.readyState&&200===s.status&&t(JSON.parse(s.responseText))},s.send(null)},removeDiacritics:r,removeNonAlphaNumeric:e=>e.replace(/[^A-Za-z0-9]/g,""),splitChunks:(e,t,s)=>{const n=e.split(new RegExp("(.{"+t.toString()+"})"));return s?n.filter((e=>e.length===t)):n.filter((e=>e.length>0))},getQuarter:e=>[(e=e||new Date).getFullYear(),Math.floor(e.getMonth()/3)+1],quarterExtent:(e,t)=>[new Date(`${t}-${(3*(e-1)+1).toString().padStart(2,"0")}-01`),new Date(`${t}-${(3*e).toString().padStart(2,"0")}-${1===e||4===e?31:30}`)],downloadDataUri:e=>{var t=document.createElement("a");t.setAttribute("href",e.data),t.setAttribute("download",e.filename),t.style.display="none",document.body.appendChild(t),t.click(),document.body.removeChild(t)},polarToCartesian:(e,t)=>({x:e*Math.cos(t),y:e*Math.sin(t)}),cartesianToPolar:(e,t)=>{let s=Math.atan2(t,e);if(s<0)for(;s<0;)s+=2*Math.PI;if(s>=2*Math.PI)for(;s>=Math.PI;)s-=2*Math.PI;return{r:Math.sqrt(e*e+t*t),angle:s}},pageOffset:e=>{const t=e.getBoundingClientRect(),s=e.ownerDocument.defaultView;return{top:t.top+s.pageYOffset,left:t.left+s.pageXOffset}},fuzzySearch:(e,t)=>{const s=".*"+t.replace(/(.)/g,"$1.*").toLowerCase();var n=new RegExp(s);return e.filter((e=>n.test(e.toLowerCase())))},dist2:a,distToSegment2:l,distToSegment:(e,t,s)=>Math.sqrt(l(e,t,s)),sepCase:i,snakeCase:c,kebabCase:e=>i(e,"-"),camelCase:e=>{const t=r(e);return(t.slice(0,1).toLowerCase()+t.slice(1)).replace(/([-_ ]){1,}/g," ").split(/[-_ ]/).reduce(((e,t)=>e+t[0].toUpperCase()+t.substring(1)))},contains:d,getCSS:g,appendCSS:p,getSVGString:e=>{e.setAttribute("xlink","http://www.w3.org/1999/xlink");var t=g(e);p(t,e);var s=(new XMLSerializer).serializeToString(e);return s=(s=s.replace(/(\w+)?:?xlink=/g,"xmlns:xlink=")).replace(/NS\d+:href/g,"xlink:href")},svgStringToImage:(e,t,s,n,o,r)=>{var a="data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(e))),l=document.createElement("canvas"),i=l.getContext("2d");l.width=t,l.height=s;var c=new Image;c.onload=()=>{i.clearRect(0,0,t,s),o||(i.beginPath(),i.fillStyle="#fff",i.fillRect(0,0,l.width,l.height)),i.drawImage(c,0,0,t,s),r&&r(l.toDataURL())},c.src=a},svgToUri:e=>{let t=(new XMLSerializer).serializeToString(e);return t.match(/^]+xmlns="http:\/\/www\.w3\.org\/2000\/svg"/)||(t=t.replace(/^]+"http:\/\/www\.w3\.org\/1999\/xlink"/)||(t=t.replace(/^{this.presets[e]=JSON.parse(this.presets[e])})),t.forEach(((t,s)=>{const n=t.settings.name?e.addFolder({title:t.settings.name}):e;"controls"in t.settings&&Object.keys(t.settings.controls).forEach((e=>{const s=u(t.settings.controls[e],"val");s.presetKey=t.settings.name?c(t.settings.name)+"_$"+e:null,s.label=s.label?s.label:e,Object.defineProperty(t,"$"+e,{get:()=>t.settings.controls[e].val,set:s=>{t.settings.controls[e].val=s}});const o=n.addInput(t,"$"+e,s);s.callback&&o.on("change",(e=>{s.callback(e)}))})),"buttons"in t.settings&&Object.keys(t.settings.buttons).forEach((e=>{n.addButton({title:t.settings.buttons[e].label}).on("click",(()=>{t.settings.buttons[e].callback()}))})),"buttons_grid"in t.settings&&t.settings.buttons_grid.grids.forEach((e=>{n.addBlade({view:"buttongrid",size:e.size,label:e.label,cells:e.cells}).on("click",(t=>{e.callbacks[t.index[1]][t.index[0]]()}))})),"monitors"in t.settings&&Object.keys(t.settings.monitors).forEach((e=>{const s=t.settings.monitors_options[e];n.addMonitor(t.settings.monitors,e,s),Object.defineProperty(t,"$"+e,{get:()=>t.settings.monitors[e],set:s=>{t.settings.monitors[e]=s}})}))}));const n=e.addFolder({title:"Presets",expanded:!1});n.addBlade({view:"list",label:"preset",options:Object.keys(this.presets).map((e=>({text:e,value:e}))),value:Object.keys(this.presets)[0]}).on("change",(e=>{this.loadSettings(this.presets[e.value])})),n.addSeparator(),n.addButton({title:"Store settings"}).on("click",(()=>{console.log("save settings");const t=e.exportPreset();localStorage.setItem(s,JSON.stringify(t)),console.log(t),console.log("json:\n",JSON.stringify(t))})),n.addButton({title:"Restore settings"}).on("click",(()=>{this.loadSettings()})),n.addButton({title:"Download settings"}).on("click",(()=>{console.log("download settings");const t=s+"_"+(new Date).toLocaleString().replace(/[^0-9]+/g,"-")+".json",n="data:text/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(e.exportPreset(),null,2));console.log(JSON.stringify(e.exportPreset()));const o=document.createElement("a");o.download=t,o.href=n,o.click(),o.remove()})),n.addButton({title:"Upload settings"}).on("click",(()=>{console.log("upload settings");const t=document.createElement("input");t.setAttribute("type","file"),t.setAttribute("accept","application/json"),t.style.opacity="0",t.style.position="fixed",document.body.appendChild(t),t.addEventListener("input",(s=>{if(t.files&&t.files[0]){const s=t.files[0];let n=new FileReader;n.readAsText(s),n.onload=()=>{console.log("settings loaded..."),console.log(n.result),e.importPreset(JSON.parse(n.result))},n.onerror=()=>{console.log("error loading file!"),console.log(n.error)}}document.body.removeChild(t)}),{once:!0}),t.click()})),n.addButton({title:"Default settings"}).on("click",(()=>{this.presets.default&&this.loadSettings(this.presets.default)}))}loadSettings(e=null){if(console.log("restore settings"),console.log("presets",this.presets),e)this.ctrl.importPreset(e),console.log("loaded settings:",e);else if(this.presets.default)this.ctrl.importPreset(this.presets.default),console.log("loaded default settings:",this.presets.default);else{const e=localStorage.getItem(this.settingsName);e&&(this.ctrl.importPreset(JSON.parse(e)),console.log("loaded settings from local storage:",preset))}}},...h}})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).utils=t()}(this,(function(){"use strict";const e=(e,t,s)=>e+(t-e)*s,t=(t,s,n)=>({x:e(t.x,s.x,n),y:e(t.y,s.y,n),z:e(t.z,s.z,n)}),s=e=>e*e,n=e=>{const t=Array(e.length);for(let s=0;se.replace(/([ĄĆĘŁŃÓŚŹŻąćęłńóśźż])/g,(function(e){return o[e]})),a=(e,t)=>s(t.x-e.x)+s(t.y-e.y)+(void 0!==e.z&&void 0!==t.z?s(t.z-e.z):0),l=(e,t,s)=>{const n=a(t,s);if(0===n)return a(e,t);const o=((e.x-t.x)*(s.x-t.x)+(e.y-t.y)*(s.y-t.y))/n;return a(e,o<0?t:o>1?s:{x:t.x+o*(s.x-t.x),y:t.y+o*(s.y-t.y)})},i=(e,t="-")=>r(e).replace(/[A-Z]/g,((e,s)=>{const n=e.toLowerCase();return s?t+n:n})).replace(/([-_ ]){1,}/g,t),c=e=>i(e,"_"),d=(e,t)=>-1!==t.indexOf(e),g=e=>{const t=[];t.push("#"+e.id);for(let s=0;s{var s=document.createElement("style");s.setAttribute("type","text/css"),s.innerHTML=e;var n=t.hasChildNodes()?t.children[0]:null;t.insertBefore(s,n)},u=(e,t)=>{const{[t]:s,...n}=e;return n};var h=Object.freeze({__proto__:null,appendCSS:p,camelCase:e=>{const t=r(e);return(t.slice(0,1).toLowerCase()+t.slice(1)).replace(/([-_ ]){1,}/g," ").split(/[-_ ]/).reduce(((e,t)=>e+t[0].toUpperCase()+t.substring(1)))},cartesianToPolar:(e,t)=>{let s=Math.atan2(t,e);if(s<0)for(;s<0;)s+=2*Math.PI;if(s>=2*Math.PI)for(;s>=Math.PI;)s-=2*Math.PI;return{r:Math.sqrt(e*e+t*t),angle:s}},clamp:(e,t,s)=>e>s?s:e180*e/Math.PI,dist:(e,t)=>Math.sqrt(a(e,t)),dist2:a,distToSegment:(e,t,s)=>Math.sqrt(l(e,t,s)),distToSegment2:l,downloadDataUri:e=>{var t=document.createElement("a");t.setAttribute("href",e.data),t.setAttribute("download",e.filename),t.style.display="none",document.body.appendChild(t),t.click(),document.body.removeChild(t)},filterUnique:e=>[...new Set(e)],fuzzySearch:(e,t)=>{const s=".*"+t.replace(/(.)/g,"$1.*").toLowerCase();var n=new RegExp(s);return e.filter((e=>n.test(e.toLowerCase())))},getCSS:g,getQuarter:e=>[(e=e||new Date).getFullYear(),Math.floor(e.getMonth()/3)+1],getSVGString:e=>{e.setAttribute("xlink","http://www.w3.org/1999/xlink");var t=g(e);p(t,e);var s=(new XMLSerializer).serializeToString(e);return s=(s=s.replace(/(\w+)?:?xlink=/g,"xmlns:xlink=")).replace(/NS\d+:href/g,"xlink:href")},intersection:(e,t)=>{const s=t.x-e.x,n=t.y-e.y,o=Math.sqrt(n*n+s*s);if(o>e.r+t.r)return!1;if(oi(e,"-"),lerp:e,lerp3:t,lerpColor:(e,t,s)=>{var n=parseInt(e.replace(/#/g,""),16),o=n>>16,r=n>>8&255,a=255&n,l=parseInt(t.replace(/#/g,""),16);return"#"+((1<<24)+(o+s*((l>>16)-o)<<16)+(r+s*((l>>8&255)-r)<<8)+(a+s*((255&l)-a))|0).toString(16).slice(1)},lerpedPoints:(e,s,n)=>{const o=[],r=1/(n+1);for(let a=0;a{const s=new XMLHttpRequest;s.overrideMimeType("application/json"),s.open("GET",e,!0),s.onreadystatechange=()=>{4===s.readyState&&200===s.status&&t(JSON.parse(s.responseText))},s.send(null)},map:(e,t,s,n,o)=>n+(o-n)*(e-t)/(s-t),norm:(e,t,s)=>(e-t)/(s-t),pageOffset:e=>{const t=e.getBoundingClientRect(),s=e.ownerDocument.defaultView;return{top:t.top+s.pageYOffset,left:t.left+s.pageXOffset}},polarToCartesian:(e,t)=>({x:e*Math.cos(t),y:e*Math.sin(t)}),precision:(e,t)=>Math.round(e*Math.pow(10,t))/Math.pow(10,t),quarterExtent:(e,t)=>[new Date(`${t}-${(3*(e-1)+1).toString().padStart(2,"0")}-01`),new Date(`${t}-${(3*e).toString().padStart(2,"0")}-${1===e||4===e?31:30}`)],radians:e=>e*Math.PI/180,random:(e,t)=>(void 0===t&&(t=e,e=0),e+Math.random()*(t-e)),randomDir:()=>Math.random()>.5?1:-1,randomIndex:e=>Math.floor(Math.random()*e),randomName:e=>(Math.random().toString(36)+"00000000000000000").slice(2,e+2),removeDiacritics:r,removeNonAlphaNumeric:e=>e.replace(/[^A-Za-z0-9]/g,""),sepCase:i,shallowCopyExcluding:u,shuffleArray:e=>{const t=n(e);for(let e=t.length-1;e>0;e--){const s=Math.floor(Math.random()*(e+1));[t[e],t[s]]=[t[s],t[e]]}return t},snakeCase:c,splitChunks:(e,t,s)=>{const n=e.split(new RegExp("(.{"+t.toString()+"})"));return s?n.filter((e=>e.length===t)):n.filter((e=>e.length>0))},square:s,svgStringToImage:(e,t,s,n,o,r)=>{var a="data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(e))),l=document.createElement("canvas"),i=l.getContext("2d");l.width=t,l.height=s;var c=new Image;c.onload=()=>{i.clearRect(0,0,t,s),o||(i.beginPath(),i.fillStyle="#fff",i.fillRect(0,0,l.width,l.height)),i.drawImage(c,0,0,t,s),r&&r(l.toDataURL())},c.src=a},svgToUri:e=>{let t=(new XMLSerializer).serializeToString(e);return t.match(/^]+xmlns="http:\/\/www\.w3\.org\/2000\/svg"/)||(t=t.replace(/^]+"http:\/\/www\.w3\.org\/1999\/xlink"/)||(t=t.replace(/^{var e=6e4*(new Date).getTimezoneOffset();let t=new Date(Date.now()-e).toISOString().replace(/z|t/gi," ").trim().replace(/:/gi,"-");return t=t.substring(0,t.indexOf(".")),t}});return{TweakpaneSettings:class{constructor(e,t,s=null){this.settingsName=s??e.title+"-settings",this.ctrl=e,this.presets=t[0].settings.presets,Object.keys(this.presets).forEach((e=>{this.presets[e]=JSON.parse(this.presets[e])})),t.forEach(((t,s)=>{const n=t.settings.name?e.addFolder({title:t.settings.name}):e;"controls"in t.settings&&Object.keys(t.settings.controls).forEach((e=>{const s=u(t.settings.controls[e],"val");s.presetKey=t.settings.name?c(t.settings.name)+"_$"+e:null,s.label=s.label?s.label:e,Object.defineProperty(t,"$"+e,{get:()=>t.settings.controls[e].val,set:s=>{t.settings.controls[e].val=s}});const o=n.addInput(t,"$"+e,s);s.callback&&o.on("change",(e=>{s.callback(e)}))})),"buttons"in t.settings&&Object.keys(t.settings.buttons).forEach((e=>{n.addButton({title:t.settings.buttons[e].label}).on("click",(()=>{t.settings.buttons[e].callback()}))})),"buttons_grid"in t.settings&&t.settings.buttons_grid.grids.forEach((e=>{n.addBlade({view:"buttongrid",size:e.size,label:e.label,cells:e.cells}).on("click",(t=>{e.callbacks[t.index[1]][t.index[0]]()}))})),"monitors"in t.settings&&Object.keys(t.settings.monitors).forEach((e=>{const s=t.settings.monitors_options[e];n.addMonitor(t.settings.monitors,e,s),Object.defineProperty(t,"$"+e,{get:()=>t.settings.monitors[e],set:s=>{t.settings.monitors[e]=s}})}))}));const n=e.addFolder({title:"Presets",expanded:!1});n.addBlade({view:"list",label:"preset",options:Object.keys(this.presets).map((e=>({text:e,value:e}))),value:Object.keys(this.presets)[0]}).on("change",(e=>{this.loadSettings(this.presets[e.value])})),n.addSeparator(),n.addButton({title:"Store settings"}).on("click",(()=>{console.log("save settings");const t=e.exportPreset();localStorage.setItem(s,JSON.stringify(t)),console.log(t),console.log("json:\n",JSON.stringify(t))})),n.addButton({title:"Restore settings"}).on("click",(()=>{this.loadSettings()})),n.addButton({title:"Download settings"}).on("click",(()=>{console.log("download settings");const t=s+"_"+(new Date).toLocaleString().replace(/[^0-9]+/g,"-")+".json",n="data:text/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(e.exportPreset(),null,2));console.log(JSON.stringify(e.exportPreset()));const o=document.createElement("a");o.download=t,o.href=n,o.click(),o.remove()})),n.addButton({title:"Upload settings"}).on("click",(()=>{console.log("upload settings");const t=document.createElement("input");t.setAttribute("type","file"),t.setAttribute("accept","application/json"),t.style.opacity="0",t.style.position="fixed",document.body.appendChild(t),t.addEventListener("input",(s=>{if(t.files&&t.files[0]){const s=t.files[0];let n=new FileReader;n.readAsText(s),n.onload=()=>{console.log("settings loaded..."),console.log(n.result),e.importPreset(JSON.parse(n.result))},n.onerror=()=>{console.log("error loading file!"),console.log(n.error)}}document.body.removeChild(t)}),{once:!0}),t.click()})),n.addButton({title:"Default settings"}).on("click",(()=>{this.presets.default&&this.loadSettings(this.presets.default)}))}loadSettings(e=null){if(console.log("restore settings"),console.log("presets",this.presets),e)this.ctrl.importPreset(e),console.log("loaded settings:",e);else if(this.presets.default)this.ctrl.importPreset(this.presets.default),console.log("loaded default settings:",this.presets.default);else{const e=localStorage.getItem(this.settingsName);e&&(this.ctrl.importPreset(JSON.parse(e)),console.log("loaded settings from local storage:",preset))}}},...h}}));