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.

            
              <!-- Note I am using Bootstrap to make my life easier centering and resizing the SVG element -->
<div class="container-fluid">
  <div class="row">
    <div class="col-sm-12">
      <br />
      <h5><i>Hover click and drag example</i></h5>
    <svg id='snappy' class='img-responsive' viewBox="0 0 1200 700"></svg>         
    </div>
  </div>



            
          
!
            
              body{
  background-color: #111;
  margin: 0;
}

h5{
  color: tomato;
}


#snappy{
  margin-top: 20px;
  background-color: black;
  position: relative; 
  display: block;
}
            
          
!
            
              // Use this as a tempalte/reference for common interactions with snap.


var s = Snap("#snappy");
var circle = s.circle(600, 350, 100);

circle.attr({
  fill: "tomato",
  stroke: "cornflowerblue",
  strokeWidth: 20
});

// Hover :
circle.hover(hoverOver, hoverOut);
// Click :
circle.mouseup(mouseUp);
circle.mousedown(mouseDown);
//onmove, onstart, onend
circle.drag(onMove, onStart, onEnd);

// Touch:
// mouseup mousedown seem to also workon mobile, but to be sure,
// you can use touch events:
circle.touchstart(touchStartHandler);
circle.touchmove(touchMoveHandler);

// Handlers :

// TOUCH (for mobile devices ):
function touchStartHandler(){
   circle.attr({
    fill: "white",
    stroke: "purple"
  });
}


function touchMoveHandler(){
     circle.attr({
    fill: "purple",
    stroke: "white"
  });
}
// DRAG:

// Normally you can just use element.drag(), but since we are using a responsive
// viewbox, there is some coordinate translation handled in the onMove function,
// you can alos use a plugin:
// http://stackoverflow.com/questions/42570545/snap-svg-drag-coordinates-inside-a-responsive-viewbox

function onMove(dx, dy, ev, x, y){
  var tdx, tdy;
    var snapInvMatrix = this.transform().diffMatrix.invert();
    snapInvMatrix.e = snapInvMatrix.f = 0;
    tdx = snapInvMatrix.x(dx, dy);
    tdy = snapInvMatrix.y(dx, dy);
    this.transform("t" + [tdx, tdy] + this.data('ot'));
}

function onStart(x, y, ev){
   this.data('ot', this.transform().local);
}

function onEnd(){
 // console.log("endedDrag");
}

// HOVER
function hoverOver() {
  document.body.style.cursor = "pointer";
  circle.attr({
    fill: "cornflowerblue",
    stroke: "tomato"
  });
}

function hoverOut() {
  document.body.style.cursor = "default";
  circle.attr({
    fill: "tomato",
    stroke: "cornflowerblue"
  });
}

//CLICK
function mouseDown() {
  circle.attr({
    fill: "grey",
    stroke: "dimgrey"
  });
}

function mouseUp() {
  circle.attr({
    fill: "cornflowerblue",
    stroke: "tomato"
  });
}
            
          
!
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.

Console