Pen Settings



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


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


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.


Save Automatically?

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.






                const frame = new Frame("fit", 1024, 768, darker, dark);
frame.on("ready", ()=>{ // ES6 Arrow Function - similar to function(){}
    zog("ready from ZIM Frame"); // logs in console (F12 - choose console)

    // often need below - so consider it part of the template
    let stage = frame.stage;
    let stageW = frame.width;
    let stageH = frame.height;

    // REFERENCES for ZIM at
    // see for video and code tutorials
    // see for documentation
    // see for INTRO to ZIM
    // see for INTRO to CODE

    // CODE HERE
		// This feature uses Box2D for physics 
		// and the ZIM physics.js helper file 
		// As of ZIM 10, physics is integrated into ZIM and the docs
		// see examples here
		// This app is for the CodePen Challenge on Polygons
    // There is a Box2D polygon shape
    // but this would not let objects inside
    // and ZIM only automatically maps Triangle, Rectangle, Circle
    // You can custom map the polygon with setMap() if desired

    // SO... the plan is to make a Polygon with a Blob
    // setting the controlType to none
    // which makes straight lines from point to point.
    // Then add Rectangle objects along the lines
    // with physics added and join them with a weld joint.
    // This means we can put things inside! (or outside)

    const width = 30; // width of polygon sides
		// we will put all the parts in a Container 
		// so we can easily remove them to change polygons
    let holder = new Container(stageW, stageH).addTo();
    // The number of sides can be set from 3-8 with the tab controls
    let startNum = 5;
    let joints; // for storing joints to remove later

    // Make a physics world
    // requires Box2D and ZIM physics.js helper
    const physics = new Physics(0); // no gravity

    // create a function to make the polygon
    // because we need to change the sides
    // with the tab controls later
    function makePoly(points) {

        let poly = new Blob({
        }).transformPoints("scale", 2).center(holder);

        // find the distance between points
        let p0 = poly.pointControls[0];
        let p1 = poly.pointControls[1];
        let length = dist(p0, p1);
        // above is ZIM dist() function - or use
        // let length = Math.sqrt((p0.x-p1.x)**2 + (p0.y-p1.y)**2);
        let angle = 360 / points; // for a regular polygon

        // We will loop through the control points
        // but we need to know the last point
        // so we can make and position lines as we go
        // Controls are inside Blob so get global points
        let firstPoint = p0.localToGlobal(0, 0); // where is the 0,0 globally
        let lastPoint = firstPoint;
        let lastRect = null;
        let firstRect = null;
        joints = [];

        // need two points each time to make a rectangle between
        // so skip the first point...
        // then need to do one more after the loop
        loop(poly.pointControls, (point, i)=>{
            if (i==0) return; // like a continue in a for loop
            let thisPoint = point.localToGlobal(0, 0);
            makeRect(lastPoint, thisPoint, i);
            lastPoint = point.localToGlobal(0, 0);
        makeRect(lastPoint, firstPoint, points); // the one more

        function makeRect(lastPoint, thisPoint, num) {
            // find half way between this point and last point
            let midPoint = new Point(
                thisPoint.x+(lastPoint.x-thisPoint.x)/2, // part way between points

            // Adjust for Blob start angle
            // let rectAngle = angle*(num-3);
            // works hard coded for 5 sides
            // but to make it work for any points use this - yum!
            // this will be 2,2,3,3,4,4 for num points from 3-8
            // and then Blob always start with point0 at top
            // and we must subtract half the angle on even numbers - sigh.
            let rectAngle = angle*(num-Math.ceil(points/2))-(points%2==0?angle/2:0);

            let rect = new Rectangle(length, width, faint) // set to color to see
                .loc(midPoint) // passing in an object with an x and y works for loc
            if (!lastRect) {
                firstRect = rect;
            } else {
                joints.push(physics.join(rect, lastRect, thisPoint));
            lastRect = rect;
        joints.push(physics.join(firstRect, lastRect, firstPoint));

        // need to make poly follow welded rectangles
        // easiest way is to weld something at registration of poly
        // we don't want the circles inside to interact with this
        // so set the maskBits to another bit setting (2,4,8,16,etc.)
        // default is 1 for all physics shapes - but can set categoryBits to adjust
        let marker = new Circle(1, clear).loc(poly, null, holder).addPhysics({maskBits:2});
        joints.push(physics.join(firstRect, marker, firstPoint));

        // now make poly follow and rotate based on our marker
            // poly.loc(marker); works until we added controls to remake poly
            // then introduced a bug where when we remove poly,
						// this ticker adds it again to the stage!
            // so make sure the loc does not add polly with the add:false
            poly.loc({x:marker.x, y:marker.y, add:false}).rot(marker.rotation);

        // Add a bunch of circles inside - or whatever...
        let colors = series(pink,green,red,orange,brown,yellow);
        interval(70, obj=>{
            new Circle({min:20, max:30}, colors)
                .impulse( // push them in different directions
                    rand(3,5,null,true), // negative too
            // normally, the end count == total
            // but when you start right away, the count starts at 0
            // so the end count == total-1
            if (obj.count == {
        }, 12, true); // run 12 times and start right away

    } // end make physics

    const controls = new Tabs({
        // make poly hit interface! false is not dynamic (static)
						// stop the drag while we add circles
						// remove old joints and physics mappings
            loop(joints, joint=>{
            loop(holder, child=>{
                if (child.physics) { // actual Blob does not get physics
    controls.selectedIndex = startNum-3; // starting tabs at 3 poly lines

    new Label({text:"ZIM - Physics Polygon with Drag", color:white}).alp(.7).pos(30,30).bot();

    // FOOTER
    // call remote script to make ZIM Foundation for Creative Coding icon

}); // end of ready