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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!-- 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>



              
            
!

CSS

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

h5{
  color: tomato;
}


#snappy{
  margin-top: 20px;
  background-color: black;
  position: relative; 
  display: block;
}
              
            
!

JS

              
                // 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

Console