blobs
Version:
Random blob generation and animation
3 lines (2 loc) • 5.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).blobs=e()}(this,function(){"use strict";function u(t,e){var n=[];return h(t,function(t){n.push(e(t))}),n}function s(t,e){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}function c(t,e){var r,a,s,n=e.stroke||(e.guides?"black":"none"),i=e.strokeWidth||(e.guides?1:0),o=new d("svg"),u=(o.attributes.width=e.width,o.attributes.height=e.height,o.attributes.viewBox="0 0 ".concat(e.width," ").concat(e.height),o.attributes.xmlns="http://www.w3.org/2000/svg",new d("g")),c=(u.attributes.transform=e.transform||"",new d("path"));return c.attributes.stroke=n,c.attributes["stroke-width"]=i,c.attributes.fill=e.fill||"none",c.attributes.d=(r="M".concat((n=t)[0].x,",").concat(n[0].y),h(n,function(t){var e=t.curr,t=(0,t.next)(),e=l(e,e.handleOut),n=l(t,t.handleIn);r+="C".concat(e.x,",").concat(e.y,",").concat(n.x,",").concat(n.y,",").concat(t.x,",").concat(t.y)}),r),u.children.push(c),o.children.push(u),e.guides&&(a=e.stroke||"black",s=e.strokeWidth||1,e.boundingBox&&((i=new d("rect")).attributes.x=0,i.attributes.y=0,i.attributes.width=e.width,i.attributes.height=e.height,i.attributes.fill="none",i.attributes.stroke=a,i.attributes["stroke-width"]=2*s,i.attributes["stroke-dasharray"]=2*s,u.children.push(i)),h(t,function(t){var e=t.curr,t=(0,t.next)(),n=l(e,e.handleOut),r=l(t,t.handleIn),i=new d("line"),o=(i.attributes.x1=e.x,i.attributes.y1=e.y,i.attributes.x2=n.x,i.attributes.y2=n.y,i.attributes["stroke-width"]=s,i.attributes.stroke=a,new d("line")),t=(o.attributes.x1=t.x,o.attributes.y1=t.y,o.attributes.x2=r.x,o.attributes.y2=r.y,o.attributes["stroke-width"]=s,o.attributes.stroke=a,o.attributes["stroke-dasharray"]=2*s,new d("circle")),n=(t.attributes.cx=n.x,t.attributes.cy=n.y,t.attributes.r=s,t.attributes.fill=a,new d("circle")),r=(n.attributes.cx=r.x,n.attributes.cy=r.y,n.attributes.r=s,n.attributes.fill=a,new d("circle"));r.attributes.cx=e.x,r.attributes.cy=e.y,r.attributes.r=2*s,r.attributes.fill=a,u.children.push(i),u.children.push(o),u.children.push(t),u.children.push(n),u.children.push(r)})),o}var e=function(){return(e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},i=function(t){return{x:t.x,y:t.y,handleIn:e({},t.handleIn),handleOut:e({},t.handleOut)}},h=function(n,r){for(var t=function(t){function e(t){return i(n[o(t,n.length)])}r({curr:i(n[t]),index:t,sibling:e,prev:function(){return e(t-1)},next:function(){return e(t+1)}})},e=0;e<n.length;e++)t(e)},l=function(t,e){return{x:t.x+e.length*Math.cos(e.angle),y:t.y+e.length*Math.sin(e.angle)}},n=function(t,a){return u(t,function(t){var e,n,r=t.curr,i=t.next,t=t.prev,o=(e=t(),o=i(),n=o.x-e.x,o=-o.y+e.y,(e=Math.atan2(o,n))<0?Math.abs(e):2*Math.PI-e);return{x:r.x,y:r.y,handleIn:{angle:o+Math.PI,length:a*s(r,t())},handleOut:{angle:o,length:a*s(r,i())}}})},o=function(t,e){return(t%e+e)%e},d=(t.prototype.render=function(){var t=this.renderAttributes(),e=this.renderChildren();return""===e?"<".concat(this.tag).concat(t,"/>"):"<".concat(this.tag).concat(t,">").concat(e,"</").concat(this.tag,">")},t.prototype.renderAttributes=function(){var t=Object.keys(this.attributes);if(0===t.length)return"";for(var e="",n=0,r=t;n<r.length;n++){var i=r[n];e+=" ".concat(i,'="').concat(this.attributes[i],'"')}return e},t.prototype.renderChildren=function(){for(var t="",e=0,n=this.children;e<n.length;e++)t+=n[e].render();return t},t);function t(t){this.tag=t,this.attributes={},this.children=[]}function f(t,e){return t=function(t,e){for(var n=2*Math.PI/t,r=[],i=0;i<t;i++){var o=e(i),a=Math.sin(i*n),s=Math.cos(i*n);r.push({x:.5+a*o,y:.5+s*o,handleIn:{angle:0,length:0},handleOut:{angle:0,length:0}})}return r}(t,e),e=2*Math.PI/t.length,e=4/3*Math.tan(e/4)/Math.sin(e/2)/2,n(t,e)}function r(t){return r.editable(t).render()}new Function("try {return this===window;}catch(e){ return false;}")()&&"localhost"!==location.hostname&&"127.0.0.1"!==location.hostname&&"file:"!==location.protocol||console.warn("You are using the legacy blobs API!\nPlease use 'blobs/v2' instead.");return r.editable=function(e){if(!e)throw new Error("no options specified");a=function(t){for(var e=2166136261,n=0;n<t.length;n++)e=Math.imul(e^t.charCodeAt(n),16777619);return function(){return(e=(e=(e=(e=(e+=e<<13)^e>>>7)+(e<<3))^e>>>17)+(e<<5))>>>0}}(a=e.seed||String(Math.random())),n=a(),r=a(),i=a(),o=a();var n,r,i,o,t=function(){var t=(n>>>=0)+(r>>>=0)|0;return n=r^r>>>9,r=(i>>>=0)+(i<<3)|0,i=(i=i<<21|i>>>11)+(t=t+(o=(o>>>=0)+1|0)|0)|0,(t>>>0)/4294967296};if(!e.size)throw new Error("no size specified");if(!e.stroke&&!e.color)throw new Error("no color or stroke specified");if(e.complexity<=0||1<e.complexity)throw new Error("complexity out of range ]0,1]");if(e.contrast<0||1<e.contrast)throw new Error("contrast out of range [0,1]");var a=3+Math.floor(14*e.complexity),s=u(f(a,function(){return(1-.8*e.contrast*t())/Math.E}),function(t){t=t.curr;return t.x*=e.size,t.y*=e.size,t.handleIn.length*=e.size,t.handleOut.length*=e.size,t.y=e.size-t.y,t.handleIn.angle*=-1,t.handleOut.angle*=-1,t});return c(s,{closed:!0,width:e.size,height:e.size,fill:e.color,transform:"rotate(".concat(t()*(360/a),",").concat(e.size/2,",").concat(e.size/2,")"),stroke:e.stroke&&e.stroke.color,strokeWidth:e.stroke&&e.stroke.width,guides:e.guides})},r.xml=function(t){return new d(t)},r});
//# sourceMappingURL=index.js.map