Source: site.view [edit]
Function name: pyramid
Arguments: blockCount
Description:
Page type: webl
Render function:  
Module: sportstreak

Page source:

    var Min = fun(a, b)
       if a < b then return a
       else return b
       end
    end;

    var blockWidth = 30;
    var blockHeight = 30;
    var blockSpacing = 2;

    // Determine the number of levels we can fully build
    var currentLevel = 1;
    while ((currentLevel * (currentLevel + 1)) / 2 <= ToInt(blockCount)) do
        currentLevel = currentLevel + 1;
    end;

    // Calculate SVG size
    var svgWidth = currentLevel * (blockWidth + blockSpacing);
    var svgHeight = currentLevel * (blockHeight + blockSpacing);

    currentLevel = currentLevel - 1;

    // Start the SVG string
    var svgStr = `<svg width="` + ToString(svgWidth) + `px" height="` + ToString(svgHeight) + `px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`;

    // Define a simple pattern for basketball texture
    svgStr = svgStr + `
        <pattern id="basketballTexture" width="` + ToString(blockWidth) + `" height="` + ToString(blockHeight) + `" patternUnits="userSpaceOnUse">
            <rect width="` + ToString(blockWidth) + `" height="` + ToString(blockHeight) + `" fill="orange" />
            <!-- Add more elements here for a detailed texture -->
        </pattern>` + "\n";

    // Draw the blocks
    var remainingBlocks = ToInt(blockCount);
    var level = 1;
    while ((level <= currentLevel + 1) and remainingBlocks > 0) do
        var levelBlocks = (if level <= currentLevel then level else remainingBlocks end);
        remainingBlocks = remainingBlocks - levelBlocks;

        var i = 0;
        while (i < levelBlocks) do
            var x = (svgWidth - level * (blockWidth + blockSpacing)) / 2 + i * (blockWidth + blockSpacing);
            var y = (level - 1) * (blockHeight + blockSpacing);
            svgStr = svgStr + `<rect x="` + ToString(x) + `" y="` + ToString(y) + `" width="` + ToString(blockWidth) + `" height="` + ToString(blockHeight) + `" fill="url(#basketballTexture)" />` + "\n";
            i = i + 1
        end;
        level = level + 1
    end;

    // Close the SVG string
    svgStr = svgStr + `</svg>`;