Source code of plot #030 back to plot

Download full working sketch as 030.tar.gz.
Unzip, then start a local web server and load the page in a browser.

let libsPending = 0;
let sketchFun;

function setSketch(fun) {
  if (typeof document !== 'undefined') {
    document.addEventListener('DOMContentLoaded', () => {
      sketchFun = fun;
      if (libsPending == 0) sketchFun();
    });
  }
}

function scriptname() {
  const d = new Date();
  const dateStr = d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" +
    ("0" + d.getDate()).slice(-2) + "!" +
    ("0" + d.getHours()).slice(-2) + "-" + ("0" + d.getMinutes()).slice(-2);
  return '57-raindrop-squares' + "-" + dateStr;
}

function loadLib(name) {

  var script = document.createElement('script');
  script.src = "lib/" + name + ".js";
  script.onload = function () {
    --libsPending;
    if (libsPending == 0 && sketchFun) sketchFun();
  };
  ++libsPending;
  document.head.appendChild(script);
}

function init(w, h) {
  document.title = scriptname();

  paper.install(window);
  paper.settings.insertItems = false;

  const elmDrawing = document.getElementById("drawing");
  elmDrawing.innerHTML = "<canvas id='paper-canvas'></canvas>";
  paper.setup("paper-canvas");
  paper.project.activeLayer.name = "0-base";

  const elmCanvas = document.getElementById("paper-canvas");
  elmCanvas.width = w;
  elmCanvas.height = h;
  const pxWidth = elmDrawing.clientWidth - 1;
  elmCanvas.style.width = pxWidth + "px";
  elmCanvas.style.height = (pxWidth * h / w) + "px";

  const elmRender = document.getElementById("render");
  elmRender.addEventListener("click", function (e) {
    e.preventDefault();
    e.stopPropagation();
    elmRender.setAttribute("href", "");
    setTimeout(function () {
      renderSvg(w, h);
      elmRender.style.display = "none";
      document.getElementById("download").style.display = "block";
    }, 50);
  });
}

function renderSvg(w, h) {
  const elmSvg = project.exportSVG({
    asString: false,
    precision: 3,
    matchShapes: false,
  });
  elmSvg.setAttribute("width", (w / 10) + "mm");
  elmSvg.setAttribute("height", (h / 10) + "mm");
  elmSvg.setAttribute("viewBox", "0,0," + w + "," + h);

  elmSvg.setAttribute("xmlns:sodipodi", "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd");
  elmSvg.setAttribute("xmlns:inkscape", "http://www.inkscape.org/namespaces/inkscape");

  let svgStr = elmSvg.outerHTML;
  svgStr = svgStr.replaceAll("<path></path>", "");
  svgStr = svgStr.replaceAll(/<g id="([^"]+)"/g, "$& inkscape:groupmode='layer' inkscape:label='$1'");

  // <g id="0-base"

  var file;
  var data = [];
  data.push(svgStr);
  var properties = { type: 'image/svg+xml' };
  try { file = new File(data, scriptname() + ".svg", properties); }
  catch { file = new Blob(data, properties); }
  var url = URL.createObjectURL(file);
  document.getElementById('download').href = url;
  document.getElementById('download').download = scriptname() + ".svg";
}

function info(str) {
  document.getElementById("info").getElementsByTagName("label")[0].textContent = str;
}

export { init, renderSvg, info, loadLib, setSketch };