SVG 'Isometrics'

Experiments with making isometric-style layouts in SVG.

by visioguy

HTML

<div class="engineering-paper"></div>

<div class="top">

  <div class="instructions">

    <h3>SVG Isometric Playground</h3>
    <div>Test out bits and pieces of SVG code, while having built-in pan & zoom.</div>
    <ul>
      <li>SVG area below has pan and zoom, courtesy of d3.js</li>
      <li>Zoom the SVG drawing using the <em>mouse wheel</em>, pan using <em>click+drag</em>.</li>
      <li>Page 'paper' is 1000 x 1000 units</li>
      <!-- <li></li> -->
    </ul>

  </div> <!-- ...instructions --> 
  
  
  <div class="buttons">
    <div><span>Rotation angle: </span>
         <span class="rotation-angle-output">45</span>
         <span>°</span>
    </div>
    <!-- <output id="rotationAngle">45</output> -->
    <div>
      <input class="bar rotation-angle-input" 
             type="range" min="-180" max="180" value="45" step="5" />      
    </div>
    <div><span>Tilt: </span>
         <span class="tilt-output">0.7</span>
    </div>
    <div>
      <input class="bar tilt-input" 
             type="range" min="0.3" max="1.0" value="0.7" step="0.1" />      
    </div>    
    <!-- <div class="button">Button</div>
    <div class="button">Button</div>
    <div class="button">Button</div> -->
  </div>

</div> <!-- ...top -->

<div class="container bg-shade flxgrow">  
    
  <div class="zones">    
    <div class="zones-ell"></div>
    <div class="zone horz-zones"></div>
    <div class="zone vert-zones"></div>
  </div>
  
  <svg viewBox="-550 -550 1100 1100">
    <!-- transform="translate(236.67,99.79) scale(0.52)" -->      
    <g class="page">    
      <g class="skew" transform="scale(1, 0.7)">
        <g class="rotate" transform="rotate(30)">
    
    <!-- Page background and axes -->
    <rect class="paper"
          x="-500" y="-500" width="1000" height="1000" />
    <path class="axes"
          d="M-10000 0  L10000 0  M0 -10000  L0 10000" />          
    <!-- ------------------------ -->
   
    <rect class="node" x="-50" y="-40" width="100" height="80"...

CSS

@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');

.bg-shade {
  background: #0001;
}
.br5 {
  border-radius: 5px;
}
.br10 {
  border-radius: 10px;
}
.flxcol {
  display: flex;
  flex-direction: column;
}
.flxrow {
  display: flex;
  flex-direction: row;
}
.flxgrow {
  flex: 1 0;
}
.pd5 {
  padding: 5px;
}
b {
  font-weight: bold;
}


html, body {
  height: 100%;
}

body {
  align-items: stretch;  
  font-family: 'Comic Neue', sans-serif;
  color: #118b; /* a ballpoint pen blue */
/*   width: 100vw;
  height: 100vh; */  
  display: flex;
  flex-direction: column;
  margin: 0px;  
  /* box-sizing: border-box; */
}

.engineering-paper {
  
  --gridline-color: #A3C0AA22;
  --paper-color: #EBF5DD;
  
  background-color: var(--paper-color);
  
  background-image:
    linear-gradient(var(--gridline-color) 2px, transparent 2px), 
    linear-gradient(90deg, var(--gridline-color) 2px, transparent 2px), 
    linear-gradient(var(--gridline-color) 1px, transparent 1px), 
    linear-gradient(90deg, var(--gridline-color) 1px, var(--paper-color) 1px);
  background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;
  background-position: -2px -2px, -2px -2px, -1px -1px, -1px -1px;
  
  height: 100%;
  left: 0;
  opacity: 1;
  pointer-events: none;
  position: absolute;
  top: 0;
  width: 100%;
  z-index: -10;
}

h3 {
  margin: 0px 0px 10px 0px;
}

.top {
  background: #0001;
  display: grid;
}

/* jsfiddle puts a blue button in the top-right corner when a
   fiddle is in "show" mode, so we'll add a column to the grid,
   to get stuff out of the way of that button */
@media (max-width:1000px){
  .top {
    grid-template-columns: 3fr auto;
  }
}

@media (min-width:1000px){
  .top {
    grid-template-columns: 3fr auto 1fr;
  }
}

.instructions {  
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  height: auto;
  margin-bottom: 10px;
  padding: 10px;
  position: relative;
}

ul {
  /* font-size: smaller; */
}
.buttons {
 ...

JavaScript

const dsvg = d3.select("svg");
const dpg = d3.select("g.page");

_initZoom();

d3.select("input.rotation-angle-input")
	.on("input", function() {
  	const angDeg = this.value;
  	// Update the text:
    d3.select(".rotation-angle-output").text(angDeg);
    // Rotate the svg:
    d3.select("g.rotate")
    	.attr("transform", `rotate(${angDeg})`);
  });
d3.select("input.tilt-input")
	.on("input", function() {
  	const val = this.value;
  	// Update the text:
    d3.select(".tilt-output").text(val);
    // Rotate the svg:
    d3.select("g.skew")
    	.attr("transform", `scale(1, ${val})`);
  });  

// Fake click a button:
//d3.select(".button.zones-4").node().click();

function _initZoom() {
  
  // Set up a zoom behavior object:
  let zoomer = d3.zoom().on("zoom", zoomD3Default)
    zoomer.scaleExtent([1 / 2, 4]);
    dsvg.call(zoomer);

  function zoomD3Default() {
    //console.log(d3.event.transform);
    let zt = d3.event.transform;
    dpg.attr("transform", zt); 
  }  
}