Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="info">
  Из статьи: <a target="_blank" href="http://frontender.info/building-a-2d-browser-game-with-physicsjs/">Создание двухмерной браузерной игры на основе PhysicsJS</a> опубликованной во Frontender Magazine. (<a href="https://codepen.io/SilentImp/pen/dDryl">Законченный пример модно посмотреть тут</a>).
              html, body {
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
body:after {
    font-size: 30px;
    line-height: 140px;
    text-align: center;
    color: rgb(60, 16, 11);
    background: rgba(255, 255, 255, 0.6);
    position: absolute;
    top: 50%;
    left: 50%;
    width: 400px;
    height: 140px;
    margin-left: -200px;
    margin-top: -70px;
    border-radius: 5px;
body.before-game:after {
    content: 'Нажмите Z, чтобы начать игру';
body.lose-game:after {
    content: 'Нажмите Z, чтобы попробовать ещё раз';
body.win-game:after {
    content: 'Победа! нажмите Z, чтобы сыграть ещё раз';
/* космический корабль взят отсюда: http://millionthvector.blogspot.ca/p/free-sprites.html */
/* звездное небо взято отсюда: https://gamedevtuts.s3.amazonaws.com/043_freeShmupSprites/Free_Shmup_Sprites_Starfield.png */
body {
  background: url(//frontender.info/building-a-2d-browser-game-with-physicsjs/img/starfield.png) 0 0 repeat;

.info {
    display: none;
    position: absolute;
    z-index: 2;
    top: 0;
    left: 0;
    font-size: 20px;
    color: #ffd;
    padding: 4px;
.info a {
    color: #ffd;
body.before-game .info {
    display: block;
              // начало main.js
    baseUrl: '//frontender.info/building-a-2d-browser-game-with-physicsjs/examples/spaceship/',
    packages: [{
      name: 'physicsjs',
      location: '//frontender.info/building-a-2d-browser-game-with-physicsjs/js',
      main: 'physicsjs-0.5.2.min'
  // основные модули
], function(
  document.body.className = 'before-game';
    var par = parent;
    try {
      par && par.innerWidth;
    } catch( e ){
      par = window;
  var inGame = false;
  document.addEventListener('keydown', function( e ){
    if (!inGame && e.keyCode === 90){
      document.body.className = 'in-game';
      inGame = true;
  var renderer = Physics.renderer('canvas', {
    el: 'viewport',
    width: par.innerWidth,
    height: par.innerHeight,
    // meta: true,
    // debug:true,
    styles: {
      'circle': {
        strokeStyle: 'rgb(0, 30, 0)',
        lineWidth: 1,
        fillStyle: 'rgb(100, 200, 50)',
        angleIndicator: false
      'convex-polygon' : {
        strokeStyle: 'rgb(60, 0, 0)',
        lineWidth: 1,
        fillStyle: 'rgb(60, 16, 11)',
        angleIndicator: false
  window.addEventListener('resize', function(){
    renderer.el.width = par.innerWidth;
    renderer.el.height = par.innerHeight;
  var init = function init( world, Physics ){
    var ship = Physics.body('circle', {
      x: 400,
      y: 100,
      vx: 0.08,
      radius: 30
    // тела
    var planet = Physics.body('circle', {
      // fixed: true,
      // hidden: true,
      mass: 10000,
      radius: 140,
      x: 400,
      y: 300
    planet.view = new Image();
    planet.view.src = require.toUrl('images/planet.png');
    // отрисовка на каждом шаге
    world.subscribe('step', function(){
    // добавляем объекты в созданный нами мир
      Physics.behavior('newtonian', { strength: 1e-4 }),
  var world = null;
  var newGame = function newGame(){
    if (world){
    world = Physics( init );
    world.subscribe('lose-game', function(){
      document.body.className = 'lose-game';
      inGame = false;
    world.subscribe('win-game', function(){
      document.body.className = 'win-game';
      inGame = false;
  // Подписываемся на ticker и начинаем итерировать
  Physics.util.ticker.subscribe(function( time ){
    if (world){
      world.step( time ); 
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.