web-assets/wordcloud/v2/wordcloud2.min.js
2023-12-30 22:34:25 +01:00

8 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*!
* wordcloud2.js
* http://timdream.org/wordcloud2.js/
*
* Copyright 2011 - 2019 Tim Guan-tin Chien and contributors.
* Released under the MIT license
*/
"use strict";window.setImmediate||(window.setImmediate=window.msSetImmediate||window.webkitSetImmediate||window.mozSetImmediate||window.oSetImmediate||function(){if(!window.postMessage||!window.addEventListener)return null;var t=[void 0],e="zero-timeout-message";return window.addEventListener("message",(function(a){if("string"==typeof a.data&&a.data.substr(0,e.length)===e){a.stopImmediatePropagation();var r=parseInt(a.data.substr(e.length),36);t[r]&&(t[r](),t[r]=void 0)}}),!0),window.clearImmediate=function(e){t[e]&&(t[e]=void 0)},function(a){var r=t.length;return t.push(a),window.postMessage(e+r.toString(36),"*"),r}}()||function(t){window.setTimeout(t,0)}),window.clearImmediate||(window.clearImmediate=window.msClearImmediate||window.webkitClearImmediate||window.mozClearImmediate||window.oClearImmediate||function(t){window.clearTimeout(t)}),function(t){var e=function(){var t=document.createElement("canvas");if(!t||!t.getContext)return!1;var e=t.getContext("2d");return!!e&&(!!e.getImageData&&(!!e.fillText&&(!!Array.prototype.some&&!!Array.prototype.push)))}(),a=function(){if(e){for(var t,a,r=document.createElement("canvas").getContext("2d"),i=20;i;){if(r.font=i.toString(10)+"px sans-serif",r.measureText("").width===t&&r.measureText("m").width===a)return i+1;t=r.measureText("").width,a=r.measureText("m").width,i--}return 0}}(),r=function(t){for(var e,a,r=t.length;r;)e=Math.floor(Math.random()*r),a=t[--r],t[r]=t[e],t[e]=a;return t},i={},o=function(t,o){if(e){var n=Math.floor(Math.random()*Date.now());Array.isArray(t)||(t=[t]),t.forEach((function(e,a){if("string"==typeof e){if(t[a]=document.getElementById(e),!t[a])throw new Error("The element id specified is not found.")}else if(!e.tagName&&!e.appendChild)throw new Error("You must pass valid HTML elements, or ID of the element.")}));var s={list:[],fontFamily:'"Trebuchet MS", "Heiti TC", "微軟正黑體", "Arial Unicode MS", "Droid Fallback Sans", sans-serif',fontWeight:"normal",color:"random-dark",minSize:0,weightFactor:1,clearCanvas:!0,backgroundColor:"#fff",gridSize:8,drawOutOfBound:!1,shrinkToFit:!1,origin:null,drawMask:!1,maskColor:"rgba(255,0,0,0.3)",maskGapWidth:.3,wait:0,abortThreshold:0,abort:function(){},minRotation:-Math.PI/2,maxRotation:Math.PI/2,rotationSteps:0,shuffle:!0,rotateRatio:.1,shape:"circle",ellipticity:.65,classes:null,hover:null,click:null};if(o)for(var l in o)l in s&&(s[l]=o[l]);if("function"!=typeof s.weightFactor){var f=s.weightFactor;s.weightFactor=function(t){return t*f}}if("function"!=typeof s.shape)switch(s.shape){case"circle":default:s.shape="circle";break;case"cardioid":s.shape=function(t){return 1-Math.sin(t)};break;case"diamond":s.shape=function(t){var e=t%(2*Math.PI/4);return 1/(Math.cos(e)+Math.sin(e))};break;case"square":s.shape=function(t){return Math.min(1/Math.abs(Math.cos(t)),1/Math.abs(Math.sin(t)))};break;case"triangle-forward":s.shape=function(t){var e=t%(2*Math.PI/3);return 1/(Math.cos(e)+Math.sqrt(3)*Math.sin(e))};break;case"triangle":case"triangle-upright":s.shape=function(t){var e=(t+3*Math.PI/2)%(2*Math.PI/3);return 1/(Math.cos(e)+Math.sqrt(3)*Math.sin(e))};break;case"pentagon":s.shape=function(t){var e=(t+.955)%(2*Math.PI/5);return 1/(Math.cos(e)+.726543*Math.sin(e))};break;case"star":s.shape=function(t){var e=(t+.955)%(2*Math.PI/10);return(t+.955)%(2*Math.PI/5)-2*Math.PI/10>=0?1/(Math.cos(2*Math.PI/10-e)+3.07768*Math.sin(2*Math.PI/10-e)):1/(Math.cos(e)+3.07768*Math.sin(e))}}s.gridSize=Math.max(Math.floor(s.gridSize),4);var d,c,h,u,m,w,g,v,M=s.gridSize,p=M-s.maskGapWidth,x=Math.abs(s.maxRotation-s.minRotation),b=Math.abs(Math.floor(s.rotationSteps)),T=Math.min(s.maxRotation,s.minRotation);switch(s.color){case"random-dark":g=function(){return L(10,50)};break;case"random-light":g=function(){return L(50,90)};break;default:"function"==typeof s.color&&(g=s.color)}"function"==typeof s.fontWeight&&(v=s.fontWeight);var y=null;"function"==typeof s.classes&&(y=s.classes);var I,k=!1,C=[],S=function(t){var e,a,r=t.currentTarget,i=r.getBoundingClientRect();t.touches?(e=t.touches[0].clientX,a=t.touches[0].clientY):(e=t.clientX,a=t.clientY);var o=e-i.left,n=a-i.top,s=Math.floor(o*(r.width/i.width||1)/M),l=Math.floor(n*(r.height/i.height||1)/M);return C[s]?C[s][l]:null},E=function(t){var e=S(t);I!==e&&(I=e,e?s.hover(e.item,e.dimension,t):s.hover(void 0,void 0,t))},F=function(t){var e=S(t);e&&(s.click(e.item,e.dimension,t),t.preventDefault())},P=[],R=function(t){if(P[t])return P[t];var e=8*t,a=e,r=[];for(0===t&&r.push([u[0],u[1],0]);a--;){var i=1;"circle"!==s.shape&&(i=s.shape(a/e*2*Math.PI)),r.push([u[0]+t*i*Math.cos(-a/e*2*Math.PI),u[1]+t*i*Math.sin(-a/e*2*Math.PI)*s.ellipticity,a/e*2*Math.PI])}return P[t]=r,r},O=function(){return s.abortThreshold>0&&(new Date).getTime()-w>s.abortThreshold},A=function(e,a,r,i,o){if(!(e>=c||a>=h||e<0||a<0)){if(d[e][a]=!1,r)t[0].getContext("2d").fillRect(e*M,a*M,p,p);k&&(C[e][a]={item:o,dimension:i})}},z=function e(i){var o,n,l;Array.isArray(i)?(o=i[0],n=i[1]):(o=i.word,n=i.weight,l=i.attributes);var f=0===s.rotateRatio||Math.random()>s.rotateRatio?0:0===x?T:b>0?T+Math.floor(Math.random()*b)*x/(b-1):T+Math.random()*x,u=function(t){if(Array.isArray(t)){var e=t.slice();return e.splice(0,2),e}return[]}(i),w=function(t,e,r,i){var o=s.weightFactor(e);if(o<=s.minSize)return!1;var n,l=1;o<a&&(l=function(){for(var t=2;t*o<a;)t+=2;return t}()),n=v?v(t,e,o,i):s.fontWeight;var f=document.createElement("canvas"),d=f.getContext("2d",{willReadFrequently:!0});d.font=n+" "+(o*l).toString(10)+"px "+s.fontFamily;var c=d.measureText(t).width/l,h=Math.max(o*l,d.measureText("m").width,d.measureText("").width)/l,u=c+2*h,m=3*h,w=Math.ceil(u/M),g=Math.ceil(m/M);u=w*M,m=g*M;var p=-c/2,x=.4*-h,b=Math.ceil((u*Math.abs(Math.sin(r))+m*Math.abs(Math.cos(r)))/M),T=Math.ceil((u*Math.abs(Math.cos(r))+m*Math.abs(Math.sin(r)))/M),y=T*M,I=b*M;f.setAttribute("width",y),f.setAttribute("height",I),d.scale(1/l,1/l),d.translate(y*l/2,I*l/2),d.rotate(-r),d.font=n+" "+(o*l).toString(10)+"px "+s.fontFamily,d.fillStyle="#000",d.textBaseline="middle",d.fillText(t,p*l,(x+.5*o)*l);var k=d.getImageData(0,0,y,I).data;if(O())return!1;for(var C,S,E,F=[],P=T,R=[b/2,T/2,b/2,T/2];P--;)for(C=b;C--;){E=M;t:for(;E--;)for(S=M;S--;)if(k[4*((C*M+E)*y+(P*M+S))+3]){F.push([P,C]),P<R[3]&&(R[3]=P),P>R[1]&&(R[1]=P),C<R[0]&&(R[0]=C),C>R[2]&&(R[2]=C);break t}}return{mu:l,occupied:F,bounds:R,gw:T,gh:b,fillTextOffsetX:p,fillTextOffsetY:x,fillTextWidth:c,fillTextHeight:h,fontSize:o}}(o,n,f,u);if(!w)return!1;if(O())return!1;if(!s.drawOutOfBound&&!s.shrinkToFit){var p=w.bounds;if(p[1]-p[3]+1>c||p[2]-p[0]+1>h)return!1}for(var I=m+1,C=function(e){var a=Math.floor(e[0]-w.gw/2),r=Math.floor(e[1]-w.gh/2);w.gw,w.gh;return!!function(t,e,a,r,i){for(var o=i.length;o--;){var n=t+i[o][0],l=e+i[o][1];if(n>=c||l>=h||n<0||l<0){if(!s.drawOutOfBound)return!1}else if(!d[n][l])return!1}return!0}(a,r,0,0,w.occupied)&&(function(e,a,r,i,o,n,l,f,d,c){var h,u,m,w=r.fontSize;h=g?g(i,o,w,n,l,c):s.color,u=v?v(i,o,w,c):s.fontWeight,m=y?y(i,o,w,c):s.classes,t.forEach((function(t){if(t.getContext){var o=t.getContext("2d"),n=r.mu;o.save(),o.scale(1/n,1/n),o.font=u+" "+(w*n).toString(10)+"px "+s.fontFamily,o.fillStyle=h,o.translate((e+r.gw/2)*M*n,(a+r.gh/2)*M*n),0!==f&&o.rotate(-f),o.textBaseline="middle",o.fillText(i,r.fillTextOffsetX*n,(r.fillTextOffsetY+.5*w)*n),o.restore()}else{var l=document.createElement("span"),c="";c="rotate("+-f/Math.PI*180+"deg) ",1!==r.mu&&(c+="translateX(-"+r.fillTextWidth/4+"px) scale("+1/r.mu+")");var g={position:"absolute",display:"block",font:u+" "+w*r.mu+"px "+s.fontFamily,left:(e+r.gw/2)*M+r.fillTextOffsetX+"px",top:(a+r.gh/2)*M+r.fillTextOffsetY+"px",width:r.fillTextWidth+"px",height:r.fillTextHeight+"px",lineHeight:w+"px",whiteSpace:"nowrap",transform:c,webkitTransform:c,msTransform:c,transformOrigin:"50% 40%",webkitTransformOrigin:"50% 40%",msTransformOrigin:"50% 40%"};for(var v in h&&(g.color=h),l.textContent=i,g)l.style[v]=g[v];if(d)for(var p in d)l.setAttribute(p,d[p]);m&&(l.className+=m),t.appendChild(l)}}))}(a,r,w,o,n,m-I,e[2],f,l,u),function(e,a,r,i,o,n){var l,f,d=o.occupied,u=s.drawMask;if(u&&((l=t[0].getContext("2d")).save(),l.fillStyle=s.maskColor),k){var m=o.bounds;f={x:(e+m[3])*M,y:(a+m[0])*M,w:(m[1]-m[3]+1)*M,h:(m[2]-m[0]+1)*M}}for(var w=d.length;w--;){var g=e+d[w][0],v=a+d[w][1];g>=c||v>=h||g<0||v<0||A(g,v,u,f,n)}u&&l.restore()}(a,r,0,0,w,i),!0)};I--;){var S=R(m-I);if(s.shuffle&&(S=[].concat(S),r(S)),S.some(C))return!0}return!!s.shrinkToFit&&(Array.isArray(i)?i[1]=3*i[1]/4:i.weight=3*i.weight/4,e(i))},W=function(e,a,r){if(a)return!t.some((function(t){var a=new CustomEvent(e,{detail:r||{}});return!t.dispatchEvent(a)}),this);t.forEach((function(t){var a=new CustomEvent(e,{detail:r||{}});t.dispatchEvent(a)}),this)};!function(){var e=t[0];if(e.getContext)c=Math.ceil(e.width/M),h=Math.ceil(e.height/M);else{var a=e.getBoundingClientRect();c=Math.ceil(a.width/M),h=Math.ceil(a.height/M)}if(W("wordcloudstart",!0)){var r,o,l,f,g;if(u=s.origin?[s.origin[0]/M,s.origin[1]/M]:[c/2,h/2],m=Math.floor(Math.sqrt(c*c+h*h)),d=[],!e.getContext||s.clearCanvas)for(t.forEach((function(t){if(t.getContext){var e=t.getContext("2d");e.fillStyle=s.backgroundColor,e.clearRect(0,0,c*(M+1),h*(M+1)),e.fillRect(0,0,c*(M+1),h*(M+1))}else t.textContent="",t.style.backgroundColor=s.backgroundColor,t.style.position="relative"})),r=c;r--;)for(d[r]=[],o=h;o--;)d[r][o]=!0;else{var v=document.createElement("canvas").getContext("2d");v.fillStyle=s.backgroundColor,v.fillRect(0,0,1,1);var p,x,b=v.getImageData(0,0,1,1).data,T=e.getContext("2d").getImageData(0,0,c*M,h*M).data;for(r=c;r--;)for(d[r]=[],o=h;o--;){x=M;t:for(;x--;)for(p=M;p--;)for(l=4;l--;)if(T[4*((o*M+x)*c*M+(r*M+p))+l]!==b[l]){d[r][o]=!1;break t}!1!==d[r][o]&&(d[r][o]=!0)}T=v=b=void 0}if(s.hover||s.click){for(k=!0,r=c+1;r--;)C[r]=[];s.hover&&e.addEventListener("mousemove",E),s.click&&(e.addEventListener("click",F),e.style.webkitTapHighlightColor="rgba(0, 0, 0, 0)"),e.addEventListener("wordcloudstart",(function t(){e.removeEventListener("wordcloudstart",t),e.removeEventListener("mousemove",E),e.removeEventListener("click",F),I=void 0}))}l=0,0!==s.wait?(f=window.setTimeout,g=window.clearTimeout):(f=window.setImmediate,g=window.clearImmediate);var y=function(e,a){t.forEach((function(t){t.removeEventListener(e,a)}),this)},S=function t(){y("wordcloudstart",t),g(i[n])};!function(e,a){t.forEach((function(t){t.addEventListener(e,a)}),this)}("wordcloudstart",S),i[n]=f((function t(){if(l>=s.list.length)return g(i[n]),W("wordcloudstop",!1),y("wordcloudstart",S),void delete i[n];w=(new Date).getTime();var e=z(s.list[l]),a=!W("wordclouddrawn",!0,{item:s.list[l],drawn:e});if(O()||a)return g(i[n]),s.abort(),W("wordcloudabort",!1),W("wordcloudstop",!1),y("wordcloudstart",S),void delete i[n];l++,i[n]=f(t,s.wait)}),s.wait)}}()}function L(t,e){return"hsl("+(360*Math.random()).toFixed()+","+(30*Math.random()+70).toFixed()+"%,"+(Math.random()*(e-t)+t).toFixed()+"%)"}};o.isSupported=e,o.minFontSize=a,o.stop=function(){if(i)for(var t in i)window.clearImmediate(i[t])},"function"==typeof define&&define.amd?(t.WordCloud=o,define("wordcloud",[],(function(){return o}))):"undefined"!=typeof module&&module.exports?module.exports=o:t.WordCloud=o}(this);