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

              
                <div class="info">
        <p class="info-item info-item-1">1の移動距離:<span id="js-distance-1">0</span></p>
        <p class="info-item info-item-2">2の移動距離:<span id="js-distance-2">0</span></p>
        <p class="info-item info-item-3">3の移動距離:<span id="js-distance-3">0</span></p>
      </div>
      <!-- /.info -->
      <div id="js-result">
          <p id="js-result-text"></p>
          <p id="js-result-img"><img src="./images/clear.png"></p>
      </div>
      <div class="container">
       <div class="draggable-wrap">
          <div class="draggable draggable-1 js-draggable" data-dragid="1"></div>
          <!-- /.draggable -->
          <div class="draggable draggable-2 js-draggable" data-dragid="2"></div>
          <!-- /.draggable -->
          <div class="draggable draggable-3 js-draggable" data-dragid="3"></div>
          <!-- /.draggable -->
       </div>

        <div id="outer-dropzone" class="dropzone">
            #outer-dropzone
            <div id="inner-dropzone" class="dropzone-1">ドロップエリア</div>
            <div id="inner-dropzone" class="dropzone-2">ドロップエリア</div>
            <div id="inner-dropzone" class="dropzone-3">ドロップエリア</div>
        </div>
      </div>
      <!-- /.container -->
      <script src="https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js"></script>
              
            
!

CSS

              
                *{
  box-sizing: border-box;
}

.container{
    /* overflow: hidden; */
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex-direction: column;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    /* -webkit-justify-content: center; */
    -ms-flex-pack: center;
    /* justify-content: center; */
    width: 100%;
    min-height: 100vh;
    padding: 10px;
    margin: auto;
    max-width: 800px;
  }
  
  .draggable {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex-direction: column;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-flex-shrink: 0;
    -ms-flex-negative: 0;
    flex-shrink: 0;
    width: 100px;
    height: 100px;
    font-family: "Helvetica Neue", Helvetica, sans-serif;
    font-size: 39px;
    font-weight: 600;
    color: #fff;
    border-radius: 50%;
    touch-action: none;
    user-select: none;
    z-index: 1;
    border: 1px solid #555555;
  }

  .draggable.active{
    filter: brightness(1.5);
  }

  .draggable-wrap{
    display: flex;
    flex-direction: row;
    z-index: 1;
  }

  .draggable img{
    width: 100%;
    height: 100%;
  }
  
  .draggable-1{
    background: #81cf7a;
  }
  
  .draggable-2{
    background: #25bdcf;
    /* margin-left: 60px; */
  }
  .draggable-3{
    background: #f9c851;
    /* margin-left: 60px; */
  }
  
  .info{
    /* position: fixed; */
    top: 0;
    z-index: 100;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    width: 100%;
    background: #fff;
    user-select: none;
  }
  
  .info-item{
    width: 100%;
    padding: 5px;
    text-align: center;
    font-size: 12px;
    margin: 0;
  }
  
  .info-item p{
    line-height: 1;
  }
  
  .info-item-1{
    color: #ffffff;
    font-weight: bold;
    border: 3px solid #3e7277;
    background-color: #3e7277;
  }
  
  .info-item-2{
    color: #ffffff;
    font-weight: bold;
    border: 3px solid #904d51;
    background-color: #904d51;
  }
  .info-item-3{
    color: #ffffff;
    font-weight: bold;
    border: 3px solid #98984e;
    background-color: #98984e;
  }

  #outer-dropzone {
    height: 400px;
    position: relative;
    width: 100%;
  }
  
  /* #inner-dropzone {
    height: 300px;
  } */
  
  .dropzone {
    /* background-color: #bfe4ff; */
    border: dashed 4px transparent;
    border-radius: 4px;
    margin: 10px auto 30px;
    padding: 10px;
    width: 80%;
    transition: background-color 0.3s;
  }
  .dropzone-1{
    height: 150px;
    width: 150px;
    background-color: #81cf7a;
    position: absolute;
    top: 30px;
    right: 0;
  }

  .dropzone-2{
    height: 150px;
    width: 150px;
    background-color: #25bdcf;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 0;
  }
  .dropzone-3{
    position: absolute;
    left: 0;
    right: 0;
    margin: auto;
    bottom: 0;
    height: 150px;
    width: 150px;
    background-color: #f9c851;
  }

  #js-result{
    text-align: center;
    font-weight: bold;
  }
  #js-result-img{
    display: none;
    opacity: 0;
    width: 70px;
    height: 70px;
    margin: auto;
    animation: fadeIn .5s .5s forwards;
    transform: scale(0);
  }
  #js-result-img img{
    width: 100%;
    height: 100%;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: scale(0);
    }
    to {
      opacity: 1;
      transform: scale(1.1);
    }
  }
              
            
!

JS

              
                // フラグ
let flagList = [
  {id: 1, flag: false},
  {id: 2, flag: false},
  {id: 3, flag: false}
]

// import interact from 'interactjs'
class Interact {
  constructor(){
    this.applyInteractJs();
  }

  //interact.jsを適用
  applyInteractJs(){
    this.createPositionStorage();
    this.addDraggingEvent();
  }

  //位置情報を保存する変数を作成
  createPositionStorage(){
    this.target = document.querySelectorAll('.js-draggable');
    if(!this.target){
      return;
    }
    const arr = [...this.target];
    this.editStyle = {};
    arr.forEach((el) => {
    //   this.editStyle[`target-${el.textContent}`] = {
      this.editStyle[`target-${el.dataset.dragid}`] = {
        x     : 0,
        y     : 0,
        scale : 1
      };
    });
  }

  //ドラッグイベントを適用
  addDraggingEvent(){
    if(!this.target){
      return;
    }

    interact('.js-draggable')
    //マルチタッチジェスチャーイベント
    .gesturable({
      onmove: (e) => {
        //2本指での拡大縮小に対応
        this.scaleMoveListener(e);
      }
    })
    //ドラッグ&ドロップイベント
    .draggable({
      // 慣性を付ける
      inertia     : true,
      // 親要素の領域内に要素を保持します
      modifiers   : [
      interact.modifiers.restrictRect({
        //ドラッグの範囲を親要素に設定
        // restriction : 'parent',
        //ドラッグの最後の移動値を適用させる。慣性と併用することで動きがスムーズになる
        endOnly     : true
      })
      ],
      // コンテナの端でドラッグまたはサイズ変更の移動が発生した場合、コンテナ(ウィンドウまたはHTMLElement)をスクロールします。
      autoScroll  : true,

      // dragmoveイベント
      onmove      : (e) => {
        this.dragMoveListener(e);
      },
      // dragend イベント
      onend       : (e) => {
        this.dragEndListner(e);
      }
    });
  }

  /*
  * dragmoveイベントを取得
  * @param {Object} event
  */
  dragMoveListener(event){
    event.stopPropagation();
    const target    = event.target;
    // const target_id = target.textContent;
    const target_id = target.dataset.dragid;
    const editStyle  = this.editStyle[`target-${target_id}`];
    // console.log(target_id);
    // console.log(flag);


    //位置情報を更新する
    editStyle.x = (parseFloat(editStyle.x) || 0) + event.dx;
    editStyle.y = (parseFloat(editStyle.y) || 0) + event.dy;

    // css transform で要素を動かす
    this.addEditedStyle(target, editStyle);

    //情報を表示させる
    this.showPositionInfomation(target_id, event);
  }

  /*
  * 操作した図形のスタイルを追加する
  * @param {Object} editStyle 図形の編集情報
  */
  addEditedStyle(target, editStyle){
     // css transform で要素を動かす
     target.style.webkitTransform =
     target.style.transform =
     `translate(${editStyle.x}px, ${editStyle.y}px) scale(${editStyle.scale}`;
   }

  /*
  * dragendイベントを取得
  * @param {Object} event
  */
  dragEndListner(event){
    const target_id = event.target.textContent;
    this.showPositionInfomation(target_id, event);
  }

  /*
  * 位置情報を表示させる
  * @param {String} target_id
  * @param {Object} event
  */
  showPositionInfomation(target_id, event){
    //移動距離
    const target = document.getElementById(`js-distance-${target_id}`);
    if(!target){
      return;
    }
    const moveValue = (Math.sqrt(Math.pow(event.pageX - event.x0, 2) + Math.pow(event.pageY - event.y0, 2) | 0)).toFixed(2) + 'px';
    target.textContent = moveValue;

    //拡大縮小値
    const targetScale = document.getElementById(`js-scale-${target_id}`);
    if(!targetScale){
      return;
    }
    targetScale.textContent = event.scale ? event.scale.toFixed(2) : 1;

  }

}
new Interact();



// 当たり判定
const array = [1,2,3];
array.forEach(index => {
interact(`.dropzone-${index}`).dropzone({
    // 受け入れる要素を指定
    accept: `.draggable-wrap .draggable-${index}`,
    // 判定する要素の重なり度を指定
    overlap: 0.75,
  
    // listen for drop related events:
    // オブジェクトがアクティブになったときに実行される
    ondropactivate: function (event) {
      // add active dropzone feedback
      event.target.classList.add('drop-active')
    },
    // オブジェクトがエリア内に入ったときに実行される
    ondragenter: function (event) {
      var draggableElement = event.relatedTarget
      var dropzoneElement = event.target
  
      // feedback the possibility of a drop
      dropzoneElement.classList.add('drop-target')
      draggableElement.classList.add('can-drop')
    //   draggableElement.textContent = 'Dragged in'
    },
    // オブジェクトがエリア内から離れたときに実行される
    ondragleave: function (event) {
      // event.relatedTarget.textContent = 'Dragged out'
      // remove the drop feedback style
      event.target.classList.remove('drop-target')
      event.relatedTarget.classList.remove('can-drop')
      event.relatedTarget.classList.remove('active')
      // flag[index] = false;
    },
    // オブジェクトがエリア内に入り、ドロップされたときに実行される
    ondrop: function (event) {
      event.relatedTarget.classList.add('active')
      // event.relatedTarget.textContent = 'Dropped'
      flagList[index - 1].flag = true;
      // console.log(flagList[index - 1]);
      // console.log(flag[index]);
      flagCheck();
    },
    ondropdeactivate: function (event) {
      // remove active dropzone feedback
      event.target.classList.remove('drop-active')
      event.target.classList.remove('drop-target')
    }
  })
});

// 全てのflagがtrueになっているかをチェックする
function flagCheck() {   
    let result = flagList.every(function(element){
        return element.flag == true;
    });
    // 全てのフラグがTrueの場合
    if (result) {
      let resultText = document.getElementById('js-result-text');
      
        resultText.textContent = '3つの要素が正しくセットされました!';
        // console.log("OK");
    }else{
        // console.log("NG");
    }
}
              
            
!
999px

Console