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

              
                <!-- Sweater 1 -->
<div>
  <svg width="480" height="480">
    <g stroke-width="1px" id="g" />
  </svg>
  <div>
    <label>Sweater 1 stroke-width</label>
    <input type="range" id="stroke-width-input" class="input-div" min="0.1" max="20" value="0" step="0.1" />
  </div>
  <button id="button1">Recreate Sweater 1</button>
</div>

<!-- Sweater 2 -->
<div>
  <svg width="480" height="480">
    <g stroke-width="1px" id="g2">
      <defs>
        <!-- Define the pattern -->
        <pattern id="pattern-chevron" x="0" y="0" patternUnits="userSpaceOnUse" width="24" height="24" viewBox="0 0 10 10">
          <linearGradient id="Gradient1" x1="0" x2="0" y1="0" y2="1">
            <stop offset="5%" stop-color="forestgreen" />
          </linearGradient>
          <g id="chevron">
            <rect id="rect" x="0" y="0" width="100" height="100" fill="url(#Gradient1)" />
            <path d="M0 0l5 3v5l-5 -3z" id="path1" />
            <path d="M10 0l-5 3v5l5 -3" id="path2" />
          </g>
        </pattern>
      </defs>
    </g>
  </svg>
  <div>
    <label>Sweater 2 stroke-width</label>
    <input type="range" id="stroke-width-input2" class="input-div" min="0.1" max="20" value="0" step="0.1" />
  </div>
  <button id="button2">Recreate Sweater 2</button>
</div>
              
            
!

CSS

              
                body,
div {
  display: flex;
  justify-content: center;
  align-items: center;
}
svg {
  border: 1px solid black;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px;
}
div {
  flex-direction: column;
  margin: 10px;
}

button {
  margin: 10px;
  padding: 10px;
}

@media screen and (max-width: 1000px) {
  body,
  div {
    flex-direction: column;
  }
}

              
            
!

JS

              
                const button1 = document.getElementById("button1");
const button2 = document.getElementById("button2");
const globalInSvg = document.getElementById("g");
const globalInSvg2 = document.getElementById("g2");
const strokeWidthInput = document.getElementById("stroke-width-input");
const strokeWidthInput2 = document.getElementById("stroke-width-input2");
const gradientColors = Array.from(
  document.getElementsByClassName("gradientStopColors")
);
let hue, saturation, lightness, alpha;

const getRandomNum = (max) => Math.floor(Math.random() * Math.floor(max));

const getRandomInt = (min, max) => {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

const generateRandomDecimal = () => Math.random().toFixed(1);

function addPolygonTagsToHTML() {
  let polygon = document.createElement("path");
  let polygon2 = document.createElement("path");
  polygon.setAttribute("class", "polygons");
  polygon2.setAttribute("class", "polygons2");
  for (let i = 0; i < 14; i++) {
    globalInSvg.innerHTML += polygon.outerHTML;
    globalInSvg2.innerHTML += polygon2.outerHTML;
  }
  globalInSvg.appendChild(polygon);
  globalInSvg2.appendChild(polygon2);
}
addPolygonTagsToHTML();

function addPathsInside() {
  let pathsInside = document.createElement("path");
  let pathsInside2 = document.createElement("path");
  pathsInside.setAttribute("class", "pathsInside");
  pathsInside2.setAttribute("class", "pathsInside2");
  for (let i = 0; i < 20; i++) {
    globalInSvg.innerHTML += pathsInside.outerHTML;
    globalInSvg2.innerHTML += pathsInside2.outerHTML;
  }
  globalInSvg.appendChild(pathsInside);
  globalInSvg2.appendChild(pathsInside2);
}
addPathsInside();

const polygons = Array.from(document.getElementsByClassName("polygons"));
const polygons2 = Array.from(document.getElementsByClassName("polygons2"));
const pathsInside = Array.from(document.getElementsByClassName("pathsInside"));
const pathsInside2 = Array.from(
  document.getElementsByClassName("pathsInside2")
);

const getRandomDecimal = () => {
  let arr = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8];
  return arr[Math.floor(Math.random() * arr.length)];
};

const addOutline = (el, sweaterNum) => {
  let sweaterOutline = `
    M${80},${80} 
    Q${40},${90} ${30},${200} 
    Q${30},${380} ${40},${420}  
    L${40},${460} 
    Q${50},${470} ${96},${460} 
    Q${100},${450} ${96},${430} 
    Q${100},${430} ${120},${200} 
    Q${110},${420} ${130},${430} 
    L${130},${460} 
    Q${140},${470} ${360},${460} 
    L${356},${426} 
    Q${380},${420} ${370},${200} 
    Q${380},${420} ${400},${430} 
    L${400},${460} 
    L${440},${460} 
    L${440},${430} 
    Q${450},${420} ${460},${280} 
    Q${460},${180} ${440},${100} 
    Q${430},${80} ${320},${34} 
    Q${310},${30} ${300},${20} 
    Q${240},${20} ${200},${20} 
    Q${200},${30} ${80},${80} `;
  if (sweaterNum === 1) {
    el[0].setAttribute("d", sweaterOutline);
    el[0].setAttribute("stroke", `rgb(0,0,0)`);
    el[0].setAttribute(
      "fill",
      `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)})`
    );
    return;
  }
  // 2nd sweater fill
  el[0].setAttribute("d", sweaterOutline);
  el[0].setAttribute("stroke", `rgb(0,0,0)`);
  hue = getRandomNum(360);
  saturation = getRandomNum(100);
  lightness = 30;
  alpha = getRandomDecimal();
  el[0].setAttribute("fill", `url(#pattern-chevron)`);
  return;
};

// for 2nd sweater only
const createPatternedBg = () => {
  document
    .getElementById("rect")
    .setAttribute(
      "fill",
      `hsla(${hue}, ${saturation}%, ${
        lightness + getRandomInt(20, 40)
      }%, ${alpha})`
    );
  document
    .getElementById("path1")
    .setAttribute(
      "fill",
      `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
    );
  document
    .getElementById("path2")
    .setAttribute(
      "fill",
      `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
    );
};

const addCollar = (el, sweaterNum) => {
  let collar = `
    M200,20
    Q220,40 250,40
    Q250,40 260,40
    Q280,40 300,14
    Q305,14 310,26 
    Q300,50 250,56
    Q210,56 180,30
    Q200,0 290,10
    Q305,10 310,26 
    `;
  if (sweaterNum === 1) {
    el[1].setAttribute("d", collar);
    el[1].setAttribute("stroke", `rgb(0,0,0)`);
    el[1].setAttribute(
      "fill",
      `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)})`
    );
    return;
  }
  // 2nd sweater collar
  el[1].setAttribute("d", collar);
  el[1].setAttribute("stroke", `rgb(0,0,0)`);
  el[1].setAttribute("fill", `hsla(${hue}, ${saturation}%, ${lightness}%, 1)`);
  return;
};

const addZigzagLines = (el) => {
  let x1 = 150;
  let y1 = 100;
  let design1 = `M${x1},${y1}`;
  for (let j = 0; j < 3; j++) {
    for (let i = 150; i < 320; i += 20) {
      design1 = design1 + ` L${x1 + 10},${y1 + 10} L${x1 + 20},${y1}`;
      x1 += 20;
    }
    y1 += 40;
    x1 = 150;
    design1 = design1 + ` M${x1},${y1}`;
  }
  el[0].setAttribute("d", design1);
  el[0].setAttribute("stroke-width", 4);
  el[0].setAttribute("fill", `transparent`);
  el[0].setAttribute(
    "stroke",
    `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)}`
  );
};

const addChristmasTrees = (el, sweaterNum) => {
  let x1 = 180;
  let y1 = 220;
  let design1;
  for (let i = 0; i < 3; i++) {
    design1 = `
    M${x1},${y1} 
    L${x1 + 10},${y1 + 10} 
    L${x1 + 4},${y1 + 10} 
    L${x1 + 15},${y1 + 20}
    L${x1 + 8},${y1 + 20} 
    L${x1 + 20},${y1 + 30} 
    L${x1 + 5},${y1 + 30}
    L${x1 + 10},${y1 + 40} 
    L${x1 - 10},${y1 + 40}  
    L${x1 - 5},${y1 + 30}  
    L${x1 - 20},${y1 + 30} 
    L${x1 - 8},${y1 + 20} 
    L${x1 - 15},${y1 + 20}
    L${x1 - 4},${y1 + 10} 
    L${x1 - 10},${y1 + 10} 
    z
    `;

    // first sweater
    el[i + 1].setAttribute("d", design1);
    el[i + 1].setAttribute("stroke-width", 4);
    el[i + 1].setAttribute("fill", sweaterNum === 1 ? "transparent" : "green");
    el[i + 1].setAttribute(
      "stroke",
      sweaterNum === 1
        ? `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)}`
        : "white"
    );
    x1 = 180 + (i + 1) * 60;
  }
};

const addSnowflakes = (el) => {
  let x1 = 180;
  let y1 = 320;
  let design1;
  for (let i = 0; i < 4; i++) {
    design1 = `
    M${x1 - 20},${y1} 
    L${x1 + 20},${y1} 
    M${x1},${y1 - 20} 
    L${x1},${y1 + 20} 
    M${x1 - 12},${y1 - 12} 
    L${x1 + 12},${y1 + 12} 
    M${x1 + 12},${y1 - 12} 
    L${x1 - 12},${y1 + 12} 
    z
    `;
    el[i + 4].setAttribute("d", design1);
    el[i + 4].setAttribute("stroke-width", 4);
    el[i + 4].setAttribute("fill", `transparent`);
    el[i + 4].setAttribute(
      "stroke",
      `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)}`
    );
    x1 = 180 + i * 60;
  }
};

const addRandomDesign = (el) => {
  let x1 = 140;
  let y1 = 360;
  let design1;
  for (let i = 0; i < 13; i++) {
    design1 = `
    M${x1 + getRandomInt(1, 200)},${y1 + getRandomInt(1, 20)} 
    L${x1 + getRandomInt(1, 200)},${y1 + getRandomInt(20, 40)} 
    `;
    el[i + 8].setAttribute("d", design1);
    el[i + 8].setAttribute("stroke-width", 4);
    el[i + 8].setAttribute("fill", `transparent`);
    el[i + 8].setAttribute(
      "stroke",
      `rgb(${getRandomNum(255)}, ${getRandomNum(255)}, ${getRandomNum(255)}`
    );
  }
};

const createSweater = () => {
  addOutline(polygons, 1);
  addCollar(polygons, 1);
  addZigzagLines(pathsInside);
  addChristmasTrees(pathsInside, 1);
  addSnowflakes(pathsInside);
  addRandomDesign(pathsInside);
};

const createSweater2 = () => {
  addOutline(polygons2, 2);
  createPatternedBg();
  addCollar(polygons2, 2);
  addZigzagLines(pathsInside2);
  addChristmasTrees(pathsInside2, 2);
  addSnowflakes(pathsInside2);
  addRandomDesign(pathsInside2);
};

function setStrokeWidth(event) {
  globalInSvg.setAttribute("stroke-width", event.target.value);
}

function setStrokeWidth2(event) {
  globalInSvg2.setAttribute("stroke-width", event.target.value);
}

createSweater();
createSweater2();

button1.addEventListener("click", createSweater);
button2.addEventListener("click", createSweater2);
strokeWidthInput.addEventListener("input", setStrokeWidth);
strokeWidthInput2.addEventListener("input", setStrokeWidth2);

              
            
!
999px

Console