css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              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);
}
            
          
!
            
              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
🕑 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