Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <canvas id="canvas"></canvas>
<div id="pen"></div>
              
            
!

CSS

              
                body {
  overflow: hidden;
  user-select: none;
}

#canvas {
  position: fixed;
  top: 0;
  left: 0;
}

#pen {
  position: absolute;
  top: 0;
  left: 0;
  width: 8px;
  height: 8px;
  background: #e91e63;  
  border: 1px solid #333;
  border-radius: 50%;
  opacity: 0;
}

// QUICK SETTINGS
.qs_main {
  background-color: #dddddd;
  text-align: left;
  position: absolute;
  width: 160px;
  font: 10px sans-serif;
  box-shadow: 5px 5px 8px rgba(0, 0, 0, 0.35);
  user-select: none;
  -webkit-user-select: none;
  color: #000000;
  border: none;
}
.qs_content {
  background-color: #cccccc;
  overflow-y: auto;
}
.qs_title_bar {
  background-color: #eeeeee;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  padding: 5px;
  font-weight: bold;
  border: none;
  color: #000000;
}
.qs_container {
  margin: 2px;
  padding: 2px;
  background-color: #eeeeee;
  border: none;
  position: relative;
}
.qs_container_selected {
  border: none;
  background-color: #ffffff;
}
/*========== range ==========*/
.qs_range {
  -webkit-appearance: none;
  -moz-appearance: none;
  width: 100%;
  height: 14px;
  padding: 0px;
  margin: 0px;
  background-color: transparent;
  border: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.qs_range:focus {
  outline: none;
  border: none;
}
.qs_range::-webkit-slider-runnable-track {
  width: 100%;
  height: 12px;
  cursor: pointer;
  background: #cccccc;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
}
.qs_range:focus::-webkit-slider-runnable-track {
  background: #cccccc;
}
.qs_range::-webkit-slider-thumb {
  -webkit-appearance: none;
  height: 12px;
  width: 12px;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  background: #999999;
  cursor: pointer;
  margin-top: 0px;
}
.qs_range::-moz-range-track {
  width: 100%;
  height: 12px;
  cursor: pointer;
  background: #cccccc;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
}
.qs_range::-moz-range-thumb {
  height: 12px;
  width: 12px;
  border: none;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  background: #999999;
  cursor: pointer;
}
.qs_range::-ms-track {
  width: 100%;
  height: 12px;
  cursor: pointer;
  visibility: hidden;
  background: transparent;
}
.qs_range::-ms-thumb {
  height: 12px;
  width: 12px;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  background: #999999;
  cursor: pointer;
  border: none;
}
.qs_range::-ms-fill-lower {
  background: #cccccc;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
}
.qs_range:focus::-ms-fill-lower {
  background: #cccccc;
}
.qs_range::-ms-fill-upper {
  background: #cccccc;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
}
.qs_range:focus::-ms-fill-upper {
  background: #cccccc;
}
/*========== end range ==========*/
.qs_button {
  background-color: #f6f6f6;
  color: #000000;
  height: 20px;
  border: 1px solid #aaaaaa;
  font: 10px sans-serif;
}
.qs_button:active {
  background-color: #ffffff;
  border: 1px solid #aaaaaa;
}
.qs_button:focus {
  border: 1px solid #aaaaaa;
  outline: none;
}
.qs_checkbox {
  cursor: pointer;
}
.qs_checkbox input {
  position: absolute;
  left: -99999px;
}
.qs_checkbox span {
  height: 16px;
  width: 100%;
  display: block;
  text-indent: 20px;
  background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAALklEQVQ4T2OcOXPmfwYKACPIgLS0NLKMmDVrFsOoAaNhMJoOGBioFwZkZUWoJgApdFaxjUM1YwAAAABJRU5ErkJggg==') no-repeat;
}
.qs_checkbox input:checked + span {
  background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAvElEQVQ4T63Tyw2EIBAA0OFKBxBL40wDRovAUACcKc1IB1zZDAkG18GYZTmSmafzgTnnMgwchoDWGlJKheGcP3JtnPceCqCUAmttSZznuYtgchsXQrgC+77DNE0kUpPbmBOoJaBOIVQylnqWgAAeKhDve/AN+EaklJBzhhgjWRoJVGTbNjiOowAIret6a+4jYIwpX8aDwLIs74C2D0IIYIyVP6Gm898m9kbVm85ljHUTf16k4VUefkwDrxk+zoUEwCt0GbUAAAAASUVORK5CYII=') no-repeat;
}
.qs_checkbox_label {
  position: absolute;
  top: 5px;
  left: 24px;
}
.qs_label {
  margin-bottom: 3px;
  user-select: none;
  -webkit-user-select: none;
  cursor: default;
  font: 10px sans-serif;
}
.qs_text_input {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  width: 100%;
  padding: 0 0 0 5px;
  height: 18px;
  border: 1px inset #ffffff;
  background-color: #ffffff;
  color: #000000;
  font-size: 10px;
}
.qs_text_input:focus {
  outline: none;
  background: #ffffff;
  border: 1px inset #ffffff;
}
.qs_select {
  background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAp0lEQVRIS+2SsQ3FIAwF7RVYhA5mgQFhFuhYhJKWL0eKxI8SGylKZ0p4+OBsHGNM+HChAiS7qkgyBKrovaLeOxhjbgtxZ+cFtgelFMg5QwgBvPd/EO5sDbKAlBLUWo/8CjmL075zDmKMj6rEKbpCqBL9aqc4ZUQAhVbInBMQUXz5Vg/WfxOktXZsWWtZLds9uIqlqaH1NFV3jdhSJA47E1CAaE8ViYp+wGiWMZ/T+cgAAAAASUVORK5CYII=') no-repeat right #f6f6f6;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  color: #000000;
  width: 100%;
  height: 18px;
  border: 1px solid #aaaaaa;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  padding: 0 5px;
  -moz-outline: none;
  font-size: 12px;
}
.qs_select option {
  font-size: 12px;
}
.qs_select::-ms-expand {
  display: none;
}
.qs_select:focus {
  outline: none;
}
.qs_number {
  height: 18px;
}
.qs_image {
  width: 100%;
}
.qs_progress {
  width: 100%;
  height: 12px;
  background-color: #cccccc;
  border: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.qs_progress_value {
  height: 100%;
  background-color: #999999;
}
.qs_textarea {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  resize: vertical;
  width: 100%;
  padding: 3px 5px;
  border: 1px inset #ffffff;
  background-color: #ffffff;
  color: #000000;
  font-size: 10px;
}
.qs_textarea:focus {
  outline: none;
  background: #ffffff;
  border: 1px inset #ffffff;
}
.qs_color {
  position: absolute;
  left: -999999px;
}
.qs_color_label {
  width: 100%;
  height: 20px;
  display: block;
  border: 1px solid #aaaaaa;
  cursor: pointer;
  padding: 0 0 0 5px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.qs_file_chooser {
  position: absolute;
  left: -999999px;
}
.qs_file_chooser_label {
  background-color: #f6f6f6;
  color: #000000;
  height: 20px;
  border: 1px solid #aaaaaa;
  font: 10px sans-serif;
  width: 100%;
  display: block;
  cursor: pointer;
  padding: 3px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}


              
            
!

JS

              
                

namespace Demo {
  
  console.clear();
  var log = console.log.bind(console);
  
  const dpr = window.devicePixelRatio || 1;
   
  const epsilon = 1e-6;
   
  const canvas  = document.querySelector("#canvas");
  const context = canvas.getContext("2d");
  
  var vw = canvas.width  = window.innerWidth; 
  var vh = canvas.height = window.innerHeight; 
      
  var ratio = ratio = vw / vh;
     
  var normalized = { x: 0, y: 0 };  
  var nextPoint  = { x: 0, y: 0 };  
  var prevPoint  = { x: 0, y: 0 };
        
  var pen = null;
  
  QuickSettings.useExtStyleSheet();
  var settings = QuickSettings.create(10, 10, "Settings");
    
  const minMass = 1;
  const maxMass = 160;
  
  const minFriction = 0;
  const maxFriction = 0.4;
  
  var mass = 0.5;
  var friction = 0.35;
  var fixedAngle = false;
  
  var stroke = {
    visible: true,
    // color: "#8080c0",
    color: "#8080ff",
    width: 2
  };
  
  var ribs = {
    visible: false,
    color: "#8080ff",
    width: 2
  };
  
  var fill = {
    visible: true,
    color: "#000000"
  };
  
  //
  // INIT
  // ===========================================================================
  function init() {
    
    context.lineCap = "round";
    context.lineJoin = "round";
    
    TweenLite.set("#pen", {
      alpha: 0,
      scale: 0,
      xPercent: -50,
      yPercent: -50
    });
        
    pen = new Pen();
        
    settings
      .addRange("Mass", 0.1, 1, mass, 0.1, value => pen.mass = value)
      .addRange("Friction", 0.1, 1, friction, 0.1, value => pen.friction = value)
      .addRange("Brush Width", 0.1, 2, pen.width, 0.1, value => pen.width = value)    
      .addBoolean("Fixed Angle", fixedAngle, value => fixedAngle = value)
      .addBoolean("Fill", fill.visible, value => {
        (fill.visible = value)
          ? settings.showControl("Fill Color")
          : settings.hideControl("Fill Color");
      })
      .addColor("Fill Color", fill.color, value => fill.color = value)
    
      .addBoolean("Outline", stroke.visible, value => {
        (stroke.visible = value)
          ? settings.showControl("Outline Color").showControl("Outline Width")
          : settings.hideControl("Outline Color").hideControl("Outline Width");
      })
      .addColor("Outline Color", stroke.color, value => stroke.color = value)
      .addRange("Outline Width", 1, 5, stroke.width, 1, value => stroke.width = value)    
      .addBoolean("Ribs", ribs.visible, value => {
        (ribs.visible = value)
          ? settings.showControl("Rib Color").showControl("Rib Width")
          : settings.hideControl("Rib Color").hideControl("Rib Width");
      })
      .addColor("Rib Color", ribs.color, value => ribs.color = value)
      .addRange("Rib Width", 1, 5, ribs.width, 1, value => ribs.width = value)    
      .addButton("Clear", clearCanvas)    
      .setBoolean("Ribs", ribs.visible)
    
    window.addEventListener("resize", resize);
    
    setInterval(queueRender, 4);    
  }
  
  //
  // PEN
  // ===========================================================================
  class Pen {
    
    _mass = 0;
    _friction = 0;
    
    x = 0;
    y = 0;  
    lastX = 0;
    lastY = 0;  
  
    ax = 0;
    ay = 0;  
    accel = 0;
  
    vx = 0;
    vy = 0;
    vel = 0;  
  
    perpX = 0;
    perpY = 0;  
  
    width = 0.5;
  
    started = false;
  
    element = document.querySelector("#pen");
  
    dragger = new Draggable(this.element, {
      cursor: "crosshair",
      bounds: window,
      trigger: canvas,
      throwProps: true,
      edgeResistance: 0.5,
      onPress: this.setPosition,      
      onRelease: this.checkDrag,
      onDrag: this.moveAction,
      onDragStart: this.startDrag,      
      onThrowUpdate: this.moveAction,
      onThrowComplete: this.hideCursor,
      callbackScope: this
    });
  
    transform = this.element._gsTransform;
  
    tween = TweenLite.to(this.element, 0.15, { alpha: 1, scale: 1, paused: true });
  
    constructor() {
            
      this.mass = mass;
      this.friction = friction;
    }
      
    get isActive() { return this.dragger.isPressed || this.dragger.isThrowing; }
  
    get mass() { return this._mass; }
    set mass(value) { this._mass = lerp(minMass, maxMass, value); }
  
    get friction() { return this._friction; }
    set friction(value) { this._friction = lerp(minFriction, maxFriction, value * value); }
  
    moveAction(event) {      
      normalized.x = ratio * this.transform.x / vw;
      normalized.y = this.transform.y / vh;      
    }
    
    startDrag() {
      this.started = true;
    }
  
    checkDrag(event) {
      if (!this.started) {
        this.hideCursor();
      }      
    }
  
    hideCursor() {
      this.tween.reverse();
    }
  
    setPosition(event) {
      
      var x = event.clientX;
      var y = event.clientY;
      
      if (event.targetTouches && event.targetTouches[0]) {
        x = event.targetTouches[0].clientX;
        y = event.targetTouches[0].clientY;
      }
      
      this.started = false;
      
      normalized.x = ratio * x / vw;
      normalized.y = y / vh;
            
      prevPoint.x = 0;
      prevPoint.y = 0;
      
      this.x = normalized.x;
      this.y = normalized.y;
      this.lastX = normalized.x;
      this.lastY = normalized.y;
      this.vx = 0;
      this.vy = 0;
      this.ax = 0;
      this.ay = 0;
            
      TweenLite.set(this.element, { x, y });
      
      this.dragger.update();     
      this.tween.play();
    }
  
    refresh() {
            
      var fx = normalized.x - this.x;
      var fy = normalized.y - this.y;
            
      this.accel = Math.sqrt(fx ** 2 + fy ** 2);
      
      if (this.accel < epsilon) {
        return false;
      }
            
      this.ax = fx / this.mass;
      this.ay = fy / this.mass;

      this.vx += this.ax;
      this.vy += this.ay;
      this.vel = Math.sqrt(this.vx ** 2 + this.vy ** 2);
      
      if (this.vel < epsilon) {
        return false;
      }
            
      // Perpendicular points
      this.perpX = -this.vy;
      this.perpY =  this.vx;
      this.perpX /= this.vel;
      this.perpY /= this.vel;
                  
      if (fixedAngle) {
        this.perpX = 0.6;
        this.perpY = 0.2;
      }
      
      this.vx = this.vx * (1 - this.friction);
      this.vy = this.vy * (1 - this.friction);

      // Update position
      this.lastX = this.x;
      this.lastY = this.y;
      this.x = this.x + this.vx;
      this.y = this.y + this.vy;
      
      return true;
    }
  } 

  //
  // RENDER
  // ===========================================================================
  function render() {
    
    var width = (0.04 - pen.vel) * pen.width;
    
    if (width < epsilon) width = epsilon;
        
    nextPoint.x = lerp(prevPoint.x, pen.perpX * width, 0.25);
    nextPoint.y = lerp(prevPoint.y, pen.perpY * width, 0.25);
        
    var px = pen.lastX;
    var py = pen.lastY;
    var nx = pen.x;
    var ny = pen.y;
        
    var x1 = vw * (px + prevPoint.x) / ratio;
    var x2 = vw * (px - prevPoint.x) / ratio;
    
    var y1 = vh * (py + prevPoint.y);
    var y2 = vh * (py - prevPoint.y);
    
    var x3 = vw * (nx - nextPoint.x) / ratio;
    var x4 = vw * (nx + nextPoint.x) / ratio;
    
    var y3 = vh * (ny - nextPoint.y);
    var y4 = vh * (ny + nextPoint.y);
        
    context.beginPath();    
    context.moveTo(x1, y1);
    context.lineTo(x2, y2);    
    context.lineTo(x3, y3);
    context.lineTo(x4, y4);        
    context.closePath();
    
    if (fill.visible) {
            
      context.lineWidth = 1;
      context.fillStyle = fill.color;      
      context.strokeStyle = fill.color;      
      context.fill();
      context.stroke();
    }
    
    if (ribs.visible) {
      
      context.lineWidth = ribs.width;
      context.strokeStyle = ribs.color;      
      context.beginPath();
      context.moveTo(x1, y1);
      context.lineTo(x2, y2);      
      context.moveTo(x3, y3);
      context.lineTo(x4, y4);      
      context.stroke();
    }
    
    if (stroke.visible) {
            
      context.lineWidth = stroke.width;
      context.strokeStyle = stroke.color;      
      context.beginPath();
      context.moveTo(x1, y1);
      context.lineTo(x4, y4);
      context.moveTo(x2, y2);
      context.lineTo(x3, y3);      
      context.stroke();      
      context.restore();
    }   
    
    prevPoint.x = nextPoint.x;
    prevPoint.y = nextPoint.y;
  }
  
  function queueRender() {    
    if (pen.isActive && pen.refresh()) {
      render();
    }
  }

  function resize() {      
  
    
    vw = canvas.width  = window.innerWidth; 
    vh = canvas.height = window.innerHeight; 
    
    ratio = vw / vh;
    
    clearCanvas();
  }

  function clearCanvas() {
    context.clearRect(0, 0, vw, vh);
    context.lineCap = "round";
    context.lineJoin = "round";
  }

  function lerp(min, max, t) {
    return min + (max - min) * t;
  }  

  init();
}




              
            
!
999px

Console