UNPKG

leapjs-plugins

Version:
20 lines 4.13 kB
/* * LeapJS-Plugins Extra - v0.1.12 - 2016-11-16 * http://github.com/leapmotion/leapjs-plugins/ * * Copyright 2016 LeapMotion, Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ !function(a,b){"function"==typeof define&&define.amd?define([],b):a.LeapDataPlotter=b()}(this,function(){var a,b,c=["#900","#090","#009","#990","#909","#099"],d=0;return a=function(a){this.options=a||(a={}),this.seriesHash={},this.series=[],this.init(a.el)},a.prototype.init=function(a){if(a)var b=a;else{var b=document.createElement("canvas");b.className="leap-data-plotter",document.body.appendChild(b)}this.canvas=b,this.context=b.getContext("2d"),this.rescale()},a.prototype.rescale=function(){var a=getComputedStyle(this.canvas),b=parseInt(a.width,10),c=parseInt(a.height,10);this.width=b,this.height=c;var d=window.devicePixelRatio||1,e=this.context.webkitBackingStorePixelRatio||this.context.mozBackingStorePixelRatio||this.context.msBackingStorePixelRatio||this.context.oBackingStorePixelRatio||this.context.backingStorePixelRatio||1,f=d/e;if(d!==e){var g=this.canvas.width,h=this.canvas.height;this.canvas.width=g*f,this.canvas.height=h*f,this.canvas.style.width=g+"px",this.canvas.style.height=h+"px",this.context.scale(f,f)}this.clear(),this.draw()},a.prototype.plot=function(a,b,c){if(c||(c={}),b.length)for(var d=0,e=120;d<b.length;d++,e=++e>122?97:e)this.getTimeSeries(a+"."+String.fromCharCode(e),c).push(b[d],{pointColor:c.pointColor});else this.getTimeSeries(a,c).push(b,{pointColor:c.pointColor})},a.prototype.getTimeSeries=function(a,c){var d=this.seriesHash[a];if(!d){var e=this.getOptions(a);for(key in c)e[key]=c[key];d=new b(e),this.series.push(d),this.seriesHash[a]=d}return d},a.prototype.getOptions=function(a){var b=d;d=(d+1)%c.length;var e=this.series.length,f=e?this.series[e-1].y+50:0;return{y:f,width:this.width,color:c[b],name:a}},a.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)},a.prototype.draw=function(){var a=this.context;this.series.forEach(function(b){b.draw(a)})},a.prototype.update=function(){this.clear(),this.draw()},b=function(a){a=a||{},this.x=a.x||0,this.y=a.y||0,this.precision=a.precision||5,this.units=a.units||"",this.width=a.width||1e3,this.height=a.height||50,this.length=a.length||600,this.color=a.color||"#000",this.name=a.name||"",this.frameHandler=a.frameHandler,this.max=-(1/0),this.min=1/0,this.data=[],this.pointColors=[]},b.prototype.push=function(a,b){return this.data.push(a),this.data.length>=this.length&&this.data.shift(),b&&b.pointColor&&(this.pointColors.push(b.pointColor),this.pointColors.length>=this.length&&this.pointColors.shift()),this},b.prototype.draw=function(a){var b=this,c=(this.width-10)/(this.length-1),d=-(this.height-10)/(this.max-this.min),e=5,f=(this.max-this.min)*d+10;a.save(),a.strokeRect(this.x,this.y,this.width,this.height),a.translate(this.x,this.y+this.height-e),a.strokeStyle=this.color,a.beginPath();var g=-(1/0),h=1/0;this.data.forEach(function(e,f){e>g&&(g=e),h>e&&(h=e),isNaN(e)?(a.stroke(),a.beginPath()):(a.lineTo(f*c,(e-b.min)*d),b.pointColors[f]&&b.pointColors[f]!=b.pointColors[f-1]&&(a.stroke(),a.strokeStyle=b.pointColors[f],a.beginPath(),a.lineTo(f*c,(e-b.min)*d)))}),a.stroke(),a.fillText(this.name,e,f),a.fillText(this.data[this.data.length-1].toPrecision(this.precision)+this.units,e,0),a.textAlign="end",a.fillText(this.min.toPrecision(this.precision)+this.units,this.width-e,0),a.fillText(this.max.toPrecision(this.precision)+this.units,this.width-e,f),a.textAlign="left",a.restore(),this.min=h,this.max=g},a});