<section id="blobs-2">
    <div class="container">
        <svg id="blobs-2-svg" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="400" height="300" viewBox="0 0 400 300">
        </svg>
    </div>
</section>

<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<script src="../../js/components/blobs-2.js" type="module"></script>
<section id="blobs-2">
    <div class="container">
        <svg id="blobs-2-svg" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="400" height="300" viewBox="0 0 400 300">
        </svg>
    </div>
</section>

<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<script src="{{ path '/js/components/blobs-2.js' }}" type="module"></script>
/* No context defined. */
  • Content:
    import {
      getRandomIntInclusive,
    } from "./utils-v1.js";
    
    const colors = [
      "#511259",
      "#553159",
      "#696273",
      "#F2EDA2",
      "#8C846C",
    ]
    
    function getColor(colors) {
      return colors[getRandomIntInclusive(0, colors.length - 1)];
    }
    
    function getRandomArrayElement(arr) {
      return arr[getRandomIntInclusive(0, arr.length - 1)];
    }
    
    console.log("blobs-2 active");
    
    const draw = SVG('#blobs-2-svg');
    
    const width = draw.width();
    const height = draw.height();
    
    const sqSize = 30;
    
    // Blob code
    
    const blobs = [];
    const numBlobs = 7;
    const defs = draw.defs();
    
    for (let i = 0; i < numBlobs; i++) {
      // Create a group
    
      const group = defs.group();
    
      // Create a square inside the group
    
      let curSquare = group.rect(sqSize, sqSize);
    
      // pick a point somewhere inside the circle
    
      let curX = getRandomIntInclusive(2, sqSize - 3);
      let curY = getRandomIntInclusive(2, sqSize - 3);
    
      let d = Math.min(curX, curY, sqSize - curX, sqSize - curY) * 2;
    
      // draw a circle there
    
      let circle = group.circle(d)
        .attr({
          cx: curX,
          cy: curY,
        });
    
      blobs.push(group);
    }
    
    // Grid code
    
    let cols = Math.floor(width / sqSize);
    let rows = Math.floor(height / sqSize);
    
    let startX = (width - (cols * sqSize)) / 2;
    let startY = (height - (rows * sqSize)) / 2;
    
    console.log({startX, startY});
    
    for (let r = 0; r < rows; r++) {
      for (let c = 0; c < cols; c++) {
        let currentShape = getRandomArrayElement(blobs).clone();
    
        let objscale = sqSize / currentShape.bbox().width * 0.979;
    
        currentShape.transform({
          position: [startX + c * sqSize + sqSize / 2, startY + r * sqSize + sqSize / 2],
          scale: objscale,
          rotate: getRandomIntInclusive(0, 3) * 90
        });
    
        let circles = currentShape.find('circle');
        console.dir(circles);
        circles.fill('#fff');
    
        let squares = currentShape.find('rect');
        console.dir(squares);
        squares.fill(getColor(colors));
    
        draw.add(currentShape);
      }
    }
    
  • URL: /components/raw/blobs-2/blobs-2.js
  • Filesystem Path: components/03-svgjs/03-blobs/02-blobs-2/blobs-2.js
  • Size: 1.9 KB
  • Content:
    #blobs-2 {
      padding: 1rem;
    
      .container {
        width: 100%;
        height: calc(100vh - 2rem);
      }
    
      svg {
        border: 1px solid teal;
        width: 100%;
        height: auto;
      }
    }
    
  • URL: /components/raw/blobs-2/blobs-2.scss
  • Filesystem Path: components/03-svgjs/03-blobs/02-blobs-2/blobs-2.scss
  • Size: 175 Bytes

No notes defined.