<section id="shapes-3e">
    <div class="container">
        <svg id="shapes-3e-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">
            <defs>
                <path id="triangle" d="M28.4,5.9l19.7,34.2c1.5,2.6-0.4,5.9-3.4,5.9H5.3c-3,0-4.9-3.3-3.4-5.9L21.6,5.9C23.1,3.3,26.9,3.3,28.4,5.9z" />
                <path id="square" d="M43,48.5H7c-3,0-5.5-2.5-5.5-5.5V7C1.5,4,4,1.5,7,1.5h36c3,0,5.5,2.5,5.5,5.5v36C48.5,46,46,48.5,43,48.5z" />
                <path id="pentagon" d="M30.6,5.3l12.8,9.3c3.4,2.4,4.8,6.8,3.5,10.7l-4.9,15c-1.3,3.9-5,6.6-9.1,6.6H17.1c-4.1,0-7.8-2.7-9.1-6.6l-4.9-15C1.9,21.3,3.3,17,6.6,14.5l12.8-9.3C22.7,2.8,27.3,2.8,30.6,5.3z" />
            </defs>
        </svg>
    </div>
</section>

<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<script src="../../js/components/shapes-3e.js" type="module"></script>
<section id="shapes-3e">
    <div class="container">
        <svg id="shapes-3e-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">
            <defs>
                <path id="triangle" d="M28.4,5.9l19.7,34.2c1.5,2.6-0.4,5.9-3.4,5.9H5.3c-3,0-4.9-3.3-3.4-5.9L21.6,5.9C23.1,3.3,26.9,3.3,28.4,5.9z"/>
                <path id="square" d="M43,48.5H7c-3,0-5.5-2.5-5.5-5.5V7C1.5,4,4,1.5,7,1.5h36c3,0,5.5,2.5,5.5,5.5v36C48.5,46,46,48.5,43,48.5z"/>
                <path id="pentagon" d="M30.6,5.3l12.8,9.3c3.4,2.4,4.8,6.8,3.5,10.7l-4.9,15c-1.3,3.9-5,6.6-9.1,6.6H17.1c-4.1,0-7.8-2.7-9.1-6.6l-4.9-15C1.9,21.3,3.3,17,6.6,14.5l12.8-9.3C22.7,2.8,27.3,2.8,30.6,5.3z"/>
            </defs>
        </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/shapes-3e.js' }}" type="module"></script>
/* No context defined. */
  • Content:
    import {
      getRandomInt,
      getRandomIntInclusive,
      shuffleArray
    } from "./utils-v1.js";
    
    const colors = [
      "#511259",
      "#553159",
      "#696273",
      "#F2EDA2",
      "#8C846C",
    ]
    
    const grays = [
      "#de8fe8",
      "#cfafd3",
      "#cdcad1",
      "#fbf9e0",
      "#d9d6ce",
    ]
    
    const shapes = [
      "triangle",
      "square",
      "pentagon",
    ]
    
    function getColor(colors) {
      return colors[getRandomIntInclusive(0, colors.length - 1)];
    }
    
    function getRandomArrayElement(arr) {
      return arr[getRandomIntInclusive(0, arr.length - 1)];
    }
    
    console.log("shapes-3e active");
    
    const draw = SVG('#shapes-3e-svg');
    
    const width = draw.width();
    const height = draw.height();
    
    const bg = draw.rect(width, height).fill(getColor(grays));
    
    const sqSize = 30;
    
    let cols = Math.floor(width / sqSize);
    let rows = Math.floor(height / sqSize);
    
    let startX = (width - (cols * sqSize)) / 2;
    let startY = (height - (rows * sqSize)) / 2;
    
    for (let r = 0; r < rows; r++) {
      for (let c = 0; c < cols; c++) {
        // Create a group
    
        const group = draw.group();
    
        // Create a square inside the group, color it white
    
        let curSquare = group.rect(sqSize, sqSize);
    
        curSquare.attr({
          fill: "#fff",
        });
    
        // Create a shape inside the group, color it black
    
        let currentShape = group.use(getRandomArrayElement(shapes)).attr({
          fill: '#000',
        });
    
        let objscale = sqSize / currentShape.bbox().width * 0.85;
    
        currentShape.transform({
          position: [sqSize / 2, sqSize / 2],
          scale: objscale
        });
    
        // Move the black and white group into position, then rotate it for fun
    
        group.transform({
          position: [startX + c * sqSize + sqSize / 2, startY + r * sqSize + sqSize / 2],
          rotate: getRandomIntInclusive(0, 3) * 90,
        });
    
        // Create a new square and give it a random color
    
        let maskedSquare = draw.rect(sqSize, sqSize);
        let colorForThis = getColor(colors);
        maskedSquare.fill(colorForThis);
    
        // Mask the square with the grouo we created above
    
        maskedSquare.maskWith(group);
    
        // Move the masked square into position
    
        maskedSquare.cx(startX + c * sqSize + sqSize / 2);
        maskedSquare.cy(startY + r * sqSize + sqSize / 2);
    
        // Stroke it to make it prettier
    
        maskedSquare.stroke({
          color: colorForThis,
          width: 1,
        });
      }
    }
    
  • URL: /components/raw/shapes-3e/shapes-3e.js
  • Filesystem Path: components/03-svgjs/02-shapes/03-shapes-3e/shapes-3e.js
  • Size: 2.3 KB
  • Content:
    #shapes-3e {
      padding: 1rem;
    
      .container {
        width: 100%;
        height: calc(100vh - 2rem);
      }
    
      svg {
        border: 1px solid teal;
        width: 100%;
        height: auto;
      }
    }
    
    
  • URL: /components/raw/shapes-3e/shapes-3e.scss
  • Filesystem Path: components/03-svgjs/02-shapes/03-shapes-3e/shapes-3e.scss
  • Size: 178 Bytes

No notes defined.