cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              body { 
    background: #8dd;
    text-align: center;
}

canvas {
    margin: 0;
    max-height:100%;
    max-width:100%;
    border-radius: 3px;
}

canvas:active { 
    cursor: pointer;
    cursor: -webkit-grabbing;
}

#canvas-container {
    margin: 0px 0;
}
            
          
!
            
              (function() {

    // Matter aliases
    var Engine = Matter.Engine,
        World = Matter.World,
        Bodies = Matter.Bodies,
        Body = Matter.Body,
        Composite = Matter.Composite,
        Composites = Matter.Composites,
        Common = Matter.Common,
        Constraint = Matter.Constraint,
        Events = Matter.Events;

    var Demo = {};

    var _engine,
        _sceneName;
  
      Body.create = function(options) {
        var defaults = {
            id: Body.nextId(),
            angle: 0,
            position: { x: 0, y: 0 },
            force: { x: 0, y: 0 },
            torque: 0,
            positionImpulse: { x: 0, y: 0 },
            speed: 0,
            angularSpeed: 0,
            velocity: { x: 0, y: 0 },
            angularVelocity: 0,
            isStatic: false,
            isSleeping: false,
            motion: 1,
            sleepThreshold: 60,
            density: 0.001,
            restitution: 0,
            friction: 0.01,
            frictionAir: 0.3,
            path: 'L 0 0 L 40 0 L 40 40 L 0 40',
            fillStyle: options.isStatic ? 'rgba(255,255,255,1.0)' : Common.choose(['#EACDCD', '#75394D', '#402E2B', '#57734A', '#BFBF56']),
            strokeStyle:'rgba(25,25,25,0.0)',
            strokeWidth: 0,
            lineWidth: 0,
            groupId: 0,
            slop: 0.0
        };

        var body = Common.extend(defaults, options);

        Body.updateProperties(body);

        return body;
    };
    
    Demo.init = function() {
        var container = document.getElementById('canvas-container');

        // engine options - these are the defaults
        var options = {
            positionIterations: 6,
            velocityIterations: 6,
            enableSleeping: false,
            timeScale: 1
        };

        // create a Matter engine, with the element to insert the canvas into
        // NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
        _engine = Engine.create(container, options);

        // run the engine
        Engine.run(_engine);

        // default scene function name
        _sceneName = 'avalanche';
        
        // get the scene function name from hash
        if (window.location.hash.length !== 0) 
            _sceneName = window.location.hash.replace('#', '');

        // set up a scene with bodies
        Demo[_sceneName]();
    };

    if (window.addEventListener) {
        window.addEventListener('load', Demo.init);
    } else if (window.attachEvent) {
        window.attachEvent('load', Demo.init);
    }
    
    Demo.avalanche = function() {
        var _world = _engine.world;
        
       //Demo.reset();
        _engine.enableSleeping = false;
        _engine.world.gravity.y = 1.5;
      
        var renderOptions = _engine.render.options;
        renderOptions.wireframes = false;        
        renderOptions.background = 'rbg(255,255,234)';

        var offset = 5;
        World.addBody(_world, Bodies.rectangle(400, 600 + offset, 800.5 + 2 * offset, 50.5, { isStatic: true }));
        World.addBody(_world, Bodies.rectangle(800 + offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true }));
        World.addBody(_world, Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true }));
        
      
        var stack = Composites.stack(300, 10, 4, 10, 0, 0, function(x, y, column, row) {
            return Bodies.polygon(x, y, 8,25,{
                                        render: {
                                          sprite: {
                                            texture: 'http://mixsandwich.info/js/sakura.png'
                                          }
                                        }
            });
        });
        
        World.addComposite(_world, stack);
        
        World.addBody(_world, Bodies.polygon(400, 300, 5, 50, { isStatic : true}));

    };
  
})();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console