123

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 id="example"></div>
<div id="js-modal-bg" class="modal-bg dn">
  <div class="modal">
    <img src="https://82mou.github.io/src/images/moukon.png" alt="">
  <p class="modal__text">毛が抜けました</p>
    <span id="js-btn" class="btn">もう一回抜く</span>
  </div>
</div>
            
          
!
            
              .modal-bg {
  position: fixed;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1;
}
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  padding: 20px;
  background: rgba(255, 255, 255, 1);
  border-radius: 6px;
  z-index: 1;
  text-align: center;
  img {
    width: 40px;
  }
}
.modal__text {
  margin-top: 20px;
}
.btn {
    display: inline-block;
    margin-top: 10px;
    padding: 0.3em 1em;
    text-decoration: none;
    color: #67c5ff;
    border: solid 2px #67c5ff;
    border-radius: 3px;
    transition: .4s;
}
.btn:hover {
    background: #67c5ff;
    color: white;
}
.dn {
  display: none;
}
            
          
!
            
              // グローバルに展開
phina.globalize();
// アセット
var ASSETS = {
  // 画像
  image: {
    'moukon': 'https://82mou.github.io/src/images/moukon.png',
  },
  sound: {
    'sound': 'https://82mou.github.io/src/sound/cork-plug1.mp3',
  },
};

/*
 * メインシーン
 */
phina.define("MainScene", {
  // 継承
  superClass: 'DisplayScene',
  // コンストラクタ
  init: function(option) {
    _this = this; // thisを保存
    this.pullFlg = false; // 抜いたかどうかのフラグ
    
    // 親クラス初期化
    this.superInit(option);
    
    // CircleShapeを作成
    let circle = CircleShape({
      radius: 1000,
      fill: "#FFD8BD",
      stroke: 0
    }).addChildTo(this).setPosition(this.gridX.center(),this.gridY.center(15));
   
    // スプライト画像を作成
    this.sprite = Sprite('moukon').addChildTo(this);
    
    // スプライト画像の初期位置設定
    this.sprite.x = this.gridX.center();
    this.sprite.y = this.gridY.center(-1.5);
    this.sprite.width = 80;
    this.sprite.height = 204;
    this.sprite.backgroundColor = 'red';
    
    // スプライト画像の初期設定を保存
    this.firstInitialPositionX = this.sprite.x;
    this.firstInitialPositionY = this.sprite.y;
    this.firstHeight = this.sprite.height;
    
    // スプライト画像の原点を左上に設定
    this.sprite.origin.set(0.5, 0);
    
    // マスクを作成
    let mask = RectangleShape({
        fill: '#FFD8BD',
        stroke: 0,
        width: 120,
        height: 300
    }).addChildTo(this).setPosition(this.gridX.center(),this.gridY.center(2));
    
    // スプライト画像をドラッグ可能にする
    this.sprite.draggable;

    // ドラッグ開始時
    this.sprite.ondragstart = () => {
      
      // ドラッグで中心より左右にずれたら中心に戻す
      this.sprite.x = this.gridX.center();
      
      // ドラッグで中心より下にずれたら中心に戻す
      if(this.sprite.y > this.gridY.center()) {
        this.sprite.y = this.gridY.center();
      }
    };
    // ドラッグ中
    this.sprite.ondrag = () => {
      
      // ドラッグで中心より左右にずれたら中心に戻す
      this.sprite.x = this.gridX.center();
      
      // ドラッグで中心より下にずれたら中心に戻す
      if(this.sprite.y > this.gridY.center()) {
        this.sprite.y = this.gridY.center();
      }
      
      // spriteの移動量を保存
      this.afterPositionX = this.firstInitialPositionX - this.sprite.x;
      this.afterPositionY = this.firstInitialPositionY - this.sprite.y;
      
      // spriteの移動量 + 補正値でドラッグ後の高さを保存
      this.afterHeight = this.firstHeight + (this.afterPositionY + 100);
      
      // spriteの移動後の高さが元の高さより小くならないように設定
      if(this.afterHeight < this.firstHeight) {
        this.sprite.height = this.firstHeight;
      } else {
        this.sprite.height = this.afterHeight;
      }
      
      // 一定数毛を上にドラッグしたら発火
      if(!this.pullFlg && this.sprite.y < 150) {
        this.pullFlg = true;
        
        // spriteのheightを1.5秒かけて元に戻す
        this.sprite.tweener.to({height: _this.firstHeight}, 1500, 'easeOutElastic') 
        
        // 音再生
        SoundManager.play('sound');
        setTimeout(() => {
          document.querySelector('#js-modal-bg').classList.remove('dn');
        },700);
      }
    };
    // ドラッグ終了時
    this.sprite.ondragend = () => {
      
      // ドラッグで中心より左右にずれたら中心に戻す
      this.sprite.x = this.gridX.center();
      
      // ドラッグで中心より下にずれたら中心に戻す
      if(this.sprite.y > this.gridY.center()) {
         this.sprite.y = this.gridY.center();
      }
    };
    
    // codepenをリセット
    document.querySelector('#js-btn').addEventListener('click', () => {
      history.go(0);
    });
  }
});

/*
 * メイン処理
 */
phina.main(function() {
  // アプリケーションを生成
  var app = GameApp({
    // MainScene から開始
    startLabel: 'main',
    width: 2000,
    height: 1000,
    // アセット読み込み
    assets: ASSETS,
  })
  // 実行
  app.run();
});
            
          
!
999px
🕑 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.

Console