UNPKG

vislite

Version:

灵活、快速、简单的数据可视化交互式跨端前端库

5 lines 15.4 kB
/*! * Canvas of VISLite JavaScript Library v1.3.0 * git+https://github.com/oi-contrib/VISLite.git */ var t=function(i,e){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,i){t.__proto__=i}||function(t,i){for(var e in i)Object.prototype.hasOwnProperty.call(i,e)&&(t[e]=i[e])},t(i,e)};function i(i,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=i}t(i,e),i.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var e=function(){return e=Object.assign||function(t){for(var i,e=1,n=arguments.length;e<n;e++)for(var r in i=arguments[e])Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r]);return t},e.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var n=function(t,i,e,n,r){var o=Math.cos(e),s=Math.sin(e);return[+((n-t)*o-(r-i)*s+t).toFixed(7),+((n-t)*s+(r-i)*o+i).toFixed(7)]};var r=function(t,i,e,n,r){return t.beginPath(),t.translate(e,n),t.rotate(r),t.font=i.fontStyle+" "+i.fontWeight+" "+i.fontSize+"px "+i.fontFamily,t},o=function(t,i,e,r,o,s,_,a){if(o>s){var h=o;o=s,s=h}return _%=2*Math.PI,a>=1.999999*Math.PI||a<=1.999999*-Math.PI?a=2*Math.PI:a%=2*Math.PI,function(t,i,e,r,o,s,_){i<0&&(t+=i,i*=-1);var a,h=[];a=n(0,0,t,o,0),h[0]=a[0],h[1]=a[1],a=n(0,0,i,a[0],a[1]),h[2]=a[0],h[3]=a[1],a=n(0,0,t,s,0),h[4]=a[0],h[5]=a[1],a=n(0,0,i,a[0],a[1]),h[6]=a[0],h[7]=a[1],_(t,t+i,h[0]+e,h[1]+r,h[4]+e,h[5]+r,h[2]+e,h[3]+r,h[6]+e,h[7]+r,.5*(s-o))}(_,a,e,r,o,s,(function(n,_,a,h,l,p,u,c,f,g,y){y<0&&(y=-y),t.beginPath(),t.moveTo(a,h),t.arc(e,r,o,n,_,!1),"round"==i.arcEndCap?t.arc(.5*(u+f),.5*(c+g),y,_-Math.PI,_,!0):"-round"==i.arcEndCap?t.arc(.5*(u+f),.5*(c+g),y,_-Math.PI,_,!1):t.lineTo(f,g),t.arc(e,r,s,_,n,!0),"round"==i.arcStartCap?t.arc(.5*(a+l),.5*(h+p),y,n,n-Math.PI,!0):"-round"==i.arcStartCap?t.arc(.5*(a+l),.5*(h+p),y,n,n-Math.PI,!1):t.lineTo(a,h)})),"butt"==i.arcStartCap&&t.closePath(),t},s=function(t,i,e,n){return t.beginPath(),t.moveTo(i+n,e),t.arc(i,e,n,0,2*Math.PI),t},_=function(t,i,e,n,r,o){t.beginPath();var s=[];if(Array.isArray(i.rectRadius)&&i.rectRadius.length>0)for(var _=0;_<4;_++)s.push.apply(s,i.rectRadius);return s.length>=4?(t.arc(e+s[0],n+s[0],s[0],Math.PI,1.5*Math.PI),t.arc(e+r-s[1],n+s[1],s[1],1.5*Math.PI,0),t.arc(e+r-s[2],n+o-s[2],s[2],0,.5*Math.PI),t.arc(e+s[3],n+o-s[3],s[3],.5*Math.PI,Math.PI),t.closePath()):t.rect(e,n,r,o),t};function a(t,i,e,n,r){for(var o=0,s="",_=0;_<i.length;_++)t.measureText(s+i[_]).width>e||/\n$/.test(s)?(r(s,((o+=1)-.5)*n),s=i[_]):_==i.length-1?(o+=1,r(s+i[_],(o-.5)*n)):s+=i[_];return o*n}function h(t){var i={fontSize:16,fontFamily:"sans-serif",fontWeight:400,fontStyle:"normal",arcStartCap:"butt",arcEndCap:"butt",rectRadius:[]},n={fillStyle:"black",strokeStyle:"black",lineWidth:1,lineCap:"butt",lineJoin:"miter",lineDash:[],textAlign:"left",textBaseline:"middle",shadowBlur:0,shadowColor:"black"};return"special"==t?i:"init"==t?n:e(e({},i),n)}var l=function(){function t(t,i,e,n,r){void 0===i&&(i={}),void 0===n&&(n=!1),this.__region=null,this.__onlyRegion=!1,this.__onlyView=!1,this.__specialConfig=h("special"),this.__initConfig=h("init"),this.painter=t.getContext("2d",i),this.__region=e,this.__isPainter=n,this.painter.textBaseline="middle",this.painter.textAlign="left"}return t.prototype.useConfig=function(t,i){if(this.__region&&!this.__onlyView&&["fillStyle","strokeStyle","shadowBlur","shadowColor"].indexOf(t)<0&&this.__region.useConfig(t,i),this.__isPainter&&this.__onlyRegion)return this;if("lineDash"==t)this.painter.setLineDash&&this.painter.setLineDash(i);else if(t in this.__specialConfig)"fontSize"==t&&(i=Math.round(i)),this.__specialConfig[t]=i;else{if(!(t in this.__initConfig))throw new Error("Illegal configuration item of painter : "+t+" !");this.painter[t]=i}return this},t.prototype.fillText=function(t,i,e,n){return void 0===n&&(n=0),this.__region&&!this.__onlyView&&this.__region.fillText(t,i,e,n),this.__isPainter&&this.__onlyRegion||(this.painter.save(),r(this.painter,this.__specialConfig,i,e,n).fillText(t,0,0),this.painter.restore()),this},t.prototype.strokeText=function(t,i,e,n){return void 0===n&&(n=0),this.__region&&!this.__onlyView&&this.__region.strokeText(t,i,e,n),this.__isPainter&&this.__onlyRegion||(this.painter.save(),r(this.painter,this.__specialConfig,i,e,n).strokeText(t,0,0),this.painter.restore()),this},t.prototype.fullText=function(t,i,e,n){return void 0===n&&(n=0),this.__region&&!this.__onlyView&&this.__region.fullText(t,i,e,n),this.__isPainter&&this.__onlyRegion||(this.painter.save(),r(this.painter,this.__specialConfig,i,e,n),this.painter.fillText(t,0,0),this.painter.strokeText(t,0,0),this.painter.restore()),this},t.prototype.fillTexts=function(t,i,e,n,o,s){var _=this;void 0===o&&(o=1.2),void 0===s&&(s=0);var h=0;if(this.__region&&!this.__onlyView&&(h=this.__region.fillTexts(t,i,e,n,o)),this.__isPainter&&this.__onlyRegion)return h;this.painter.save(),r(this.painter,this.__specialConfig,i,e,s);var l=a(this.painter,t,n,this.__specialConfig.fontSize*o,(function(t,i){_.painter.fillText(t,0,i)}));return this.painter.restore(),l},t.prototype.strokeTexts=function(t,i,e,n,o,s){var _=this;void 0===o&&(o=1.2),void 0===s&&(s=0);var h=0;if(this.__region&&!this.__onlyView&&(h=this.__region.fillTexts(t,i,e,n,o)),this.__isPainter&&this.__onlyRegion)return h;this.painter.save(),r(this.painter,this.__specialConfig,i,e,s);var l=a(this.painter,t,n,this.__specialConfig.fontSize*o,(function(t,i){_.painter.strokeText(t,0,i)}));return this.painter.restore(),l},t.prototype.fullTexts=function(t,i,e,n,o,s){var _=this;void 0===o&&(o=1.2),void 0===s&&(s=0);var h=0;if(this.__region&&!this.__onlyView&&(h=this.__region.fillTexts(t,i,e,n,o)),this.__isPainter&&this.__onlyRegion)return h;this.painter.save(),r(this.painter,this.__specialConfig,i,e,s);var l=a(this.painter,t,n,this.__specialConfig.fontSize*o,(function(t,i){_.painter.fillText(t,0,i),_.painter.strokeText(t,0,i)}));return this.painter.restore(),l},t.prototype.beginPath=function(){return this.__region&&!this.__onlyView&&this.__region.beginPath(),this.__isPainter&&this.__onlyRegion||this.painter.beginPath(),this},t.prototype.closePath=function(){return this.__region&&!this.__onlyView&&this.__region.closePath(),this.__isPainter&&this.__onlyRegion||this.painter.closePath(),this},t.prototype.moveTo=function(t,i){return this.__region&&!this.__onlyView&&this.__region.moveTo(t,i),this.__isPainter&&this.__onlyRegion||this.painter.moveTo(Math.round(t)+.5,Math.round(i)+.5),this},t.prototype.lineTo=function(t,i){return this.__region&&!this.__onlyView&&this.__region.lineTo(t,i),this.__isPainter&&this.__onlyRegion||this.painter.lineTo(Math.round(t)+.5,Math.round(i)+.5),this},t.prototype.arc=function(t,i,e,n,r){return this.__region&&!this.__onlyView&&this.__region.arc(t,i,e,n,r),this.__isPainter&&this.__onlyRegion||this.painter.arc(t,i,e,n,n+r,r<0),this},t.prototype.fill=function(){return this.__region&&!this.__onlyView&&this.__region.fill(),this.__isPainter&&this.__onlyRegion||this.painter.fill(),this},t.prototype.stroke=function(){return this.__region&&!this.__onlyView&&this.__region.stroke(),this.__isPainter&&this.__onlyRegion||this.painter.stroke(),this},t.prototype.full=function(){return this.__region&&!this.__onlyView&&this.__region.full(),this.__isPainter&&this.__onlyRegion||(this.painter.fill(),this.painter.stroke()),this},t.prototype.save=function(){return this.__region&&!this.__onlyView&&this.__region.save(),this.__isPainter&&this.__onlyRegion||this.painter.save(),this},t.prototype.restore=function(){return this.__region&&!this.__onlyView&&this.__region.restore(),this.__isPainter&&this.__onlyRegion||this.painter.restore(),this},t.prototype.clip=function(){return this.__region&&!this.__onlyView&&this.__region.clip(),this.__isPainter&&this.__onlyRegion||this.painter.clip(),this},t.prototype.quadraticCurveTo=function(t,i,e,n){return this.__region&&!this.__onlyView&&this.__region.quadraticCurveTo(t,i,e,n),this.__isPainter&&this.__onlyRegion||this.painter.quadraticCurveTo(t,i,e,n),this},t.prototype.bezierCurveTo=function(t,i,e,n,r,o){return this.__region&&!this.__onlyView&&this.__region.bezierCurveTo(t,i,e,n,r,o),this.__isPainter&&this.__onlyRegion||this.painter.bezierCurveTo(t,i,e,n,r,o),this},t.prototype.clearRect=function(t,i,e,n){return this.__region&&!this.__onlyView&&this.__region.clearRect(t,i,e,n),this.__isPainter&&this.__onlyRegion||this.painter.clearRect(t,i,e,n),this},t.prototype.clearCircle=function(t,i,e){return this.__region&&!this.__onlyView&&this.__region.clearCircle(t,i,e),this.__isPainter&&this.__onlyRegion||(this.painter.beginPath(),this.painter.globalCompositeOperation="destination-out",this.painter.arc(t,i,e,0,2*Math.PI),this.painter.fill(),this.painter.globalCompositeOperation="source-over",this.painter.closePath()),this},t.prototype.fillArc=function(t,i,e,n,r,s){return this.__region&&!this.__onlyView&&this.__region.fillArc(t,i,e,n,r,s),this.__isPainter&&this.__onlyRegion||o(this.painter,this.__specialConfig,t,i,e,n,r,s).fill(),this},t.prototype.strokeArc=function(t,i,e,n,r,s){return this.__region&&!this.__onlyView&&this.__region.strokeArc(t,i,e,n,r,s),this.__isPainter&&this.__onlyRegion||o(this.painter,this.__specialConfig,t,i,e,n,r,s).stroke(),this},t.prototype.fullArc=function(t,i,e,n,r,s){return this.__region&&!this.__onlyView&&this.__region.fullArc(t,i,e,n,r,s),this.__isPainter&&this.__onlyRegion||(o(this.painter,this.__specialConfig,t,i,e,n,r,s),this.painter.fill(),this.painter.stroke()),this},t.prototype.fillCircle=function(t,i,e){return this.__region&&!this.__onlyView&&this.__region.fillCircle(t,i,e),this.__isPainter&&this.__onlyRegion||s(this.painter,t,i,e).fill(),this},t.prototype.strokeCircle=function(t,i,e){return this.__region&&!this.__onlyView&&this.__region.strokeCircle(t,i,e),this.__isPainter&&this.__onlyRegion||s(this.painter,t,i,e).stroke(),this},t.prototype.fullCircle=function(t,i,e){return this.__region&&!this.__onlyView&&this.__region.fullCircle(t,i,e),this.__isPainter&&this.__onlyRegion||(s(this.painter,t,i,e),this.painter.fill(),this.painter.stroke()),this},t.prototype.fillRect=function(t,i,e,n){return this.__region&&!this.__onlyView&&this.__region.fillRect(t,i,e,n),this.__isPainter&&this.__onlyRegion||_(this.painter,this.__specialConfig,t,i,e,n).fill(),this},t.prototype.strokeRect=function(t,i,e,n){return this.__region&&!this.__onlyView&&this.__region.strokeRect(t,i,e,n),this.__isPainter&&this.__onlyRegion||_(this.painter,this.__specialConfig,t,i,e,n).stroke(),this},t.prototype.fullRect=function(t,i,e,n){return this.__region&&!this.__onlyView&&this.__region.fullRect(t,i,e,n),this.__isPainter&&this.__onlyRegion||(_(this.painter,this.__specialConfig,t,i,e,n),this.painter.fill(),this.painter.stroke()),this},t.prototype.draw=function(){return Promise.resolve()},t.prototype.drawImage=function(t,i,e,n,r,o){var s=this;return void 0===o&&(o=!1),new Promise((function(_){if(s.__region&&!s.__onlyView&&s.__region.fillRect(i,e,n,r),s.__isPainter&&s.__onlyRegion)_({});else if("string"!=typeof t||o)s.painter.drawImage(t,i,e,n,r),_({});else{var a=new Image;a.onload=function(){s.painter.drawImage(a,i,e,n,r),_({})},a.src=t}}))},t.prototype.install=function(t){var i=this,e=function(e){if(e in n)throw new Error("VISLite Canvas:Method already exists and cannot be overwritten.");n[e]=function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];var o=t[e].apply(i,n);return null!=o?o:i}},n=this;for(var r in t)e(r);return this},t}();var p=function(t,i){var e={value:function(){return t},setColor:function(n,r){return t.addColorStop(i?i(n):n,r),e}};return e},u=function(t){function e(i,e,n,r){void 0===n&&(n={}),void 0===r&&(r=1);var o=t.call(this,i,n,e?new l(e,{willReadFrequently:!0}):void 0,!0,r)||this;return o.name="Canvas",o.__regionList={},o.__regionAssemble=function(t,i,e,n){for(var r=[],o=0;o<n;o++)r[o]=t;return function(){for(var o=0;o<n;o++){if(r[o]+e<i){r[o]=+(r[o]+e).toFixed(7);break}o<n-1&&(r[o]=t)}return r}}(0,255,10,3),o.__scaleSize=r,o.setRegion(""),o}return i(e,t),e.prototype.config=function(t){for(var i in t)this.useConfig(i,t[i]);return this},e.prototype.reset=function(){return this.config(h()),this},e.prototype.onlyRegion=function(t){return this.__onlyRegion=t,this},e.prototype.onlyView=function(t){return this.__onlyView=t,this},e.prototype.setRegion=function(t){if(this.__region)if(t){if(null==this.__regionList[t]){var i=this.__regionAssemble();this.__regionList[t]="rgb("+i[0]+","+i[1]+","+i[2]+")"}this.__region.useConfig("fillStyle",this.__regionList[t])&&this.__region.useConfig("strokeStyle",this.__regionList[t])}else this.__region.useConfig("fillStyle","#000000")&&this.__region.useConfig("strokeStyle","#000000");return this},e.prototype.getRegion=function(t,i){var e=this;return new Promise((function(n){var r=e.__region?e.__region.painter.getImageData(t-.5,i-.5,1,1):{data:[0,0,0,0]},o=r.data,s=function(){if(e.__region)for(var t in e.__regionList)if("rgb("+o[0]+","+o[1]+","+o[2]+")"==e.__regionList[t]){n(t);break}n("")};o?s():r.then((function(t){o=t,s()}))}))},e.prototype.textWidth=function(t){this.painter.save(),r(this.painter,this.__specialConfig,0,0,0);var i=this.painter.measureText(t+"").width;return this.painter.restore(),i},e.prototype.getContext=function(t){return void 0===t&&(t=!1),t?this.__region?this.__region.painter:null:this.painter},e.prototype.getInfo=function(){return{width:this.painter.canvas.width/this.__scaleSize,height:this.painter.canvas.height/this.__scaleSize}},e.prototype.createLinearGradient=function(t,i,e,n){return function(t,i,e,n,r){var o=t.createLinearGradient(i,e,n,r);return p(o)}(this.painter,t,i,e,n)},e.prototype.createRadialGradient=function(t,i,e){return function(t,i,e,n){var r=t.createRadialGradient(i,e,0,i,e,n);return p(r)}(this.painter,t,i,e)},e.prototype.createConicGradient=function(t,i,e,n){return function(t,i,e,n,r){var o=t.createConicGradient(n,i,e);return p(o,r?function(t){return r/(2*Math.PI)*t}:void 0)}(this.painter,t,i,e,n)},e.prototype.getColor=function(t,i){t*=this.__scaleSize,i*=this.__scaleSize;var e=this.painter.getImageData(t-.5,i-.5,1,1).data;return"rgba("+e[0]+","+e[1]+","+e[2]+","+e[3]+")"},e}(l);var c=function(t){function e(i,e,n,r){void 0===e&&(e={}),void 0===n&&(n=0),void 0===r&&(r=0);var o=this;if(!i)throw new Error("VISLite Canvas:The mount point requires an HTMLElement type but encountered null.");e=function(t,i){for(var e in t)i[e]=t[e];return i}(e,{region:!0,willReadFrequently:!1}),n=n||i.clientWidth,r=r||i.clientHeight;var s,_=null,a=i;a._vislite_canvas_?(s=a._vislite_canvas_[0],_=a._vislite_canvas_[1]):(s=document.createElement("canvas"),i.appendChild(s),e.region&&(_=document.createElement("canvas")),a._vislite_canvas_=[s,_],i.setAttribute("vislite","Canvas"));for(var h=[_,s],l=0;l<h.length;l++){var p=h[l];p&&(p.style.width=n+"px",p.setAttribute("width",l*n+n+""),p.style.height=r+"px",p.setAttribute("height",l*r+r+""))}return(o=t.call(this,s,_,{willReadFrequently:e.willReadFrequently},2)||this).__canvas=s,o.__el=i,o.painter.scale(2,2),o}return i(e,t),e.prototype.toDataURL=function(){var t=this;return new Promise((function(i){i(t.__canvas.toDataURL())}))},e.prototype.bind=function(t,i){var e=this;return this.__el.addEventListener(t,(function(t){e.getRegion(t.offsetX,t.offsetY).then((function(e){i(e,t.offsetX,t.offsetY)}))})),this},e}(u);export{c as default};