cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <main class="main-con">
  
  <section class="control-area">
    
    <div class="controls">
      
      <div class="control">
        <label for="size-in">No. of Points<span class="sub-label">(7-20)</span></label>
        <input class="inp" id="size-in" type="number" value="13" min="7" 

max="20"/>
      </div>
      
      <div class="control">
        <label for="angGap-in">Angle Gap (45-360)</label>
        <input class="inp" id="angGap-in" type="number" value="90" min="45" 

max="360" step="45"/>
      </div>
      
      <div class="control">
        <label for="func-in">Function</label>
        <select class="inp" id="func-in">
          <option value="sin">sin</option>
          <option value="cos">cos</option>
        </select>
      </div>
      
      <div class="control control-f-size">
        <label for="simpNavigatorController">Graph 1 Navigator</label>
        <div class="range-con range-con-simple">
<input id="simpNavigatorController" steps="1" type="range" class="range"/>
  <div class="range-thumb">
  </div>
  </div>
      </div>
      
       <div class="control control-f-size">
        <label for="inverseNavigatorController">Graph 2 Navigator</label>
        <div class="range-con range-con-inverse">
<input id="inverseNavigatorController" steps="1" type="range" class="range"/>
  <div class="range-thumb">
  </div>
  </div>
      </div>
      
      <div class="control control-toggle">
        <label id="tg">Show Tangent</label>
              <div class="inp">
        <input id="toggleTangent" type="checkbox" checked="true"/>
        <label for="toggleTangent"></label>
              </div>
      </div>
      
            <div class="control control-toggle">
              <label id="tg">Show Normal</label>
              <div class="inp">
        <input id="toggleNormal" type="checkbox"/>
        <label for="toggleNormal"></label>
              </div>
      </div>
      
      <button class="button button-plot">Plot Graph</button>
      
    </div><!--/controls-->
      
  </section><!--/control area-->
  
  
  <section class="graph-area">
    
    <section class="graph-area-header">
      
      <ul class="indicators">
        
          <li class="indicator navig">  
          <div class="block"></div> <span>Navigator</span>
        </li>
        
        <li class="indicator tangent">
          <div class="block"></div> <span>Tangent</span>
          </li>
          
        <li class="indicator normal">  
          <div class="block"></div> <span>Normal</span>
        </li>
      
      </ul>
      
  <button class="button button-help">?</button>
    </section>
    
    <!--simple graph-->
    <div class="graph-con graph-con-simple">
      <header>
        <h4 class="title">sin</h4>
        
        <section class="output-area">
          <ul>
            <li class="fx">f(x) => sin x</li>
  <li class="fx-value"></li>
  <li class="fx-derivative"></li>
          </ul>
          
        </section>
      </header>
      
      <svg id="simpleGraph" class="graph graph-simple" viewBox="0 0 800 300">
  <g class="main-secs"></g>
  <g class="cross-secs"></g>
  
  <g class="angles-con">
  </g>

  <path class="function-path"></path>
        
  <path class="mobile tangent tangent-simple"></path>
  <path class="mobile normal normal-simple hidden"></path>
  <circle r="7" class="mobile navigator navigator-simple"/>
</svg>
    </div>
    
    
    <!--inverse graph-->
    
      <div class="graph-con graph-con-inverse">
         
      <header>
        <h4 class="title"><span class="main-title">sin</span><span class="up-suffix">-1</span></h4>
        
        <section class="output-area">
          <ul>
            <li class="fx"><span class="main">f(x) => sin</span><span class="up-suffix">-1</span> x</li>
<li class="fx-value"></li>
<li class="fx-derivative"></li>
          </ul>
        </section>
      </header>
        
        
        <svg class="graph graph-inverse" viewBox="0 0 300 500">
  <g class="main-secs"></g>
  <g class="cross-secs"></g>
  
  <g class="angles-con"></g>
  
  <path class="function-path"></path>
          
  <path class="mobile tangent tangent-inverse"></path>
  <path class="mobile normal normal-inverse hidden"></path>
  <circle r="7" class="mobile navigator navigator-inverse"/>
</svg>
        
        
    </div><!---graph-inverse-con-->
    
    
  </section>
  
  
  <div class="diag-help-overlay">
    <div class="diag-help">
      <p>Move the sliders in order to move the navigator point across the graph and visualize the live values.</p>
    </div>
  </div>
</main>

            
          
!
            
              body {
  background-color: #F5F5F5;
  color: #555;
  font-size: 1.1em;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  margin: 0;
}

ul,
li {
  padding: 0;
  margin: 0;
  list-style: none;
}

h1,h2,h3,h4,h5,h6 {
  margin: 0;
}

a {
	text-decoration: none;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

button {
  border: 0;
  outline: 0;
  cursor: pointer;
}

p {
  line-height: .1.5;
}

input {
  outline: 0;
  border: 0;
}


.main-con {
  font-size: 1rem;
  display: flex;
  flex-wrap: wrap;
  height: 100vh;
  overflow: hidden;
}

.main-con .control-area {
  flex: 0 0 300px;
  background: repeating-linear-gradient(135deg,transparent 0,transparent 2em, 

rgba(255, 255, 255, 0.06) 0,rgba(255, 255, 255, 0.06) 2.1em), #57add3;
  padding: 2em 1em;
  overflow: auto;
}


.control-area .controls .control {
  margin-bottom: 2em;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  color: #fbf8f8;
  font-weight: 700;
  letter-spacing: .05em;
}


.control-area .controls .control.control-f-size > *{
  flex: 1 0 100%;
}

.control-area .controls .control > label {
  flex: 0 0 10em;
  text-transform: uppercase;
  padding: .5em 1em .5em 0;
  whitespace: nowrap;
}


.control-area .controls .control > label .sub-label {
  font-size: 0.9em;
  margin-left: 1em;
}
.control-area .controls .control .inp {
  position: relative;
  flex: 1 1 100px;
  padding: .3em 1em;
}

.control-area .controls .control-toggle .inp {
  flex: 0 0 50px;
}

.control-area .controls .control .inp input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

.control-area .controls .control .inp label::after {
  content: "";
  position: absolute;
  top: ;
  width: 1.5rem;
  height: 1.5rem;
  text-align: center;
  line-height: 1.5rem;
  background: white;
  font-size: 2em;
  transform: translateY(-50%);
  color: #333;
  border-radius: .05em;
  box-shadow: 0 .1em .1em rgba(51, 51, 51, 0.03),
              inset 0 0 .1em rgba(51, 51, 51, 0.31);
  cursor: pointer;
}

.control-area .controls .control .inp label:active::after {
 opacity: 0.6; 
}

.control-area .controls .control .inp input[type="checkbox"]:checked + 

label::after {
  content: "✓";
}


.range-con {
  position: relative;
    background: linear-gradient(to bottom, transparent, transparent calc(50% - 

.1em), currentColor 0, currentColor calc(50% + .1em), transparent 0);
}

.range-con .range {
	position: relative;
  display: block;
  width: 100%;
  margin: 1em 0;
  opacity: 0;
  z-index: 2;
}

.range-con .range-thumb {
	position: absolute;
  top: 50%;
  width: 1em;
  height: 1em;
  margin-top: -.5em;
  color: white;
  background: currentColor;
  transform: translateX(-50%);
  border-radius: 50%;
  z-index: 1;
}

 .range-con label {
   pointer-events: none;
   color: currentColor;
 }


.control-area .controls .button-plot {
  display: block;
  width: 100%;
  background: white;
  letter-spacing: .2em;
  font-weight: 700;
  color: #333;
  padding: 1em 2em
}

.button:active {
  opacity: 0.6;
}



.main-con .graph-area {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  padding: .5em 1em;
  overflow: auto;
}

.main-con .graph-area .graph-area-header {
  flex: 100%;
  display: flex;
  font-size: 0.7em;
  padding: 2em .5em;
}

.main-con .graph-area .graph-area-header .indicators .indicator {
  display: inline-block;
  font-size: 1em;
}

.main-con .graph-area .graph-area-header .indicators .indicator.navig .block {
background: #5cf699;
}

.main-con .graph-area .graph-area-header .indicators .indicator.tangent .block  {
background: #fb3;
}

.main-con .graph-area .graph-area-header .indicators .indicator.normal .block  {
	background: tomato;

}

.main-con .graph-area .graph-area-header .indicators .indicator .block {
	display: inline-block;
  vertical-align: middle;
  width: 2em;
  height: 2em;
  border-radius: 50%;
}

.main-con .graph-area .graph-area-header .indicators .indicator span {
  font-size: 1.5em;
  vertical-align: middle;
}

.main-con .graph-area .graph-area-header .indicators .indicator:not(:first-child) 

{
  margin-left: .5em;
}

.main-con .graph-area .graph-area-header .button-help {
  margin-left: auto;
  width: 2em;
  height: 2em;
  background: #57add3;
  color: white;
  border-radius: .2em;
  z-index: 101;
}


.graph-area .graph-con {
  margin: 0 .3em 1em;
  background: #333;
  border-radius: .2em;
  box-shadow: 0 .1em .1em rgba(51, 51, 51, 0.12);
}


.graph-area .graph-con header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  color: white;
  font-size: 1.5rem;
  padding: .5em .5em;
  margin-bottom: 1em;
}

.graph-area .graph-con header .title {
  font-size: 1.2em;
  border-bottom: .1em solid white;
}

.graph-area .graph-con header .output-area {
	flex: 0 0 1;
  font-family: code;
  font-style: italic;
}

.graph-area .graph-con header .output-area li {
  margin-bottom: .5em;
  text-shadow: 0 0 .3em;
}

.graph-area .graph-con-simple {
  position: relative;
  flex: 5 1 500px;
  z-index: 15;
}

.graph-area .graph-con-inverse {
  flex: 0 1 300px;
}

 .up-suffix {
  display: inline-block;
  font-size: .7em;
  transform: translateY(-0.5em);
}

.graph {
  color: white;
    background: linear-gradient(to right, transparent calc(50% - 

.1em),currentColor 0, 
white calc(50% + .1em), transparent 0),
    linear-gradient(to bottom,transparent calc(50% - .1em),currentColor 0, 

currentColor calc(50% + .1em), transparent 0), #333;
  overflow: visible;
}

.graph .cir {
  fill: currentColor;
}


.graph .angles-con text {
  fill: #24aba5;
  font-size: 1em;

}

.graph path {
  stroke: currentColor;
  stroke-width: 4;
}

.graph .navigator {
  opacity: 0;
  fill: #5cf699;
}

.graph .navigator.visible {
  opacity: 1;
  
}

.graph .mobile.invisible {
  opacity: 0;
}

.graph .tangent,
.graph.normal {
  fill: none;
}

.graph .tangent {
  stroke: #fb3;
}

.graph .normal {
  stroke: tomato;
}


.graph-con .graph-simple {
  width: 100%;
}
.graph-con .graph-inverse {
  min-height: 500px;
}

.hidden {
  display: none !important;
}



.diag-help-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height:  100%;
  background: rgba(255, 255, 255, 0.3);
  z-index: 100;
  perspective: 500px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease-out;
}

.diag-help-overlay .diag-help {
  max-width: 300px;
  padding: 2em 3em;
  margin: 50vh auto;
  background: white;
  transform: translateY(-50%) rotateX(180deg);
  color: transparent;
  box-shadow: 0 .35em .35em rgba(51, 51, 51, 0.12);
  transition: all .3s ease-out, color .1s ease-out .15s;
}

.diag-help-overlay.opened {
  opacity: 1;
  pointer-events: auto;
}

.diag-help-overlay.opened .diag-help {
 transform: translateY(-50%) rotateX(0);
color: #333;
}

@media screen and (max-width: 800px) {
  
html,
  body {
    font-size: 12px;
  }
  
  .main-con {
    overflow: auto;
  }
  
  .main-con .control-area {
    flex: 0 0 100%;
  }
  
  .main-con .graph-area .graph-con {
    font-size: 2rem;
  }
  
  .diag-help-overlay .diag-help {
    max-width: 90%;
  }
  
}
            
          
!
            
              (function() {
  
function $$(selector) {
  return [].slice.call(document.querySelectorAll(selector));
}


var M= {
   sin: function sin(x) {
    return Math.sin(x*(Math.PI/180));
  },
  
  sinInverse: function sinInverse(x) {
 return Math.asin(x)*(180/Math.PI);
},
    
  
 cos: function cos(x) {
    return Math.cos(x*(Math.PI/180));
  },
  
  cosInverse: function cosInverse(x) {
 return Math.acos(x)*(180/Math.PI);
},
  
 ratio:  function ratio(name, x) {
return Math[name](x*(Math.PI/180));
},
    
ratioInverse: function ratioInverse(name, x) {
 return Math[("a"+name)](x)*(180/Math.PI);
}
  };


var Graph= (function() {

 
  
  var graphSimple= document.querySelector(".graph-simple"),
    graphInverse= document.querySelector(".graph-inverse"),
      size= 13, //fallback size
      
      altitude= 50,
      
       ns= "http://www.w3.org/2000/svg",
  separation= {},
      angGap= 45; //fallback angGap
  
  
  function removeChildrenOf(el) {
for(var i=el.children.length-1; i>=0; i--) {
    var item= el.children[i];
    item.remove();
  }
    
  }
  
function appendSections(graph, type, radius, size) {
  var viewBox= graph.getAttribute("viewBox"),
      vWidth= parseInt(viewBox.split(" ")[2]),
      vHeight= parseInt(viewBox.split(" ")[3]),
      gapX= vWidth/size,
    gapY= vHeight/size;
      
  
if((graph.querySelector(".main-secs").children[0])) {
    removeChildrenOf(graph.querySelector(".main-secs"));
    removeChildrenOf(graph.querySelector(".cross-secs"));
    removeChildrenOf(graph.querySelector(".angles-con"));
  }
  
 
     var hGaps= (function() {
        var n= Math.floor(size/2),
              gap= type==="simple" ? gapX : gapY,
            inPoint= type==="simple" ? (vWidth/2) - n*gap : (vHeight/2) - n*gap,
            gaps= [];
        for(var i= 0; i < size; i++) {
          gaps.push(inPoint+gap*i);
        }
        
        
        if(type=== "simple") {
          separation.simple= gapX;
        } else {
          separation.inverse= gapY;
        }
        
        return gaps;
        
      })();
      
  
  
      mainSecs= graph.querySelector(".main-secs"), 
      crossSecs= graph.querySelector(".cross-secs");
      
  
  
  //main sections
  for(var i=0; i < size; i++) {
    
    let circle= document.createElementNS(ns,"circle");
    
    circle.setAttribute("r", radius); 
    circle.classList.add("cir");
    
    if(type === "simple") {
      
      circle.setAttribute("cx", hGaps[i]); 
      circle.setAttribute("cy",vHeight/2);
      
    } else if(type === "inverse") {
      
       circle.setAttribute("cy", hGaps[i]); 
       circle.setAttribute("cx",vWidth/2);
      
    }
    
    mainSecs.appendChild(circle);
    
  }
  
  //cross sections
  
  for(var i=0; i < 2; i++) {
    let circle= document.createElementNS(ns, "circle");
    
    circle.classList.add("cir");
    circle.setAttribute("r", radius);
    
    if(type === "simple") {
      
      circle.setAttribute("cx", vWidth/2);
      if(i === 0) {
      circle.setAttribute("cy", (vHeight/2)-altitude);
      } else {
         circle.setAttribute("cy", (vHeight / 2) + altitude);
      }
      
    } else if(type === "inverse") {
      
       circle.setAttribute("cy", (vHeight / 2));
      if(i === 0) {
         circle.setAttribute("cx", (vWidth / 2)-altitude);
      } else {
         circle.setAttribute("cx", (vWidth / 2) + altitude);
      }
      
    }
    
    crossSecs.appendChild(circle);
  }
      
    
}
  
  
  function appendAngleText(sec,graph,type,angGap) {
    var _x= parseInt(sec.getAttribute("cx")),
        _y= parseInt(sec.getAttribute("cy")),
        angleCon= graph.querySelector(".angles-con"),
        
        angleText= document.createElementNS(ns, "text");
    
        angleText.textContent= sec.angle;
  
    if(type=== "simple") {
  _y+=35;
    } else if(type=== "inverse") {
 _x+=25;
      
    }
   
    //append angle texts
    angleCon.appendChild(angleText);
    
    
    //center text element to it's relative point
    if(type==="simple") {
      _x-=(angleText.clientWidth/2);
    } else if(type==="inverse") {
      _y+= (angleText.clientHeight/2)
    }
    
     angleText.style.transform= "translateX("+_x  +"px) "+ "translateY("+ _y+ 

"px)";
  }
  
  function assignAngles(graph, type, size, angGap) {
    var secs=  graph.querySelector(".main-secs"),
    half= Math.floor(size/2);
    
     var sec= secs.children;
    for(var i=half; i >= 0; i--) {
      let angVal= ((half-i)*angGap);
      
      sec[i].angle= type==="simple" ? -1* angVal: angVal;
      
      appendAngleText(sec[i],graph,type);
    }
    
    for(var i= half+1; i<size; i++) {
      let angVal= ((half-i)*angGap);
      
     sec[i].angle=type=="simple" ? -1* angVal: angVal;
      
     appendAngleText(sec[i],graph,type, angGap);
    }
      
  }
  
  

var plotter= {
  mapValues: function(func, angGap) {
    var _graph= this,
        mainSecs= this.element.querySelector(".main-secs"),
        crossSecs= this.element.querySelector(".cross-secs"),
        viewBox= _graph.element.viewBox.baseVal,
        vWidth= viewBox.width,
        vHeight= viewBox.height,
        
        inAng= _graph.name=== "simple"?mainSecs.children[0].angle: 

mainSecs.children[mainSecs.children.length-1].angle,
        
        lastAng= _graph.name==="simple" ? mainSecs.children

[mainSecs.children.length-1].angle : mainSecs.children[0].angle,
        
 _gap= separation[_graph.name]/angGap,
        
        _dGap= _graph.name==="simple"?mainSecs.children[0].cx.baseVal.value: 

mainSecs.children[mainSecs.children.length-1].cy.baseVal.value;
     
    _graph.values= [],
    _graph.angles= [];
    _graph.range= [];
 
    
     for(var i=inAng; i <= lastAng; i++) {
       var sec= mainSecs.children[i],
           _values;
       _graph.angles.push(i);
       (_graph.name==="simple"? _dGap+= _gap: _dGap-= _gap);
       
       if(_graph.name==="simple") {
      _values= {
        x: _dGap,
        y: (vHeight/2) + (-1*((M[func](i))*altitude))
      } 
      
       
       } else if(_graph.name==="inverse") { 
         
         console.log("there!");
         _values= {
        x: (vWidth/2) + ((M[func](i))*altitude),
        y: _dGap
      } 
       }
        
        _graph.values.push(_values);
        _graph.range.push(M[func](i));
    
        }
     _graph.element.values= _graph.values;
     _graph.element.angles= _graph.angles;
    _graph.element.range= _graph.range;
   
  } ,
  
  plot: function() {
    var _graph= this,
  pathData,
    pathEl= this.element.querySelector(".function-path");
   pathData= "M"+ this.values[0].x + "," + this.values[0].y+ " L";
    
  
    for(var i=0; i<this.values.length; i++) {
      var _x= this.values[i].x,
          _y= this.values[i].y;
      
      pathData+= _x+ ","+ _y +" ";
  
    }
    
    pathEl.setAttribute("d", pathData);
  
    },
  
  animate: function() {
    var _graph= this.element,
        path= _graph.querySelector(".function-path"),
        pathLength= path.getTotalLength();
    
  var animatePath= path.animate([
    {
     strokeDasharray: pathLength,
     strokeDashoffset: pathLength,
    fill: "transparent"},
    
    {
      strokeDashoffset: 0,
    strokeDasharray: pathLength,
    fill: "rgba(186, 229, 126, 0.16)"
    }
    
  ], {
    duration: 300,
    iterations: 1,
    easing: "ease-out",
    fill: "forwards"
  });
    
    path.style.strokeDashoffset= 0;
  }

  
},
    
  
sGraph= {
    name: "simple",
    element: graphSimple,
    values: []
  },
  inGraph= {
    name: "inverse",
    element: graphInverse,
    values: []
  };
  
  Object.setPrototypeOf(inGraph, plotter);
  Object.setPrototypeOf(sGraph, plotter);
  
  
  
  return {
    make: function(size, angGap) {
      
      return {
        simple: function(func) {
           appendSections(graphSimple, "simple", 10, size);
           assignAngles(graphSimple, "simple", size, angGap);
           sGraph.mapValues(func, angGap);
           sGraph.plot();
          if(!(typeof Element.prototype.animate=== "undefined")) {
           sGraph.animate();
          }
          
          
        },
        inverse: function(func) {
          appendSections(graphInverse, "inverse", 10, size);
           assignAngles(graphInverse, "inverse", size, angGap);
           inGraph.mapValues(func, angGap);
           inGraph.plot();
          
            if(!(typeof Element.prototype.animate=== "undefined")) {
           inGraph.animate();
          }
          
        }
        
      }
    }
  }
  
  })();

Graph.make(13, 90).simple("sin");
Graph.make(13, 90).inverse("sin");

//API design
  
/**

Graph.make(size, angGap)[type](name);

***/

var controller= (function() {
  var sizeIn= document.querySelector("#size-in"),
      angGapIn= document.querySelector("#angGap-in"),
      funcIn= document.querySelector("#func-in"),
      btnPlot= document.querySelector(".button-plot"),
      toggleTangentCheckBox= document.querySelector("#toggleTangent"),
      toggleNormalCheckBox= document.querySelector("#toggleNormal"),
      options= {
        size: sizeIn.value,
        angGap: angGapIn.value,
        func: funcIn.value
      },
      navs= {
        simple: document.querySelector(".navigator-simple"),
        inverse: document.querySelector(".navigator-inverse")
      };
  
  sizeIn.addEventListener("input", function() {
    options.size= this.value;
  });
  
   angGapIn.addEventListener("change", function() {
    options.angGap= this.value;
  });
  
  funcIn.addEventListener("change", function() {
    options.func= this.value;
    console.log(this.value);
  });
  
  function handleTitles(title) {
    var titleEls= {
      simple: document.querySelector(".graph-con-simple header h4"),
      inverse:document.querySelector(".graph-con-inverse header h4 .main-title"),
      simpleFx: document.querySelector(".graph-con-simple header .output-area ul .fx"),
 inverseFx: document.querySelector(".graph-con-inverse header .output-area ul .fx .main")
    }
    
    titleEls.simple.textContent= title;
    titleEls.inverse.textContent= title;
    titleEls.simpleFx.textContent= "f(x) => "+ title + "x";
    titleEls.inverseFx.textContent= "f(x) => "+title;
  }
  
  
  toggleTangentCheckBox.addEventListener("change", handleMobileToggle, false);
    toggleNormalCheckBox.addEventListener("change", handleMobileToggle, false);
  
  function handleMobileToggle() {
    var _id= this.id,
        checked= document.querySelector("#"+ _id).checked,
        _mobiles= document.querySelectorAll("."+ _id.substring(6).toLowerCase());
    
    console.log(checked);
    if(checked) {
      _mobiles.forEach(function(mobile) {
        mobile.classList.remove("hidden")
      });
    } else {
      _mobiles.forEach(function(mobile) {
        mobile.classList.add("hidden")
      });
    }
  }
  
  
  
  btnPlot.addEventListener("click", function() {
    if(options.size) {
      Graph.make(options.size, options.angGap).simple(options.func);
      Graph.make(options.size, options.angGap).inverse(options.func);
      handleTitles(options.func);
      
      initSliders("simple");
      initSliders("inverse");
     
      switchMobiles("off");
      
      
    }
  });
  
  
  function switchMobiles(state) {
       var mobiles= $$(".mobile");
    
    var actions= {
      on: function() {
        mobiles.forEach(function(mobile, index) {
          if(mobile.classList.contains("invisible")) {
          mobile.classList.remove("invisible");
          }
        });
      },
      off: function() {
        mobiles.forEach(function(mobile, index) {
         if(!(mobile.classList.contains("invisible"))) {
           mobile.classList.add("invisible");
         } 
        });
      }
    }
    
    actions[state]();
  }
  
  
  function initSliders(type) {
  var range= document.querySelector(".range-con.range-con-"+type+ " .range"),
    thumb= range.parentElement.querySelector(".range-thumb"),
      _graph= document.querySelector(".graph-"+type),
      size= Math.abs(_graph.angles[0])+Math.abs(_graph.angles

[_graph.angles.length-1]),
      hs= size/2,
      minVal= -1*(size/2),
      maxVal= size/2;
    console.log(_graph.values)
  
    
    range.setAttribute("min", minVal);
    range.setAttribute("max", maxVal);
    
    range.addEventListener("input", handleSliding, false);
    

    function handleSliding() {
      
      if(navs.simple.classList.contains("invisible")) {
        switchMobiles("on");
      }
      switchMobiles("on");
      //thumb handling
      var val= parseInt(this.value),
           tx,
            index= hs+ val;
        tx= (((hs+ val)/size)*100);
        thumb.style.left=  tx + "%";
      
        manageNavigator(type, _graph.values[index].x, _graph.values[index].y);
       
        manageValues(type,_graph, index,  manageSlope(type, _graph, index));
      
    }
    
    
function filterValue(val) {
      var _val= val + "",
          finalRes= 1;
      
      for(var i=0; i< _val.length; i++) {
        if(_val[i]=== "0" || _val[i]=== "." ||_val[i]=== "-") {
          finalRes *= 1;
        } else {
          finalRes *= 0
        }
        
      }
  if(finalRes===1) {
    console.log("there!");
  }
      
      return (finalRes? Math.abs(val): val);
    }
    
    function manageValues(gType,_graph, index, m) {
      var graphCon= _graph.parentElement,
          valuesEl= {
            fxValue: graphCon.querySelector("header .output-area ul .fx-value"),
            derivative: graphCon.querySelector("header .output-area ul .fx-derivative")
          };
      
      var valueText= gType==="simple" ? "f("+ _graph.angles[index] + ") = "+ 

filterValue(_graph.range[index].toFixed(4)): "f("+ _graph.range[index].toFixed(4) + ") = "+ 
_graph.angles[index];
      valuesEl.fxValue.textContent= valueText;
valuesEl.derivative.textContent= "slope = f `(x) = "+ filterValue(m.toFixed(3));

    }
    
    
    function manageNavigator(nav, x, y) {
      
      var nav= navs[nav];
      
   
      if(!(nav.classList.contains("visible"))) {
        nav.classList.add("visible");
      }
      nav.setAttribute("cx",x);
      nav.setAttribute("cy",y);
      
    }
    
    
   function manageSlope(gType, graph, index) {
     
     var ang= graph.angles[index],
         func= options.func,
         m,
         x= graph.values[index].x,
         y= graph.values[index].y;
     
     if(func === "sin") {
       m= M.cos(ang);
     } else {
       m= -1 *M.sin(ang);
     }
     
     manageTangent(gType, m, x, y);
     manageNormal(gType, (-1/m), x, y);
     
     return m;
   }
    
    
    function manageTangent(gType, m, x, y) {
      manageLine("tangent", gType, m, x, y);
    }
    
    
    function manageNormal(gType, m, x, y) {
      manageLine("normal", gType, m, x, y)
    }
    
  
    function manageLine(line, gType, m, x, y, forwards) {
      var line= document.querySelector("."+line+"-" + gType),
  linePath= "m"+ x+ ","+ y +" l";
        const d= gType==="simple" ? x: y,
              initPoint= forwards ? 0: -15;
      
      
    
      for(var i= initPoint; i< 25; i+=1) {
        let _x,
        _y;
        
        if(gType === "simple") {
          _x= i;
         _y= -1* (m * _x);
        } else {
          _y= i;
          _x= -1* (m * _y);
        }
        
        linePath+= _x +"," + _y + " ";
      }
      

      line.setAttribute("d", linePath);
    }
    
    
    
  } //initSliders
 
  initSliders("simple");
  initSliders("inverse");
  
})();
  
  
  //Extra UI
  
  (function() {
    var els= {
      btnHelp:  document.querySelector(".button-help"),
      diagHelpOverlay: document.querySelector(".diag-help-overlay"),
        diagHelp: document.querySelector(".diag-help")
    };
    
    els.btnHelp.addEventListener("click", function() {
     els.diagHelpOverlay.classList.toggle("opened");
    });
    
els.diagHelpOverlay.addEventListener("click", function(e) {
  if((e.target===this) &&(this.classList.contains("opened"))) {
        this.classList.remove("opened");
      }
    });    
    
  })();
  
})();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console