cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <h3 class="text">Drag or click</h3>
<svg id="ghosty" viewBox="0 0 500 300">
  <defs>
    <filter id="sofGlow" height="300%" width="300%" x="-75%" y="-75%">
      <feMorphology operator="dilate" radius="4" in="SourceAlpha" result="thicken" />
      <feGaussianBlur in="thicken" stdDeviation="10" result="blurred" />
      <feFlood flood-color="rgba(0,186,255,0.4)" result="glowColor" />
      <feComposite in="glowColor" in2="blurred" operator="in" result="softGlow_colored" />
      <feMerge>
        <feMergeNode in="softGlow_colored"/>
        <feMergeNode in="SourceGraphic"/>
      </feMerge>
    </filter>
  </defs>
  
  <rect width="100%" height="100%" />
  
  <g id="ghosty" filter="url(#sofGlow)">
    <path d="
             M 0 200
             Q 125 200, 200 200
             c 0 -150, 100 -150, 100 0
             Q 375 200, 500 200
             " />

    <circle id="eyeLeft" class="eye" cx="240" cy="120" r="5" />
    <circle id="eyeLeft" class="eye" cx="260" cy="120" r="5" />
  </g>
  
</svg>
            
          
!
            
              html, body {
  height: 100%;
}

body {
 background: linear-gradient(rgb(240,240,255), rgb(200,200,215)); 
}

svg {
  border: 4px solid grey;
  width: 100%;
  max-width: 800px;
  box-sizing: border-box;
  display: block;
  margin: 0 auto;
  overflow: hidden;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  -webkit-tap-highlight-color: transparent;

}

svg:not(:root) {
    overflow: hidden;
}

#ghosty {
  stroke: rgba(255,255,255,0.8);
  stroke-width: 3px;
  fill: none;
}

rect {
  fill: rgb(30,30,30);
}

.text {
  color: rgb(110,110,110);
  margin: 0;
  text-align: center;
  padding: 10vh 0 5vh;
}
            
          
!
            
              var svg = document.getElementsByTagName("svg")[0];

svg.addEventListener("mousedown", onMouseDown);
svg.addEventListener("touchstart", onMouseDown);
svg.addEventListener("mouseup", onMouseUp);
svg.addEventListener("touchend", onMouseUp);


var Line = {
  _x: 0,
  _y: 0,
  _cx: 0,
  _cy: 0,
  _type: "Q",
  
  create: function(cx,cy,x,y,type) {
    var obj = Object.create(this);
    obj.x(x);
    obj.y(y);
    obj.cx(cx);
    obj.cy(cy);
    obj.type(type);
    
    return obj;
  },
  
  x: function(x) {
    if(x === undefined) {
      return this._x;
    } else {
      this._x = x;
    }
  },
  
  y: function(y) {
    if(y === undefined) {
      return this._y;
    } else {
      this._y = y;
    }
  },
  
  cx: function(cx) {
    if(cx === undefined) {
      return this._cx;
    } else {
      this._cx = cx;
    }
  },

  cy: function(cy) {
    if(cy === undefined) {
      return this._cy;
    } else {
      this._cy = cy;
    }
  },

  type: function(type) {
    if(type === undefined) {
      return this._type;
    } else {
      this._type = type;
    }
  },
  
  path: function() {
    return this._type + " " + this._cx + " " + this._cy + " " + this._x + " " + this._y;
  }
};


var Ghosty = {
  _start: null,
  _leftLine: null,
  _rightLine: null,
  _centralLine: null,
  _index: 0,
  path: null,
  
  create: function(start, leftLine, centralLine, rightLine) {
    var obj = Object.create(this);
    
    obj._start = start;
    obj._leftLine = leftLine;
    obj._centralLine = centralLine;
    obj._rightLine = rightLine;
    
    obj.setPath(obj._index);
    
    return obj;
  },
  
  position: function(x,y) {
    this._leftLine.x(this._leftLine.x() + x);
    this._leftLine.y(this._leftLine.y() + y);
    this._leftLine.cx(this._leftLine.cx() + x);
    this._leftLine.cy(this._rightLine.cy() + y);
    // We only want to move the control point in the right hand side
    this._rightLine.cx(this._rightLine.cx() + x);
    this._rightLine.cy(this._rightLine.cy() + y);
        
    this.setPath(this._index);
  },
  
  morph: function(index) {
    this._index = index;
    this.setPath();
  },
  
  setPath: function() {
    this.path = this._start + " " + this._leftLine.path() + " " + this._centralLine[this._index] + " " + this._rightLine.path();
  }
};




var start = "M 0 200",
    leftLine = Line.create(125,200,200,200),
    rightLine = Line.create(375,200,500,200),
    ghosty = [
      "c 0 -150, 100 -150, 100 0",
      "c 0 120, 100 120, 100 0",
      "c 0 -50, 100 -50, 100 0",
      "q 25 -25, 0 -50 h 100 q -25 25, 0 50",
      "l 0 -150",
      "l -50 -30 l 50 0 l -30 -50 l 50 20 l -30 -60 l 30 10 l 5 -30 l 20 40 l 30 -60 l 5 40 l 30 -10 l -20 40 l 30 -5 l -30 50 l 20 -10 l -10 40 l 40 -20 l -20 35",
      "q -50 -25, 0 -50 t 100 0 t 0  50"
    ],
    eyes = [
      {y:"-=60"},
      {y:"+=140"},
      {y:"-=80"},
      {y:"-=20"},
      {y:"-=40"},
      {y:"+=30"},
      {y:"+=30"}
    ];


var p = Ghosty.create(start,leftLine,ghosty,rightLine);



var timestamp = null,
    lastMouse,
    ttlGhosty = ghosty.length,
    currIndex = 0;

function onMouseDown(e) {
  e.preventDefault();
  e.stopPropagation();
  var size = svg.getBoundingClientRect();
  var mouse = getMouse(e);
  svg.addEventListener("mousemove", onMouseMove);
  svg.addEventListener("touchmove", onMouseMove);
}


function onMouseUp(e) {
  e.preventDefault();
  e.stopPropagation();
  svg.removeEventListener("mousemove", onMouseMove);
  svg.removeEventListener("touchmove", onMouseMove);
  
  // Morph    
  currIndex++;
  if(currIndex === ttlGhosty) currIndex = 0;

  p.morph(currIndex);

  TweenMax.to("path", 1, {
    morphSVG:p.path,
    ease:Elastic.easeOut
  });

  TweenMax.to(".eye", 1.2, {x:eyes[currIndex].x, y:eyes[currIndex].y, ease:Elastic.easeOut});

  
  timestamp = null;
}



function onMouseMove(e) {
  
  e.preventDefault();
  e.stopPropagation();

   if(timestamp === null) {
    timestamp = Date.now();
    lastMouse = getMouse(e);
    return;
   };

   var now = Date.now(),
   currMouse = getMouse(e),
   dx = currMouse.x - lastMouse.x,
   dy = currMouse.y - lastMouse.y;
   

   timestamp = now;
   lastMouse = currMouse;

  
   p.position(dx, dy);
  
  TweenMax.set(".eye", {
    x:"+="+dx,
    y:"+="+dy
  })
  
    
   TweenMax.set("path", {
      attr:{
       d:p.path
      },
    });

  };


function getMouse(e) {

 return {
  x:e.clientX || e.targetTouches[0].clientX,
  y:e.clientY || e.targetTouches[0].clientY
 };

};



var blink = new TimelineMax({repeat:1, yoyo:true, onComplete:loop});

blink.staggerTo(".eye", 0.04, {
  scaleY:0,
  ease:Linear.easeNone
}, 0.03);

function loop() {
  TweenMax.delayedCall(5, function() {
    blink.play(0);
  });
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console