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 id="box">
  <div id="spider">
    <div class="legs"></div>
    <div class="legs"></div>
    <div class="legs"></div>
    <div class="legs"></div>
  </div>
</div>
<p>Click and swipe the spider!</p>
              
            
!

CSS

              
                body { padding:0; margin: 0; background:#888; }

p { position:absolute; top:0; left:0; margin-left:360px; font-family:sans-serif,verdana,arial,helvetica; color:#fff; }

#spider { width:50px; height:50px; display:block; position:absolute; background-color:#000; border-radius:50px; left:200px; top:100px; cursor:pointer;}

#box { width:350px; height:350px; background:#fff;}

/* left eye */
#spider:before {
  width:12px; height:12px; border-radius:12px; background-color:green;
  position:absolute; content:" "; display:block; left:15px; z-index:10;  
}

/* right eye */
#spider:after {
  width:12px; height:12px; border-radius:12px; background-color:green;
  position:absolute; content:" "; display:block; left:30px; top:3px; z-index:10;  
}

/* a pair of legs */
.legs { width:80px; height:4px; background-color:#000; position:absolute; left:-15px; top:21px; }

/* rotate 3 pairs around the body */
.legs:nth-child(1) {
  transform:rotate(25deg);
}
.legs:nth-child(2) {
  transform:rotate(50deg);
}
.legs:nth-child(3) {
  transform:rotate(-25deg);
}
              
            
!

JS

              
                var MAX_BOUNDS = 300;
var STEP = 10;
var startX = -1, startY = -1;
var spiderOffsetX, spiderOffsetY;

var spider = document.getElementById("spider");

spider.onmousedown = function(event) {
  startX = event.pageX;
  startY = event.pageY;
  // the spider offset is the distance between
  // the coordinates of the spider and where
  // you actually clicked on the body
  spiderOffsetX = startX - spider.offsetLeft;
  spiderOffsetY = startY - spider.offsetTop;
};

// set this to return false so we don't
// run into the default browser behavior
// for drag
spider.ondragstart = function() { return false }

document.onmousemove = function(event) {
  var y = event.pageY, x = event.pageX;
  // only allow if spider was clicked and
  // you're within the bounds
  if (startX > 0 && startY > 0 && y > 0 && y < MAX_BOUNDS && x > 0 && x < MAX_BOUNDS) {
    spider.style.top = (y - spiderOffsetY) + "px";
    spider.style.left = (x - spiderOffsetX)  + "px";
  }
};

document.onmouseup = function(event) {
  if (startX > 0 && startY > 0) {
  	swipeSpider(startX, startY, event.pageX, event.pageY);
  }
  startX = startY = -1;
};

// get the point offscreen where the spider 
// is flung to, based on the direction you
// swipes it
function swipeSpider(fromX, fromY, toX, toY) {
  // using an arbitrary x value off the screen
  var dx1 = (MAX_BOUNDS + 100);
  if (fromX - toX < 0) { // if to left
    dx1 = -100;
  }
  var dy1 = getY(fromX, fromY, toX, toY, dx1);
  animateSpider(toX, toY, dx1, dy1);
}

// use slope equation to get the
// y value when given a new x value
function getY(fromX, fromY, toX, toY, newX) {
  var slope = (fromY - toY) / (fromX - toX);
  return slope * (newX - fromX) + fromY;
}

// animate in steps
function animateSpider(fromX, fromY, toX, toY) {
  var stepX;
  if (fromX - toX < 0) {
    stepX = fromX - STEP;
  } else {
    stepX = fromX + STEP;
  }
  var stepY = getY(fromX, fromY, toX, toY, stepX);
  // if slope is undefined
  if (stepY == Number.POSITIVE_INFINITY) {
    stepY = fromY + STEP;
    stepX = fromX;
  } else if (stepY == Number.NEGATIVE_INFINITY) {
    stepY = fromY - STEP;
    stepX = fromX;
  }
  // subtract the spider offset 
  stepY -= spiderOffsetY;
  stepX -= spiderOffsetX;
  
  // only animate if we're within the bounds
  if (stepY < MAX_BOUNDS && stepY > 0 && stepX < MAX_BOUNDS && stepX > 0) {
    spider.style.top = stepY + "px";
    spider.style.left = stepX + "px";
  
    window.setTimeout(function() {
      animateSpider(stepX + spiderOffsetX, stepY + spiderOffsetY, toX, toY);
    }, 10);
  }
}
              
            
!
999px

Console