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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              <p>Move your mouse left and right at different speeds.</p>
<p>analogRead</p>
<div id="slider_input" class="slider">
  <div class="slider_indicator"></div>
</div>
<p>responsiveAnalogRead</p>
<div id="slider_output" class="slider">
  <div class="slider_indicator"></div>
</div>
<p id="output"></p>
            
          
!
            
              html {
  background-color: #CC6633;
  font-family: "Fira Mono",Monaco,"Andale Mono","Lucida Console","Bitstream Vera Sans Mono","Courier";
  user-select: none;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -o-user-select: none;
}

.slider {
  margin: 10px;
  width: 400px;
  height: 16px;
  background: rgba(0,0,0,.3);
  position: relative;
}

.slider_indicator {
  width: 16px;
  height: 16px;
  background: #000;
  position: absolute;
}
            
          
!
            
              //
// scroll to bottom to see responsiveAnalogRead() function
//

// consts for demo
var SLIDER_WIDTH = 400;
var ANALOG_RESOLUTION = 1024;
var NOISE = 0.003;

// vars for demo
var mouseX = 0;
var responsiveValue = 0;

//
// set up HTML elements and JS events for demo
//

var output = $('#output');
var sliderIndicatorInput = $('#slider_input > div');
var sliderIndicatorOutput = $('#slider_output > div');

var updateSlider = function(slider, value) {
  var sliderLeft = value / (ANALOG_RESOLUTION - 1) * SLIDER_WIDTH - 8;
  slider.css('left', sliderLeft+'px');
}

var begin = function() {
  var loop = function() {
    var newValue = analogRead();
    responsiveValue = responsiveAnalogRead(newValue);

    // update sliders
    updateSlider(sliderIndicatorInput, newValue);
    updateSlider(sliderIndicatorOutput, responsiveValue);

    // output status
    var outputText = "analogRead: " + newValue + "<br>";
    outputText += "responsiveAnalogRead: " + responsiveValue + "<br><br>";
    outputText += "snap: "+parseFloat(snap).toFixed(2)+"<br>";
    output.html(outputText);
  };

  // event handlers
  $(window).mousemove(function( event ) {
    mouseX = event.pageX;
  });

  // begin loop
  setInterval(loop,50);
}

//
// Define fake Arduino millis() and analogRead() functions
//

var millis = function() {
  var d = new Date();
  return d.getTime(); 
};

var analogRead = function() {
  
  // get input from 0 - 1
  var input = Math.max(0,Math.min(mouseX - 10,SLIDER_WIDTH)) / SLIDER_WIDTH;
  
  // add noise to input
  input += (Math.random() - 0.5) * 2 * NOISE;
  input = Math.max(0, Math.min(1, input));
  
  // simulate analog read
  return Math.floor(input * (ANALOG_RESOLUTION - 1));
};

//
// vars for responsiveAnalogRead
//

var snap = 0.4;
var smoothValue = 0;

//
// responsive analog read
//

var responsiveAnalogRead = function(newValue) {
  // get difference between new input value and current smooth value
  var diff = Math.abs(newValue - smoothValue);
  
  // calculate the exponential moving average based on the snap
  smoothValue += (newValue - smoothValue) * snap;
  
  // expected output is an integer
  return Math.floor(smoothValue);
};

//
// begin demo
//

begin();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console