cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

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.

            
              <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 75">
  <path d="M15,35 C15,60 50,60 50,35 50,10 85,10 85,35" id="bezier" fill="none" stroke="hsla(0, 75%, 50%, 0.2)" stroke-width="0.5" />
  <text font-family="Source Sans Pro" font-size="6" fill="hsla(210, 75%, 50%, 0.85)">
    <textPath xlink:href="#bezier" id="tp" startOffset="10%">
     Text on an interactive bezier path!
    </textPath>
  </text>
</svg>
<label for="sOffset">Text start offset:</label>
<input type="range" min="0" max="100" value="10" id="sOffset">
            
          
!
            
              @import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro);
*, *::before, *::after {
  margin: 0;
  padding: 0;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html {
  font-family: Source Sans Pro, serif;
  min-height: 100%;
}
body {
  background-color: hsl(0, 0%, 10%);
  color: hsl(0, 0%, 85%);
  margin: .5em;
}
svg {
  background-color: hsl(0, 0%, 12%);
  stroke-linecap: round;
}
circle {
  fill: hsla(0, 0%, 100%, 0.02);
  stroke: hsla(120, 0%, 0%, 0.25);
  stroke-width: 0.05;
  -ms-touch-action: none;
  touch-action: none;
}
circle:hover {
  fill: hsla(0, 0%, 75%, 0.25);
}
circle:active {
  fill: hsla(0, 0%, 75%, 0.5);  
}
line {
  stroke: hsla(60, 75%, 50%, 0.1);
  stroke-width: 0.5;
}
            
          
!
            
              var svgEl = document.querySelector("svg");
var pathEl = document.querySelector("#bezier");
var pathElSegList = pathEl.pathSegList;
var evtStartTarget = {};
var pointArray = [{x: 15, y: 35}, {x: 15, y: 60}, {x: 50, y: 60}, {x: 50, y: 35}, {x: 50, y: 10}, {x: 85, y: 10}, {x: 85, y: 35}];

document.querySelector("#sOffset").addEventListener("input", function(ev) {
  document.querySelector("#tp").setAttribute("startOffset", ev.target.value + "%");
});

// draw initial UI
svgEl.appendChild(uiLine(pointArray[0].x, pointArray[0].y, pointArray[1].x, pointArray[1].y, 1));
svgEl.appendChild(uiLine(pointArray[2].x, pointArray[2].y, pointArray[3].x, pointArray[3].y, 2));
svgEl.appendChild(uiLine(pointArray[3].x, pointArray[3].y, pointArray[4].x, pointArray[4].y, 3));
svgEl.appendChild(uiLine(pointArray[5].x, pointArray[5].y, pointArray[6].x, pointArray[6].y, 4));

for (var i = 0; i < pathElSegList.numberOfItems; i++) {
  svgEl.appendChild(uiDot(pathElSegList.getItem(i).x, pathElSegList.getItem(i).y, 3*i));
  if ("x1" in pathElSegList.getItem(i)) {
    svgEl.appendChild(uiDot(pathElSegList.getItem(i).x1, pathElSegList.getItem(i).y1, 3*(i-1)+1));
  }
  if ("x2" in pathElSegList.getItem(i)) {
    svgEl.appendChild(uiDot(pathElSegList.getItem(i).x2, pathElSegList.getItem(i).y2, 3*(i-1)+2));
  }
}

function uiLine(x1, y1, x2, y2, i) {
  var lineEl = document.createElementNS("http://www.w3.org/2000/svg", "line");
  lineEl.setAttribute('x1', x1);
  lineEl.setAttribute('y1', y1);
  lineEl.setAttribute('x2', x2);
  lineEl.setAttribute('y2', y2);
  lineEl.setAttribute('id', "line" + i);
  return lineEl;
}

function uiDot(x, y, i) {
  var circleEl = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  circleEl.setAttribute('cx', x);
  circleEl.setAttribute('cy', y);
  circleEl.setAttribute('r', 2);
  circleEl.addEventListener("mousedown", mdEvent, true);
  circleEl.addEventListener("touchstart", mdEvent, true);
  circleEl.setAttribute("id", "dot" + i);
  return circleEl;
}

// mouse/touch event handling
function mdEvent (evt) {
  evt.preventDefault();
  evtStartTarget = evt.target;
  document.addEventListener("mousemove", mmEvent, true);
  document.addEventListener("mouseup", muEvent, true);
  document.addEventListener("touchmove", mmEvent, true);
  document.addEventListener("touchend", muEvent, true);
}

function mmEvent (evt) {
  var pointIndex = 0;
  evt.preventDefault();
  var p = svgEl.createSVGPoint();
  if(evt.targetTouches) {
    p.x = evt.targetTouches[0].clientX;
    p.y = evt.targetTouches[0].clientY;
  } else {
    p.x = evt.clientX;
    p.y = evt.clientY;
  }
  p = coordinateTransform(p);
  pointIndex = parseInt(evtStartTarget.getAttribute("id").slice(-1), 10);
  pointArray[pointIndex].x = p.x.toFixed(2);
  pointArray[pointIndex].y = p.y.toFixed(2);
  updateUI(pointIndex);
}

function muEvent (evt) {
  evt.preventDefault();
  document.removeEventListener("mousemove", mmEvent, true);
  document.removeEventListener("mouseup", muEvent, true);
  document.removeEventListener("touchmove", mmEvent, true);
  document.removeEventListener("touchend", muEvent, true);    }

// update the ui
function updateUI (index) {
  // draw dots
  evtStartTarget.setAttribute("cx", pointArray[index].x);
  evtStartTarget.setAttribute("cy", pointArray[index].y);
  // draw lines
  switch (index) {
    case 0:
    case 1:
      var lineEl = document.querySelector("#line1");
      lineEl.setAttribute("x1", pointArray[0].x);
      lineEl.setAttribute("y1", pointArray[0].y);
      lineEl.setAttribute("x2", pointArray[1].x);
      lineEl.setAttribute("y2", pointArray[1].y);
      break;
    case 2:
    case 3:
    case 4:
      var lineEl = document.querySelector("#line2");
      lineEl.setAttribute("x1", pointArray[2].x);
      lineEl.setAttribute("y1", pointArray[2].y);
      lineEl.setAttribute("x2", pointArray[3].x);
      lineEl.setAttribute("y2", pointArray[3].y);
      var lineEl = document.querySelector("#line3");
      lineEl.setAttribute("x1", pointArray[3].x);
      lineEl.setAttribute("y1", pointArray[3].y);
      lineEl.setAttribute("x2", pointArray[4].x);
      lineEl.setAttribute("y2", pointArray[4].y);
      break;
    case 5:
    case 6:
      var lineEl = document.querySelector("#line4");
      lineEl.setAttribute("x1", pointArray[5].x);
      lineEl.setAttribute("y1", pointArray[5].y);
      lineEl.setAttribute("x2", pointArray[6].x);
      lineEl.setAttribute("y2", pointArray[6].y);
  }
  // draw bezier path
  pathElSegList[0].x = pointArray[0].x;
  pathElSegList[0].y = pointArray[0].y;
  pathElSegList[1].x1 = pointArray[1].x;
  pathElSegList[1].y1 = pointArray[1].y;
  pathElSegList[1].x2 = pointArray[2].x;
  pathElSegList[1].y2 = pointArray[2].y;
  pathElSegList[1].x = pointArray[3].x;
  pathElSegList[1].y = pointArray[3].y;
  pathElSegList[2].x1 = pointArray[4].x;
  pathElSegList[2].y1 = pointArray[4].y;
  pathElSegList[2].x2 = pointArray[5].x;
  pathElSegList[2].y2 = pointArray[5].y;
  pathElSegList[2].x = pointArray[6].x;
  pathElSegList[2].y = pointArray[6].y;
}

//  http://msdn.microsoft.com/en-us/library/ie/hh535760(v=vs.85).aspx
function coordinateTransform(screenPoint) {
  var CTM = svgEl.getScreenCTM();
  return screenPoint.matrixTransform( CTM.inverse() );
}
            
          
!
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