cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <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
Loading ..................

Console