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

              
                <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>


<button onclick="click_addPath()">ADD PATH</button>

<div class="template-background" id="template-container">

</div>
              
            
!

CSS

              
                 .template-background {
        background-repeat: no-repeat;
        background-position: center;
        background-size: 100% 100%;
        width: 100%;
        height: 80vh; /* responsive height */
    }

    .svg {
        position: fixed;
        min-width: 100%;
        min-height: 100%;
    }

    .handle {
        fill: dodgerblue;
    }

    .path {
        fill: none;
        stroke: dodgerblue;
        stroke-width: 6;
    }

    .handle {
        fill: dodgerblue;
    }

    .path {
        fill: none;
        stroke: dodgerblue;
        stroke-width: 6;
    }
              
            
!

JS

              
                 function click_addPath() {
        var pathId = getRandomId(10000);
        var newPath = `
                  <svg class="svg path" >
                    <path id=path_`+ pathId + ` />
                    <circle class="handle  handle`+ pathId+`" cx="0" cy="0" r="8" />
                    <circle class="handle  handle`+ pathId +`" cx="0" cy="0" r="8" />
                    <circle class="handle  handle`+ pathId +`" cx="0" cy="0" r="8" />
                </svg>
                    `;
        pathHandler(newPath,null);
    }

 gsap.registerPlugin(Draggable);

    let bezierWeight = 0;
    function pathHandler(path, model) {
        $("#template-container").append(path);
        var html = $(path);
        var pathId = html[0].firstElementChild.id.split("_")[1];

        // Amount to offset control points


        let handles = document.querySelectorAll(".handle" + pathId);
        let pathPoint = document.querySelector("#path_" + pathId);
        if (model == null) {
            gsap.set(handles[0], { x: 400, y: 150 });
            gsap.set(handles[1], { x: 400, y: 250 });
            gsap.set(handles[2], { x: 400, y: 350 });
        } else {
            var percModel = JSON.parse(model.PercentageJson);

            var x1 = getPixelX(percModel.x1);
            var y1 = getPixelY(percModel.y1);
            var x4 = getPixelX(percModel.x4);
            var y4 = getPixelY(percModel.y4);
            var x7 = getPixelX(percModel.x7);
            var y7 = getPixelY(percModel.y7);

            gsap.set(handles[0], { x: x1, y: y1 });
            gsap.set(handles[1], { x: x4, y: y4 });
            gsap.set(handles[2], { x: x7, y: y7 });
        }

       var draggable= Draggable.create(handles, {
            bounds: window,
            dragClickables: true,
            throwProps: false,
            zIndexBoost: false,
            onDrag: updatePath,
            onClick: function () { alert("asd");}
       });

        draggable.index = getRandomId() * -1;


        let handlePropsA = gsap.getProperty(handles[0]);
        let handlePropsB = gsap.getProperty(handles[1]);
        let handlePropsC = gsap.getProperty(handles[2]);

        updatePath();

        function updatePath(e) {
            let x1 = handlePropsA("x");
            let y1 = handlePropsA("y");

            let x4 = handlePropsB("x");
            let y4 = handlePropsB("y");

            let x7 = handlePropsC("x");
            let y7 = handlePropsC("y");

            let dx = Math.abs(x4 - x1) * bezierWeight;

            let x2 = x1 - dx;
            let x3 = x4 + dx;
            let x5 = x4 + dx;
            let x6 = x7 + dx;

            let data = `M${x1} ${y1} C ${x2} ${y1} ${x3} ${y4} ${x4} ${y4}
    ${x5} ${y4} ${x6} ${y7} ${x7} ${y7} `;

            pathPoint.setAttribute("d", data);

  
        }

    }

 function getRandomId(max) {
        return -1 * Math.floor(Math.random() * max);
    }
              
            
!
999px

Console