Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>
  <a href="#" class="link-download">Download displayed SVG</a>
</p>

<svg id="svgField" width="1600" height="3500" viewBox="0 0 1600 3500" xmlns="http://www.w3.org/2000/svg">
</svg>
              
            
!

CSS

              
                svg {
  border: solid thin gray;
}

              
            
!

JS

              
                const SVG = document.getElementById("svgField");
const L = 100;

const R = 0.6; // 0.7; // 1;
const M = 100;

const N = 8; //6; // 8
const θ = (Math.PI * 2) / N;

const CURVE_MARGINE = 0.1;
const WING_MARGINE = 0.1 * 1 / 2;

class Vector2D {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  lift(surface) {
    return new Vector(this.x, this.y, surface(this.x, this.y));
  }

  toArray() {
    return [this.x, this.y];
  }

  m(t) {
    return new Vector2D(this.x * t, this.y * t);
  }

  plus(a) {
    return new Vector2D(this.x + a.x, this.y + a.y);
  }

  minus(a) {
    return this.plus(a.m(-1));
  }

  orthogonal() {
    return new Vector2D(this.y, -this.x); // turn right 90 degree
  }

  ratio(p, a) {
    return this.plus(a.minus(this).m(p));
  }

  innerProduct(v) {
    return this.x * v.x + this.y * v.y;
  }

  norm() {
    return Math.sqrt(this.innerProduct(this));
  }

  normalize() {
    return this.m(1 / this.norm());
  }

  center(v) {
    return this.ratio(1 / 2, v);
  }

  rotate(theta) {
    return new Vector2D(
      this.x * Math.cos(theta) - this.y * Math.sin(theta),
      this.x * Math.sin(theta) + this.y * Math.cos(theta)
    );
  }
}

class Vector3D {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  toArray() {
    return [this.x, this.y, this.z];
  }

  m(t) {
    return new Vector3D(this.x * t, this.y * t, this.z * t);
  }

  plus(a) {
    return new Vector3D(this.x + a.x, this.y + a.y, this.z + a.z);
  }

  minus(a) {
    return this.plus(a.m(-1));
  }

  ratio(p, a) {
    return this.plus(a.minus(this).m(p));
  }

  innerProduct(v) {
    return this.x * v.x + this.y * v.y + this.z * v.z;
  }

  norm() {
    return Math.sqrt(this.innerProduct(this));
  }

  normalize() {
    return this.m(1 / this.norm());
  }

  center(v) {
    return this.ratio(1 / 2, v);
  }
}

function drawSVGPath(lines, offset, color) {
  let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  let d = "";
  lines.forEach((line) => {
    let p0 = line[0];
    d += "M " + p0.plus(offset).x * L + " " + p0.plus(offset).y * L;
    line.slice(1).forEach((p) => {
      d += "L " + p.plus(offset).x * L + " " + p.plus(offset).y * L;
    });
  });
  path.setAttribute("d", d);
  path.setAttribute("stroke", color);
  path.setAttribute("fill", "none");
  SVG.appendChild(path);
}

function drawSVGPathEach(lines, offset, color) {
  lines.forEach((line) => {
    let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
    let d = "";
    let p0 = line[0];
    d += "M " + p0.plus(offset).x * L + " " + p0.plus(offset).y * L;
    line.slice(1).forEach((p) => {
      d += "L " + p.plus(offset).x * L + " " + p.plus(offset).y * L;
    });
    path.setAttribute("d", d);
    path.setAttribute("stroke", color);
    path.setAttribute("fill", "none");
    SVG.appendChild(path);
  });
}

// function calcPoint1(z) {
//   let x = Math.sqrt(1 - (z / R) ** 2) / ((Math.sqrt(2) - 1) ** 2 + 1);
//   let y = (Math.sqrt(2) - 1) * x;
//   return new Vector3D(x, y, z);
// }

function calcPoint1(z) {
  // x^2 + y^2 + (z/R)^2 = 1
  // y = 0
  let x = Math.sqrt(1 - (z / R) ** 2);
  let y = 0;
  return new Vector3D(x, y, z);
}

function calcPoint2(z) {
  // x^2 + y^2 + (z/R)^2 = 1
  // y = tan(θ) * x
  let x = Math.sqrt(1 - (z / R) ** 2) / (Math.tan(θ) ** 2 + 1);
  let y = Math.tan(θ) * x;
  return new Vector3D(x, y, z);
}

function createCurveBase() {
  let path = [];
  let pm_last;
  let t = 0;
  for (var i = 0; i <= M; i++) {
    let j = i / M;
    let z = j * R;
    let p1 = calcPoint1(z);
    let p2 = calcPoint2(z);
    let pm = p1.ratio(1 / 2, p2);
    let l = p1.minus(p2).norm() / 2;
    if (pm_last) {
      t += pm.minus(pm_last).norm();
    }
    path.push(new Vector2D(l, t));
    pm_last = pm;
  }

  return path;
}

function createCurve() {
  let path = createCurveBase();

  let w = Math.max(...path.map((p) => p.x));
  let h = Math.max(...path.map((p) => p.y));

  let l = path.length;
  let p_1 = path[l - 1];
  let p_2 = path[l - 2];
  let p_margine = p_1.plus(p_1.minus(p_2).normalize().m(CURVE_MARGINE));
  path.push(p_margine);

  let reversed = path.toReversed().map((p) => new Vector2D(p.x, -p.y));
  reversed.pop();
  path = reversed.concat(path);

  let w2 =
    Math.max(...path.map((p) => p.x)) - Math.min(...path.map((p) => p.x));
  let h2 = Math.max(...path.map((p) => p.y));

  return [path, w, h, w2, h2];
}

function movePath(path, a) {
  return path.map((p) => p.plus(a));
}

function drawFace(lines, w, h, w2, h2) {}

function drawJoints(lines, w, h, w2, h2) {
  //       p0       s0
  //       |        |
  //       p1       s1
  //      /|        |\
  // p3 /  |      s2|__\ s3
  //   |  _|m       |   |
  //   |   |        |___|
  // q3 \  |      t2|  / t3
  //      \|        |/
  //       q1       t1
  //       |        |
  //       q0       t0

  let a = new Vector2D(w, 0);
  let d = w * 2 + WING_MARGINE;

  // 左側
  let p0 = new Vector2D(0, -h2);
  let q0 = new Vector2D(0, h2);
  let m0 = p0.ratio(1 / 2, q0);
  let m1 = m0.minus(a.m(1 / 2));

  let p1 = p0.ratio(1 / 8, q0);
  let p2 = p0.ratio(1 / 4, q0);
  let p3 = p2.minus(a);

  let q1 = q0.ratio(1 / 8, p0);
  let q2 = q0.ratio(1 / 4, p0);
  let q3 = q2.minus(a);

  lines.cut.push([p0, p1, p3], [q3, q1, q0], [p2, q2], [m0, m1]);
  lines.dot.push([p1, p2], [q1, q2]);
  lines.dotcut.push([p3, q3]);

  // 右側
  let dn = new Vector2D(d * N, 0);
  let s0 = p0.plus(dn);
  let t0 = q0.plus(dn);

  let s1 = s0.ratio(1 / 8, t0);
  let s2 = s0.ratio(1 / 4, t0);
  let s3 = s2.plus(a);

  let t1 = t0.ratio(1 / 8, s0);
  let t2 = t0.ratio(1 / 4, s0);
  let t3 = t2.plus(a);

  lines.cut.push([s0, s2], [t0, t2], [s1, s3], [t3, t1]);
  lines.dot.push([s3, s2, t2, t3]);
  lines.dotcut.push([s3, t3]);
}

function drawJoints2(lines, w, h, w2, h2) {
  /*
     p0           s00 s1
     |            :   |
     |            :   |s1
     |            :   |\
     |   p1       : s2|..\ s3
     |   |        :       |
    m| m0|_m1     :       |
     |   |        :       |
     |   |        :    ...|
     |   q1       : t2|  / t3
     |            :   |/
     |            :   |t1
     |            :   |
     q0           t00 t1
  */

  let a = new Vector2D(w / 3, 0);
  let d = w * 2 + WING_MARGINE;

  // 左側
  let p0 = new Vector2D(0, -h2);
  let q0 = new Vector2D(0, h2);
  let m0 = p0.ratio(1 / 2, q0).plus(a.m(2));
  let m1 = m0.plus(a);

  let p1 = p0.ratio(1 / 4, q0).plus(a.m(2));
  let q1 = q0.ratio(1 / 4, p0).plus(a.m(2));

  lines.cut.push([p1, q1], [m0, m1]);
  lines.dotcut.push([p0, q0]);

  // 右側
  let dn = new Vector2D(d * N, 0);
  let s00 = p0.plus(dn);
  let t00 = q0.plus(dn);

  let s0 = s00.plus(a.m(2));
  let t0 = t00.plus(a.m(2));

  let s1 = s0.ratio(1 / 8, t0);
  let s2 = s0.ratio(1 / 4, t0);
  let s3 = s2.plus(a.m(2));

  let t1 = t0.ratio(1 / 8, s0);
  let t2 = t0.ratio(1 / 4, s0);
  let t3 = t2.plus(a.m(2));

  lines.cut.push([s00, s0], [t00, t0], [s0, s2], [t0, t2], [s1, s3], [t3, t1]);
  lines.dot.push([s00, t00], [s3, s2], [t2, t3]);
  lines.dotcut.push([s3, t3]);
}

function drawNWings(lines) {
  let [path, w, h, w2, h2] = createCurve();

  let d = w * 2 + WING_MARGINE;

  for (let i = 0; i < N; i++) {
    let vPath = [new Vector2D(i * d, -h2), new Vector2D(i * d, h2)];
    if (i == 0) {
      //lines.dotcut.push(vPath);
    } else {
      lines.dot.push(vPath);
    }

    let path2 = movePath(path, new Vector2D(w + i * d, 0));
    lines.dot.push(path2);
  }

  let vPath = [new Vector2D(N * d, -h2), new Vector2D(N * d, h2)];
  //lines.cut.push(vPath);

  let hPath0 = [new Vector2D(0, -h2), new Vector2D(N * d, -h2)];
  lines.cut.push(hPath0);

  let hPath1 = [
    new Vector2D(0, h2),
    new Vector2D(N * d, h2)
    //new Vector2D(N * d + (w * 2 - w2), h2)
  ];
  lines.cut.push(hPath1);

  drawFace(lines, w, h, w2, h2);
  //drawJoints(lines, w, h, w2, h2);
  drawJoints2(lines, w, h, w2, h2);
}

function draw(offset) {
  const lines = {
    dot: [],
    cut: [],
    dotcut: []
  };

  drawNWings(lines);

  drawSVGPath(lines.dot, offset, "red");
  drawSVGPath(lines.cut, offset, "blue");
  drawSVGPath(lines.dotcut, offset, "green");
}

draw(new Vector2D(1, 2));

document.querySelector(".link-download").addEventListener("click", (evt) => {
  const svgContent = document.getElementById("svgField").outerHTML,
    blob = new Blob([svgContent], {
      type: "image/svg+xml"
    }),
    url = window.URL.createObjectURL(blob),
    link = evt.target;

  link.target = "_blank";
  link.download = "n_wings.svg";
  link.href = url;
});

              
            
!
999px

Console