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.

            
              <pre>&lt;svg width="<span id="preW"></span>" height="<span id="preH"></span>" viewBox="<span id="preFromX"> </span> <span id="preFromY"> </span> <span id="preToX"></span> <span id="preToY"></span>" preserveAspectRatio="<span id="preAR">xMaxYMax</span>" &gt;&lt;/svg&gt;</pre>
<svg width="600" height="350"></svg>
<form>
  <fieldset>
    <legend>preserveAspectRatio ="&lt;align&gt; [meet|slice|none] "</legend>

    <p id="msn">
      <input type="radio" name="msn" value="none" checked> none
      <input type="radio" name="msn" value="meet"> meet
      <input type="radio" name="msn" value="slice"> slice </p>
    <p id="xctrl">
      <input type="radio" name="xctrl" value="xMax" checked> <em>xMax</em>
      <input type="radio" name="xctrl" value="xMid"> <em>xMid</em>
      <input type="radio" name="xctrl" value="xMin"> <em>xMin</em>
    </p>
    <p id="yctrl">
      <input type="radio" name="yctrl" value="YMax" checked> <em>YMax</em>
      <input type="radio" name="yctrl" value="YMid"> <em>YMid</em>
      <input type="radio" name="yctrl" value="YMin"> <em>YMin</em>
    </p>

  </fieldset>

  <fieldset>
    <legend>viewBox="fromX fromY width height"</legend>
    <p><label>fromX: </label><input type="range" id="fromX"> <span id="spanFromX"></span></p>
    <p><label>fromY: </label><input type="range" id="fromY"> <span id="spanFromY"></span></p>

    <p><label>width: </label><input type="range" min="100" max="1000" value="600" id="toX"> <span id="spanToX"></span></p>
    <p><label>height: </label><input type="range" min="100" max="1000" value="350" id="toY"> <span id="spanToY"></span></p>
  </fieldset>
</form>
            
          
!
            
              * {
  font-family: "Courier New", Courier, monospace;
  font-size: 14px;
  padding: 0;
  margin: 0;
}

body {
  background-color: GhostWhite;
}

p,
legend {
  margin: 1em;
}

em {
  font-style: normal;
}

svg {
  border: 1px solid #d4d4d4;
  display: block;
  margin: 1em auto;
  background-color: white;
}

pre {
  background: black;
  color: white;
  padding: 1em;
  margin: .5em;
  text-align: center;
}

form {
  margin: 1em auto;
}

fieldset {
  margin: 1.2em 0;
  border: 1px solid #d4d4d4;
  background-color: white;
}

legend {
  padding: 0 1em;
  text-align: center
}

label {
  display: inline-block;
  width: 12%;
}

[type='range'] {
  width: 74%;
}

[type='radio']:disabled {
  opacity: .2;
}

[type='radio']:disabled + em {
  color: #d9d9d9;
}

form span {
  display: inline-block;
  width: 10%;
  padding-left: 1em;
}
            
          
!
            
              var svgCanvas = document.querySelector("svg");
var svgW = parseInt(window.getComputedStyle(svgCanvas, null).getPropertyValue("width")); //console.log(svgW)
var svgH = parseInt(window.getComputedStyle(svgCanvas, null).getPropertyValue("height")); //console.log(svgH)
document.querySelector("form").style.width = svgW + "px";
document.querySelector("#toX").value = svgW;
document.querySelector("#toY").value = svgH;
document.querySelector("#preW").innerHTML = svgW;
document.querySelector("#preH").innerHTML = svgH;

var preToX = document.querySelector("#preToX");
var preToY = document.querySelector("#preToY");
var preFromX = document.querySelector("#preFromX");
var preFromY = document.querySelector("#preFromY");
var preAR = document.querySelector("#preAR");

var spanFromX = document.querySelector("#spanFromX");
var spanFromY = document.querySelector("#spanFromY");
var spanToX = document.querySelector("#spanToX");
var spanToY = document.querySelector("#spanToY");

var inputFromX = document.querySelector("#fromX");
var inputFromY = document.querySelector("#fromY");
inputFromX.setAttribute("min", -svgW);
inputFromX.setAttribute("max", svgW);
inputFromX.setAttribute("value", -300);
inputFromY.setAttribute("min", -svgH);
inputFromY.setAttribute("max", svgH);
inputFromY.setAttribute("value", -140);

var inputToX = document.querySelector("#toX");
var inputToY = document.querySelector("#toY");

var radioXctrl = document.querySelectorAll("input[name='xctrl']");
var radioYctrl = document.querySelectorAll("input[name='yctrl']");

function drawContent() {
  var newG = document.createElementNS("http:\/\/www.w3.org/2000/svg", "g");

  var redPath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  redPath.setAttributeNS(null, "fill", "red");
  redPath.setAttributeNS(null, "d", "M-26,13 l72-124c-10-2-21-4-33-4c-36,0-69,13-95,34 l55,95 L-26.411,12.856z");
  newG.appendChild(redPath);

  var orangePath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  orangePath.setAttributeNS(null, "fill", "orange");
  orangePath.setAttributeNS(null, "d", "M156-10c-14-44-47-79-90-95 L11-10 H156z");
  newG.appendChild(orangePath);

  var goldPath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  goldPath.setAttributeNS(null, "fill", "gold");
  goldPath.setAttributeNS(null, "d", "M160,5 H47 l4,8 l72,124 c24-27,40-62,40-101 C163,25,162,15,160,5 z");
  newG.appendChild(goldPath);

  var greenPath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  greenPath.setAttributeNS(null, "fill", "#64b150");
  greenPath.setAttributeNS(null, "d", "M39,80l-59,101 c11,2,21,4,33,4 c36,0,69-13,95-34L52,56 L39,80 z");
  newG.appendChild(greenPath);

  var bluePath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  bluePath.setAttributeNS(null, "fill", "DeepSkyBlue");
  bluePath.setAttributeNS(null, "d", "M-131,80 c14,44,47,79,90,95 L14,80 H-131z");
  newG.appendChild(bluePath);

  var purplePath = document.createElementNS("http:\/\/www.w3.org/2000/svg", "path");
  purplePath.setAttributeNS(null, "fill", "MediumOrchid");
  purplePath.setAttributeNS(null, "d", "M-39,35L-98-66C-122-39-138-4-138,35 c0,10,1,20,3,30h112 L-39,35 z");
  newG.appendChild(purplePath);

  svgCanvas.appendChild(newG);
}

function drawNewLine(x1, y1, x2, y2) {
  var newLine = document.createElementNS("http:\/\/www.w3.org/2000/svg", "line");
  newLine.setAttributeNS(null, "x1", x1);
  newLine.setAttributeNS(null, "y1", y1);
  newLine.setAttributeNS(null, "x2", x2);
  newLine.setAttributeNS(null, "y2", y2);
  newLine.setAttributeNS(null, "stroke", "rgb(0, 0, 255)");
  newLine.setAttributeNS(null, "stroke-width", "1");
  svgCanvas.appendChild(newLine);
}

function newText(x, y, db, ta, txt) {
  var newText = document.createElementNS("http:\/\/www.w3.org/2000/svg", "text");
  newText.setAttributeNS(null, "x", x);
  newText.setAttributeNS(null, "y", y);
  newText.setAttributeNS(null, "dominant-baseline", db);
  newText.setAttributeNS(null, "text-anchor", ta);
  newText.textContent = txt;
  svgCanvas.appendChild(newText);
}

function drawRullers() {
  // rulers
  for (var i = 0; i <= toX; i += 10) {
    y2 = 10;
    if (i % 50 == 0) {
      y2 = 15;
    }
    if (i > 0 && i % 100 == 0) {
      y2 = 20;
      newText(i, y2 + 2, "hanging", "middle", i)
    }
    drawNewLine(i, 0, i, y2);
  }
  for (var j = 0; j <= toY; j += 10) {
    x2 = 10;
    if (j % 50 == 0) {
      x2 = 15;
    }
    if (j > 0 && j % 100 == 0) {
      x2 = 20;
      newText(x2 + 2, j, "middle", "start", j)
    }
    drawNewLine(0, j, x2, j);
  }
}

function clearRect() {
  while (svgCanvas.firstChild) {
    svgCanvas.removeChild(svgCanvas.firstChild);
  }

}

function drawMarkers(e) {
  console.log(inputFromX);

  apX = document.querySelector('input[name="xctrl"]:checked').value;
  apY = document.querySelector('input[name="yctrl"]:checked').value;
  msn = document.querySelector('input[name="msn"]:checked').value;

  // viewBox
  if(e.type=="load"){
  fromX = document.querySelector("#fromX").getAttribute("value");
  fromY = document.querySelector("#fromY").getAttribute("value");  
  }else{
  fromX = document.querySelector("#fromX").value;
  fromY = document.querySelector("#fromY").value;   
  }
  toX = document.querySelector("#toX").value;
  toY = document.querySelector("#toY").value;

  // preserveAspectRatio
  if (msn == "none") {
    alignParam = "none";

    for (var i = 0; i < radioXctrl.length; i++) {
      radioXctrl[i].disabled = true;
    }
    for (var j = 0; j < radioYctrl.length; j++) {
      radioYctrl[j].disabled = true;
    }
  } else {
    alignParam = apX + apY + " " + msn;
    for (var i = 0; i < radioXctrl.length; i++) {
      radioXctrl[i].disabled = false;
    }
    for (var j = 0; j < radioYctrl.length; j++) {
      radioYctrl[j].disabled = false;
    }

  }

  // <pre>
  preFromX.innerHTML = fromX;
  spanFromX.innerHTML = fromX;
  preFromY.innerHTML = fromY;
  spanFromY.innerHTML = fromY;
  preToX.innerHTML = toX;
  spanToX.innerHTML = toX;
  preToY.innerHTML = toY;
  spanToY.innerHTML = toY;
  preAR.innerHTML = alignParam;

  svgCanvas.setAttributeNS(null, "viewBox", fromX + " " + fromY + " " + toX + " " + toY);
  svgCanvas.setAttributeNS(null, "preserveAspectRatio", alignParam);

  clearRect();
  drawContent();
  drawRullers();
}

window.addEventListener("load", drawMarkers, false);
xctrl.addEventListener("change", drawMarkers, false);
yctrl.addEventListener("change", drawMarkers, false);
msn.addEventListener("change", drawMarkers, false);
inputFromX.addEventListener("input", drawMarkers, false);
inputFromY.addEventListener("input", drawMarkers, false);
inputToX.addEventListener("input", drawMarkers, false);
inputToY.addEventListener("input", drawMarkers, false);
            
          
!
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