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

              
                <html>
<head>
<meta charset="utf-8">
<title>matter.js demo</title>
<script src="http://www.otwo.jp/blog/demo/matter/matter.js"></script>
</head>
<body>
  <div id="matter"></div>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                    // 変数宣言
    const engWidth = 680; // 表示領域(幅)
    const engHeight = 400; // 表示領域(高)
    const wall = 10;  // 表示領域周辺を囲む壁の厚み
    const ball = 30;  // 表示するボールの数
    const density = 0.1;  // 質量
    const frictionAir = 0.1;  // 空気抵抗
    const restitution = 0.5;  // 弾力性
    var ballSize = 20;  // ボールのサイズ
    var gravityx = 0.5; // 重力方向x
    var gravityy = 0.5; // 重力方向y
    var objs = [];      // 作成オブジェクト管理用

    // Engine生成(表示領域)
    var engine = Matter.Engine.create(document.getElementById("matter"), {
      render: {
        options: {
          wireframes: false, // ワイヤー表示
          width: engWidth, // canvasの横幅
          height: engHeight, // canvasの高さ
          background: "rgba(0, 0, 0, 1)"
        }
      }
    });
    engine.world.gravity.y = gravityy;
    engine.world.gravity.x = gravityx;

    // 四方に壁を作成(10pxで生成)
    // Matter.Bodies.rectangle(x, y, width, height, options)
    // 補足:x,yともに表示オブジェクトの中心位置を指定
    objs.push(Matter.Bodies.rectangle(engWidth/2, wall/2, engWidth, wall, {isStatic: true}));              // 上
    objs.push(Matter.Bodies.rectangle(engWidth-wall/2, engHeight/2, wall, engHeight, {isStatic: true}));   // 右
    objs.push(Matter.Bodies.rectangle(engWidth/2, engHeight-wall/2, engWidth, wall, {isStatic: true}));    // 下
    objs.push(Matter.Bodies.rectangle(wall/2, engHeight/2, wall, engHeight, {isStatic: true}));            // 左

    // ボールを作成
    for (var i = 0; i < ball; i++) {
      var x = Math.random()*engWidth;  // ボールの初期表示位置:xをランダムで生成
      var y = Math.random()*engHeight;
      objs.push(Matter.Bodies.circle(x, y, ballSize, {
        density: density, // 質量
        frictionAir: Math.random()*frictionAir, // 空気抵抗
        restitution: Math.random()+restitution, // 弾力性
      }));
    }

    // 作成オブジェクトをワールドに追加
    Matter.World.add(engine.world, objs);

    // ドラッグ処理
    var MouseConstraint = Matter.MouseConstraint;
    var mousedrag = MouseConstraint.create(engine, {
      element: document.getElementById("matter").childNodes[0],
    });
    Matter.World.add(engine.world, mousedrag);

    // 物理演算実行
    Matter.Engine.run(engine);

    // 重力方向の更新
    function update(){
      if(Math.random() > 0.5){
        // 重力方向を反転させる
        gravityy = gravityy * -1;
      }
      if(Math.random() > 0.5){
        // 重力方向を反転させる
        gravityx = gravityx * -1;
      }
      engine.world.gravity.y = gravityy;
      engine.world.gravity.x = gravityx;
    }

    // 2秒毎に重力方向の切り替え判定
    setInterval(update , 2000);
              
            
!
999px

Console