123

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.

            
              
<h2>Single Path</h2>

<div class="svg-container">
  <div id="pointer"></div>
  <svg id="stage" version="1.1" viewBox="0 0 800 240" class="svg-content">
    <svg id="rndLine" width="800" height="300">
    </svg>
    <rect id="rect" width="800" height="240"/>
    <path id="path" d=""/>
    <circle id="p0" cx="20" cy="220" r="8"/>
    <circle id="p1" cx="380" cy="220" r="8"/>
  </svg>
</div>

<div class="controls">
    <button type="button" id="reset" class="reset">Reset</button> <button type="button" id="btnRnd" class="start">Begin</button> <button type="button" id="btnCheck" class="check">Check</button> <span class="time">10</span>
  </div>


            
          
!
            
              .container {
  /* Disables panning and selecting on touch devices */
	touch-action: none;
	user-select: none;
  position: relative;
  width: 300px;
  max-width: 300px;
  top: 0px;
}

.svg-container {
  /* Disables panning and selecting on touch devices */
  touch-action: none;
  user-select: none;
  cursor: pointer;

  /* Padding hack for cross-browser responsiveness. */
  height: 0; /* Collapse the container's height. */
  width: 90%; /* Specify any width you want (a percentage value). */
  padding-top: 27%; /* Apply a padding using the following formula: (svg height / svg width) * width-value. This formula makes sure the aspect ratio of the container equals that of the svg graphic. */
  position: relative; /* Create positioning context for the svg. */
  margin: auto;
  overflow: hidden;
}

.svg-content {
  /* Disables panning and selecting on touch devices */
  touch-action: none;
  user-select: none;
  cursor: pointer;

  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#stage {
  display: inline-block;
  overflow: hidden;
}

#stage rect {
	/* Disables panning and selecting on touch devices */
	touch-action: none;
	user-select: none;
  position: relative;
	cursor: default !important;
	fill: #ffffff;
	opacity: 0;
}

#stage circle {
  fill: #43ba8e;
	cursor: pointer !important;
}

#p0 {
	cursor: default !important;
}

#stage path {
  fill: none;
  stroke: #43ba8e;
  stroke-width: 2px;
}

/* Control Area - Buttons */
	.controls {
		position: relative;
		top: 20px;
	}

            
          
!
            
              window.addEventListener('load', function() {
  'use strict';

// Random SVG line
let rndLine = "http://www.w3.org/2000/svg";
  for (let i = 0; i < 1; i++) {
    let x1 = 20,
        y1 = 220,
        x2 = Math.random() * 770 + 20, /* The + 20 prevents the line from going left of vertical */
        y2 = Math.random() * 220;
    let line = document.createElementNS(rndLine, 'line');
    line.setAttributeNS(null, 'x1', x1);
    line.setAttributeNS(null, 'y1', y1);
    line.setAttributeNS(null, 'x2', x2);
    line.setAttributeNS(null, 'y2', y2);
    line.setAttributeNS(null, 'stroke-width', 6);
    line.setAttributeNS(null, 'stroke', "#7e2328");
    document.getElementById('rndLine').appendChild(line);

    function removeRandomLine() {
      document.getElementById('rndLine').removeChild(line);
    };

    document.querySelector('#btnRnd').onclick = function(){
      let rndLine = "http://www.w3.org/2000/svg";
        for (let i = 0; i < 1; i++) {
          let x1 = 20,
              y1 = 220,
              x2 = Math.random() * 770 + 20, /* The + 20 prevents the line from going left of vertical */
              y2 = Math.random() * 220;
          let line = document.createElementNS(rndLine, 'line');
          line.setAttributeNS(null, 'x1', x1);
          line.setAttributeNS(null, 'y1', y1);
          line.setAttributeNS(null, 'x2', x2);
          line.setAttributeNS(null, 'y2', y2);
          line.setAttributeNS(null, 'stroke-width', 6);
          line.setAttributeNS(null, 'stroke', "#7e2328");
          document.getElementById('rndLine').appendChild(line);
          document.querySelector('#btnCheck').onclick = function(){
            document.getElementById('rndLine').appendChild(line);
          };
        };
    };
  };

// Interactive Path
let svg = document.getElementById('stage'),
    field = document.getElementById('rect'),
    p0 = document.getElementById('p0'),
    p1 = document.getElementById('p1'),
    path = document.getElementById('path'),
    point = svg.createSVGPoint(),
    viewBox = svg.viewBox.baseVal,
    xy = {
      p0: {x: p0.cx.baseVal.value, y: p0.cy.baseVal.value},
      p1: {x: p1.cx.baseVal.value, y: p1.cy.baseVal.value}
    },
    OFFSET = {x: 8, y: 8},
    VIEW_WIDTH = 800,
    VIEW_HEIGHT = 240;

  function updatePath(rect, xySeg) {
    if (rect) {
      point.x = rect.left - window.pageXOffset;
      point.y = rect.top - window.pageYOffset;
      point = point.matrixTransform(svg.getScreenCTM().inverse());
      xySeg.x = point.x + OFFSET.x;
      xySeg.y = point.y + OFFSET.y;
    }
    path.setAttribute('d', 'M' + xy.p0.x + ',' + xy.p0.y + ',' + xy.p1.x + ',' + xy.p1.y + '');
  }

  let drgp1 = new PlainDraggable(p1, {containment: field, onMove: function() { updatePath(this.rect, xy.p1); }}),
    pointer = new PlainDraggable(document.getElementById('pointer'), {
      onMove: function() {
        drgp1.position();
      }
    });
  updatePath();

  function hidePath() {
    document.getElementById('path').style.visibility = 'hidden';
    document.getElementById('p1').style.visibility = 'hidden';
  };

  function showPath() {
    document.getElementById('path').style.visibility = 'visible';
    document.getElementById('p1').style.visibility = 'visible';
  };

  // Timer Functions
  class Countdown {
  	constructor() {
      this.duration = 0;
      this.elapsed = 0;
      this.isActive = false;
      this.lastFrameTime = Date.now();
      this.onTick = () => {};
      this.onCompleted = () => {};
      this.tick();
    }

    getTimeLeft() {
      const t = this.duration - this.elapsed;
      return Math.max(0, t);
    }

    pause() {
      this.isActive = false;
      return this;
    }

    reset() {
      this.elapsed = 0;
    }

    setDuration(seconds) {
      this.lastFrameTime = Date.now();
      this.duration = seconds;
      return this;
    }

    start() {
  	  this.isActive = true;
      return this;
    }

    /*wait() {
      this.isActive = true;
      return this;
      label.innerHTML = Math.ceil(countdown.getTimeLeft());
    }*/

    tick() {
    	const currentFrameTime = Date.now();
      const deltaTime = currentFrameTime - this.lastFrameTime;
      this.lastFrameTime = currentFrameTime;
      if (this.isActive) {
        this.elapsed += deltaTime / 1000;
        this.onTick(this.getTimeLeft());
        if(this.getTimeLeft() <= 0) {
          this.onCompleted();
        }
      }

      window.requestAnimationFrame(this.tick.bind(this));
    }
  }

  const countdown = new Countdown().setDuration(10);
  const label = document.querySelector('.time');
  document.querySelector('.check').addEventListener('click', () => {
    countdown.reset();
    label.innerHTML = Math.ceil(countdown.getTimeLeft());
  });

  document.querySelector('.start').addEventListener('click', () => {
    countdown.start();
  });

  countdown.onTick = (time) => {
    label.innerHTML = Math.round(time);
  };

  countdown.onCompleted = () => {
    let element = document.getElementById('rndLine');
    while (element.firstChild) {
      element.removeChild(element.firstChild);
    };
    showPath();
    countdown.pause();
  };

  document.querySelector('.reset').addEventListener('click', () => {
    let element = document.getElementById('rndLine');
    while (element.firstChild) {
      element.removeChild(element.firstChild);
    };
    hidePath();
  });

});

            
          
!
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