css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <body>
    <div class="mv">
    </div>

    <p class="text">DRAG</p>
</body>
            
          
!
            
                    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        font-family: 游ゴシック体, 'Yu Gothic', YuGothic, 'ヒラギノ角ゴシック Pro', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, Osaka, 'MS Pゴシック', 'MS PGothic', sans-serif;
      }
      body, html {
        width: 100%;
        height: 100%;
        background-color: #f1cdd0;
      }
      .mv {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        -webkit-user-select: none;
      }
      .item {
        position: absolute;
        top: 0;
        left: 0;
        width: 10%;
        height: 3%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

      }
      .line {
        position: relative;
        z-index: 1;
        width: 1px;
        height: 80%;
        background-color: #262558;
      }
      .dot {
        z-index: 0;
        position: relative;
        width: 5%;
        border-radius: 50%;
        background-color: #262558;
      }
      .dot:before {
        content: '';
        display: block;
        padding-top: 100%;
      }
      .dot > .inner {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
      }
      .text {
        position: absolute;
        left: 0;
        bottom: 1.5vw;
        font-size: 1.5vw;
        font-weight: bold;
        font-style: italic;
        text-align: center;
        width: 100%;
      }
      @media screen and (max-width:750px) {
        .text {
          font-size: 5vw;
        }
      }
            
          
!
            
              // -----------------------------------------
//
// -----------------------------------------
Item = function(el) {

  // 操作する要素
  this.el = el;

  // アニメーションさせる要素
  this.line = this.el.find('.line');
  this.dot = this.el.find('.dot');

  // パラメータ
  this.paramLine = {
    y:0
  }
  this.paramDot = {
    y:0
  }
  this.fric = random(0.2, 1)

};

// 初期化
Item.prototype.init = function() {
};

// 更新
Item.prototype.update = function() {

  var rot = Math.atan2(param.y, param.x)
  TweenMax.set(this.line, {
    scaleY:0 + Math.abs(param.y) * 0.05,
    y:param.y,
    rotationZ:radian(param.y * param.x) * -1
  });

  TweenMax.set(this.dot, {
    x:param.y * this.fric * -1,
    y:param.x * this.fric * -1,
    scale:1 + Math.abs(param.x * param.y) * this.fric * 0.001
  });

  TweenMax.set(this.el, {
    x:param.x * this.fric,
    y:param.y * this.fric
  });

};
// -----------------------------------------


// 動かすやつ
items = [];

// マウス
mouse = {
  isDown:false,     // 画面押されてるか
  x:0,              // マウス位置 X
  y:0,              // マウス位置 Y
  start:{x:0, y:0}, // マウスダウン時の座標
  dist:{x:0, y:0}   // マウスダウンしてからの移動距離
};

// アニメーション用パラメータ
param = {
  x:0,
  y:0,
  d:0
};

// 初期設定
init();
function init() {

  var itemNum = 50;
  for(var i = 0; i< itemNum; i++) {
    $('.mv').append('<div class="item js-tg"><div class="line"></div><div class="dot"><div class="inner"></div></div></div>');
  }

  $('.js-tg').each(function(i,e) {
    var item = new Item($(e));
    item.init();
    items.push(item);
  });

  window.addEventListener('resize', resize);

  if(isMobile.any) {
    document.addEventListener('touchmove', _eMouseMove, {passive:false});
    document.addEventListener('touchstart', _eMouseDown, {passive:false});
    document.addEventListener('touchend', _eMouseUp, {passive:false});
  } else {
    $(window).on('mousemove', _eMouseMove).on('mousedown', _eMouseDown).on('mouseup', _eMouseUp);
  }

  update();
  resize();
}

// 毎フレーム実行
window.requestAnimationFrame(update);
function update() {

  if(mouse.isDown) {
    // マウス押してるときは、マウスダウン時からのマウス移動量をちゃんと計算
    dx = mouse.x - mouse.start.x;
    dy = mouse.y - mouse.start.y;
    mouse.dist.x = dx;
    mouse.dist.y = dy;

    // ##########################################################
    var friction = 0.2; // 摩擦係数 小さいとより引っ張られる感じに
    mouse.dist.x *= friction;
    mouse.dist.y *= friction;
    // ##########################################################

  } else {
    // マウス押してないときの移動量は0
    mouse.dist.x = 0;
    mouse.dist.y = 0;
  }

  // 滑らかに移動させるためイージングつける
  var ease = 0.075; // イージング量 小さいとゆっくり
  param.x += (mouse.dist.x - param.x) * ease;
  param.y += (mouse.dist.y - param.y) * ease;

  // 中心からの距離
  var d = Math.sqrt(param.x * param.x + param.y * param.y);
  param.d += (d - param.d) * ease;

  var len = items.length;
  for(var i = 0; i < len; i++) {
    items[i].update();
  }

  window.requestAnimationFrame(update);
}

// リサイズ
function resize() {

  // 位置ランダムに
  var rangeX = window.innerWidth * 0.5;
  var rangeY = window.innerHeight * 0.5;
  var len = items.length;
  for(var i = 0; i < len; i++) {
    items[i].el.css({
      top:window.innerHeight * 0.5 + random(-rangeY, rangeY),
      left:window.innerWidth * 0.5 + random(-rangeX, rangeX)
    })
  }

}


// ----------------------------------------
// イベント マウス動いた
// ----------------------------------------
function _eMouseMove(e) {

  if(isMobile.any) {
    event.preventDefault();
    touches = event.touches;
    if(touches != null && touches.length > 0) {
      mouse.x = touches[0].pageX;
      mouse.y = touches[0].pageY;
    }
  } else {
    mouse.x = e.clientX;
    mouse.y = e.clientY;
  }

}


// ----------------------------------------
// イベント マウス押した
// ----------------------------------------
function _eMouseDown(e) {

  if(!mouse.isDown) {
    mouse.isDown = true;
    if(isMobile.any) {
      event.preventDefault();
      touches = event.touches;
      if(touches != null && touches.length > 0) {
        mouse.start.x = mouse.x = touches[0].pageX;
        mouse.start.y = mouse.y = touches[0].pageY;
      }
    } else {
      mouse.start.x = e.clientX;
      mouse.start.y = e.clientY;
    }
  }

}


// ----------------------------------------
// イベント マウス離した
// ----------------------------------------
function _eMouseUp(e) {

  mouse.isDown = false;

}




// ########################################
// ユーティリティ系 ↓
// ########################################

// ----------------------------------------
// 度からラジアンに変換
// @val : 度
// ----------------------------------------
function radian(val) {
  return val * Math.PI / 180;
}

// ----------------------------------------
// ラジアンから度に変換
// @val : ラジアン
// ----------------------------------------
function degree(val) {
  return val * 180 / Math.PI;
}

// ----------------------------------------
// minからmaxまでランダム
// ----------------------------------------
function random(min, max) {
  return Math.random() * (max - min) + min;
}

// ----------------------------------------
// 範囲変換
// @val     : 変換したい値
// @toMin   : 変換後の最小値
// @toMax   : 変換後の最大値
// @fromMin : 変換前の最小値
// @fromMax : 変換前の最大値
// ----------------------------------------
function map(val, toMin, toMax, fromMin, fromMax) {
  if(val <= fromMin) {
    return toMin;
  }
  if(val >= fromMax) {
    return toMax;
  }
  p = (toMax - toMin) / (fromMax - fromMin);
  return ((val - fromMin) * p) + toMin;
}

            
          
!
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.
Loading ..................

Console