Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="actions">
    <button onclick="r2d2.run();">RUN</button>
    <button onclick="r2d2.stop();">STOP</button>
    <button onclick="r2d2.clearConsole();">CLEAR CONSOLE</button>
  </div>
  <div class="console"></div>
</div>
              
            
!

CSS

              
                @mixin flexbox(
  $display: flex,
  $direction: row,
  $placeContent: null,
  $placeItems: null,
  $wrap: nowrap,
  $shrink: 1,
  $grow: 0,
  $alignContent: null,
  $justifyContent: null,
  $alignItems: null,
  $justifyItems: null) {

    display: $display;
    flex-direction: $direction;
    
    @if $placeContent == null {
      @if $alignContent   { align-content: $alignContent; }
      @if $justifyContent { justify-content: $justifyContent; }
    } @else {
      place-content: $placeContent;
    }
    
    @if $placeItems == null {
      @if $alignItems   { align-items: $alignItems; }
      @if $justifyItems { justify-items: $justifyItems; }
    } @else {
      place-items: $placeItems;
    }

    flex-wrap: $wrap;
    flex-shrink: $shrink;
    flex-grow: $grow;
}
html, body {
  width:       100%;
  height:      100%;
  padding:     0;
  margin:      0;
  background:  #4a4a4a;
  color:       lawngreen;
  font-family: 'Roboto Mono', monospace;
  overflow:    auto;
  @include flexbox($placeContent: flex-start, $placeItems: center, $direction: column);
  
  .console {
    background: black;
    padding: 1rem;
    overflow: auto;
    margin: 1rem 0;
    height: 75vh;
    width: 75vw;
  }
  
  button {
    outline: none;
    background: lawngreen;
    color: black;
    border: none;
    margin: 1rem 0 0 0;
  }
}
              
            
!

JS

              
                /** console html element */
const consoleElement = document.querySelector('.console');
/**
 _______  ___             ___  _______ 
|   _   ||   |           |   ||       |
|  |_|  ||   |           |   ||  _____|
|       ||   |           |   || |_____ 
|       ||   |  ___   ___|   ||_____  |
|   _   ||   | |   | |       | _____| |
|__| |__||___| |___| |_______||_______|

@desc   a script to automate js method run
@author nicolacastellanidev@gmail.com
*/
function AI(...bindings) {
  if (!bindings) {
    throw new Error('AI cannot run without bindings');
  }
  this.bindings = bindings;
}

AI.prototype = {
  running: false,
  /** @desc run a random binding with random parameters*/
  run() {
    this.running = true;
    /** get a random method to call with random params */
    const randomBindingIndex = Math.floor(Math.random() * this.bindings[0].length);
    const binding = this.bindings[0][randomBindingIndex];
    const randomParamIndex = Math.floor(Math.random() * binding.withParams.length);
    
    /** call random binding */
    try {
      this.log(`>>> SIMULATING *** ${binding.method.name} *** WITH PARAMS *** ${Object.values(binding.withParams[randomParamIndex])} ***`);
      binding.method.call(this, ...Object.values(binding.withParams[randomParamIndex]));
    } catch (ex) {
      this.log(`>>> SIMULATING *** ${binding.method.name} *** WITH PARAMS *** ${binding.withParams[randomParamIndex]} ***`);
      this.log(`>>> ERROR: ${ex.message}`);
    }
    /** then set a random timeout for the next call */
    this.runTimeout = setTimeout(() => {
      this.run();
    }, (Math.floor(Math.random() * 5) + 2) * 1000);
  },
  /** @desc stop the AI execution */
  stop() {
    this.running = false;
    this.log(`>>> SIMULATION STOPPED`);
    clearTimeout(this.runTimeout);
  },
  /** @desc log method - appen a string to the console html element */
  log(what) {
    consoleElement.innerHTML += `${what}</br>`
  },
  /** @desc empty the console */
  clearConsole() {
    consoleElement.innerHTML = ``;
  }
};

/**
* FAKE METHODS IMPLEMENTATION
*/
const fakeMethod = (url) => {
  r2d2.log(`A fakeMethod has been called with url: ${url}`);
}

const sum = (a, b) => {
  r2d2.log(`The sum is: ${a + b}`);
}

const diff = (a, b) => {
  r2d2.log(`The diff is: ${a - b}`);
}

/**
* AI INITIALIZATION
*/
const r2d2 = new AI(
  [
    {
      method: fakeMethod,
      withParams: [
        {url: '/simulate?action=1'},
        {url: '/simulate?action=2'},
      ]
    },
    {
      method: sum,
      withParams: [
        {a: 1, b: 1},
        {a: 1, b: 3}
      ]
    },
    {
      method: diff,
      withParams: [
        {a: 1, b: 1},
        {a: 3, b: 5}
      ]
    }
  ]
);

/**
Currently you can run r2d2 with the method r2d2.run(); but in this example you need to click on the right button
*/
              
            
!
999px

Console