第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 javascript 根據(jù)旋轉(zhuǎn)角度在 3D 圓柱體/輪子上查找段

使用 javascript 根據(jù)旋轉(zhuǎn)角度在 3D 圓柱體/輪子上查找段

波斯汪 2023-02-24 15:18:43
我有一個(gè) 3D 輪子,我正在使用 javascriptrequestAnimationFrame()函數(shù)制作動(dòng)畫。輪子看起來像:有 4 個(gè)主要變量需要考慮:items輪子上的段數(shù)。spinSpeed旋轉(zhuǎn)速度修改器。將每幀的角度增加/減少乘以該值。spinDuration減速停止前全速旋轉(zhuǎn)動(dòng)畫的長(zhǎng)度。spinDirection輪子應(yīng)該旋轉(zhuǎn)的方向。接受up或down?,F(xiàn)在我想使用輪子停止的角度從 DOM 中獲取線段(紅線相交的地方)。輪段的弧起點(diǎn)和終點(diǎn)角度存儲(chǔ)在數(shù)據(jù)屬性中。例如:<div class="wheel__inner">    <div class="wheel_segment" ... data-start-angle="0" data-end-angle="12.85">Item 1</div>    <div class="wheel_segment" ... data-start-angle="12.85" data-end-angle="25.71">Item 2</div>    <div class="wheel_segment" ... data-start-angle="25.71" data-end-angle="38.58">Item 3</div>    ...</div>我通過在每個(gè)刻度上存儲(chǔ)修改后的角度來跟蹤當(dāng)前的車輪旋轉(zhuǎn)。例如:let wheelAngle = 0;window.requestAnimationFrame( function tick() {    if ( spinDirection === 'up' ) {        wheelAngle += speedModifier;    } else {        wheelAngle -= speedModifier;    }   window.requestAnimationFrame( tick );} );當(dāng)動(dòng)畫停止時(shí),我嘗試通過使用開始和結(jié)束角度對(duì)旋轉(zhuǎn)和過濾片段進(jìn)行歸一化來獲取片段。我將旋轉(zhuǎn)歸一化,因?yàn)樗梢栽谏戏?60°和下方移動(dòng)0°,我使用以下函數(shù)執(zhí)行此操作:function normaliseAngle( angle ) {    angle = Math.abs( angle ) % 360;    angle = 360 - angle; // Invert    return angle;}并像這樣使用 jQuery 過濾元素:const $found = $wheel.find( '.wheel__segment' ).filter( function() {    const startAngle = parseFloat( $( this ).data( 'start-angle' ) );    const endAngle = parseFloat( $( this ).data( 'end-angle' ) );    return angle >= startAngle && angle < endAngle;} );然而,盡管我盡了最大的努力,我還是無法讓它發(fā)揮作用。請(qǐng)?jiān)诖颂幉榭次业?JSFiddle:https ://jsfiddle.net/thelevicole/ps04fnxm/2/
查看完整描述

2 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

有兩個(gè)問題。

下圖顯示了狀態(tài)wheelAngle = 0

http://img1.sycdn.imooc.com//63f8651100011f4804660308.jpg

在您的代碼中,項(xiàng)目 0 具有startAngle = 0和endAngle = some positive value。這與您所看到的不符。實(shí)際上,項(xiàng)目 0 應(yīng)該以 0 為中心。所以你需要將你的區(qū)域偏移項(xiàng)目角度寬度的一半:


var rotateAngle = angle * i;        

var transform = `rotateX(${ rotateAngle }deg) translateZ(${ radius }px)`;

var startAngle = rotateAngle - angle / 2

var endAngle = rotateAngle + angle / 2;

第二個(gè)問題是您的規(guī)范化功能。您采用絕對(duì)值,因此會(huì)丟失任何方向信息。這是該功能的更好版本:


function normaliseAngle( angle ) {

    angle = -angle;

    return angle - 360 * Math.floor(angle / 360);

}


查看完整回答
反對(duì) 回復(fù) 2023-02-24
?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

主要問題是開始/結(jié)束角度。我更新了如下邏輯:


$segment.attr('data-start-angle', -startAngle + angle / 2);

$segment.attr('data-end-angle', -endAngle + angle / 2);

并且


function normaliseAngle(angle) {

    angle = angle % 360;

    if (angle > 0)

      angle = angle - 360;

    return angle;

  }

負(fù)旋轉(zhuǎn)將向您顯示從第一個(gè)元素開始的元素(而不是正旋轉(zhuǎn))。您還需要考慮偏移量,angle / 2因?yàn)?startAngle 會(huì)將您置于元素的中間。然后你應(yīng)該在邏輯上將你的角度歸一化為負(fù)值。


完整代碼


(function($) {


  // Settings

  const items = 28; // Segments on wheel

  const spinSpeed = randNumber(1, 10); // Spin speed multiplier

  const spinDuration = randNumber(2, 5); // In seconds

  const spinDirection = randNumber(0, 1) ? 'up' : 'down'; // Animate up  or down


  // Vars

  const $wheel = $('.wheel .wheel__inner');

  const diameter = $wheel.height();

  const radius = diameter / 2;

  const angle = 360 / items;

  const circumference = Math.PI * diameter;

  const height = circumference / items;


  // Trackers

  let wheelAngle = 0;

  const wheelStarted = new Date();


  // Add segments to the wheel

  for (let i = 0; i < items; i++) {

    var startAngle = angle * i;

    var endAngle = angle * (i + 1);

    var transform = `rotateX(${ startAngle }deg) translateZ(${ radius }px)`;


    var $segment = $('<div>', {

      class: 'wheel__segment',

      html: `<span>Item ${ i }</span>`

    }).css({

      'transform': transform,

      'height': height,

    });


    // Add start and end angles for this segment

    $segment.attr('data-start-angle', -startAngle + angle / 2);

    $segment.attr('data-end-angle', -endAngle + angle / 2);


    $segment.appendTo($wheel);

  }



  /**

   * Print debug info to DOM

   *

   * @param {object}

   */

  function logInfo(data) {

    const $log = $('textarea#log');

    let logString = '';


    logString += '-----' + "\n";

    for (var key in data) {

      logString += `${ key }: ${ data[ key ] }` + "\n";

    }

    logString += "\n";


    // Prepend log to last value

    logString += $log.val();


    // Update field value

    $log.val(logString);

  }


  /**

   * Get random number between min & max (inclusive)

   *

   * @param {number} min

   * @param {number} max

   * @returns {number}

   */

  function randNumber(min, max) {

    min = Math.ceil(min);

    max = Math.floor(max);

    return Math.floor(Math.random() * (max - min + 1)) + min;

  }


  /**

   * Limit angles to 0 - 360

   *

   * @param {number}

   * @returns {number}

   */

  function normaliseAngle(angle) {

    angle = angle % 360;

    if (angle > 0)

      angle = angle - 360;

    return angle;

  }


  /**

   * Get the wheel segment at a specific angle

   *

   * @param {number} angle

   * @returns {jQuery}

   */

  function segmentAtAngle(angle) {


    angle = normaliseAngle(angle);


    const $found = $wheel.find('.wheel__segment').filter(function() {

      const startAngle = parseFloat($(this).data('start-angle'));

      const endAngle = parseFloat($(this).data('end-angle'));

      return angle >= endAngle && angle < startAngle;

    });


    return $found;

  }


  /**

   * @var {integer} Unique ID of requestAnimationFrame callback

   */

  var animationId = window.requestAnimationFrame(function tick() {


    // Time passed since wheel started spinning (in seconds)

    const timePassed = (new Date() - wheelStarted) / 1000;


    // Speed modifier value (can't be zero)

    let speedModifier = parseInt(spinSpeed) || 1;


    // Decelerate animation if we're over the animation duration

    if (timePassed > spinDuration) {


      const decelTicks = (spinDuration - 1) * 60;

      const deceleration = Math.exp(Math.log(0.0001 / speedModifier) / decelTicks);

      const decelRate = (1 - ((timePassed - spinDuration) / 10)) * deceleration;


      speedModifier = speedModifier * decelRate;


      // Stop animation from going in reverse

      if (speedModifier < 0) {

        speedModifier = 0;

      }

    }


    // Print debug info

    logInfo({

      timePassed: timePassed,

      speedModifier: speedModifier,

      wheelAngle: wheelAngle,

      normalisedAngle: normaliseAngle(wheelAngle)

    });


    // Wheel not moving, animation must have finished

    if (speedModifier <= 0) {

      window.cancelAnimationFrame(animationId);


      const $stopped = segmentAtAngle(wheelAngle);

      alert($stopped.text());


      return;

    }


    // Increase wheel angle for animating upwards

    if (spinDirection === 'up') {

      wheelAngle += speedModifier;

    }


    // Decrease wheel angle for animating downwards

    else {

      wheelAngle -= speedModifier;

    }


    // CSS transform value

    const transform = `rotateX(${wheelAngle}deg) scale3d(0.875, 0.875, 0.875)`;


    $wheel.css({

      '-webkit-transform': transform,

      '-moz-transform': transform,

      '-ms-transform': transform,

      '-o-transform': transform,

      'transform': transform,

      'transform-origin': `50% calc(50% + ${height/2}px)`,

      'margin-top': `-${height}px`

    });


    // New tick

    animationId = window.requestAnimationFrame(tick);

  });


})(jQuery);

*,

*:before,

*:after {

  box-sizing: border-box;

}


.app {

  display: flex;

  flex-direction: row;

  padding: 15px;

}


textarea#log {

  width: 300px;

}


.wheel {

  perspective: 1000px;

  border: 1px solid #333;

  margin: 0 25px;

  flex-grow: 1;

}


.wheel:after {

  content: '';

  display: block;

  position: absolute;

  top: 50%;

  left: 0;

  right: 0;

  height: 2px;

  background-color: red;

  transform: translateY(-50%);

}


.wheel .wheel__inner {

  position: relative;

  width: 200px;

  height: 350px;

  margin: 0 auto;

  transform-style: preserve-3d;

}


.wheel .wheel__inner .wheel__segment {

  display: flex;

  justify-content: center;

  align-items: center;

  width: 100%;

  height: 40px;

  position: absolute;

  top: 50%;

  background-color: #ccc;

}


.wheel .wheel__inner .wheel__segment:nth-child(even) {

  background-color: #ddd;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="app">

  <textarea id="log"></textarea>

  <div class="wheel">

    <div class="wheel__inner">

    </div>

  </div>

</div>


查看完整回答
反對(duì) 回復(fù) 2023-02-24
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)