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 globalSnap = 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(globalSnap).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));
};

//
// consts
//

// affects the curve of movement amount > snap amount
// smaller amounts like 0.001 make it ease slower
// larger amounts like 0.1 make it less smooth
var SNAP_MULTIPLIER = 0.007;

//
// vars for responsiveAnalogRead
//

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);
  
  // now calculate a 'snap curve' function, where we pass in the diff (x) and get back a number from 0-1. We want small values of x to result in an output close to zero, so when the smooth value is close to the input value it'll smooth out noise aggressively by responding slowly to sudden changes. We want a small increase in x to result in a much higher output value, so medium and large movements are snappy and responsive, and aren't made sluggish by unnecessarily filtering out noise. A hyperbola (f(x) = 1/x) curve is used. First x has an offset of 1 applied, so x = 0 now results in a value of 1 from the hyperbola function. High values of x tend toward 0, but we want an output that begins at 0 and tends toward 1, so 1-y flips this up the right way. Finally the result is multiplied by 2 and capped at a maximum of one, which means that at a certain point all larger movements are maximally snappy
  
  var snapCurve = function(x) {
    var y = 1 / (x + 1);
    y = (1 - y)*2;
    if(y > 1) {
      return 1;
    }
    return y;
  };
  
  // multiply the input by SNAP_MULTIPLER so input values fit the snap curve better.
  var snap = snapCurve(diff * SNAP_MULTIPLIER);
  // (update globalSnap so we can show snap in the output window)
  globalSnap = snap;
  
  // 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