【问题标题】:JavaScript - Rotate Image to Angle Between Two Values and Re-Position Image CentreJavaScript - 将图像旋转到两个值之间的角度并重新定位图像中心
【发布时间】:2014-05-06 12:32:41
【问题描述】:

我正在尝试实现一个仪表,我有两个硬编码值 - min (0) 和 max (200),然后是通过 ajax 返回的计算值。

为简单起见,假设值为100,然后我试图找出将图像旋转到的正确角度。

我知道它是 0 度数(值 0-90 度数,200+90 度数)但我似乎找不到正确的公式来以编程方式计算它。

然后,如果返回正确的角度并旋转图像,则图像的底部将偏离中心,因此也需要重新计算(lefttop 位置)。

小提琴:http://jsfiddle.net/2uj5n/

JavaScript

$(document).ready(function() {

    var data = [{"count":100}]; // from ajax

    var error_rate = data[0].count;

    var max = 200;

    if (error_rate > max) {
        max = error_rate
        $('div.max-label').text(max);
    }

    $('#pointer_value').text(error_rate);

    var x = 0;
    var y = max;

    var theta = Math.atan2(-y, x);

    if (theta < 0) {
        theta += 2 * Math.PI;
     }

    var degree = theta * (180 / Math.PI); // 270 degrees - result should be 0


    var img = $('img.pointer');
    img.css('-moz-transform', 'rotate(' + degree + 'deg)');
    img.css('-webkit-transform', 'rotate(' + degree + 'deg)');
    img.css('-o-transform', 'rotate(' + degree + 'deg)');
    img.css('-ms-transform', 'rotate(' + degree + 'deg)');

});

HTML

<article class="widget">
    <div class="widget-inner">
        <header>
            <h1>Todays Error Rate</h1>
        </header>

        <section class="widget-body">
            <div class="widget-canvas">

                <div id="pointer_value" class="value">0</div>
                <div class="scale">
                    <div class="pointer-canvas">
                        <img class="pointer" src="https://d28q98t4icjy0h.cloudfront.net/assets/icons/geckometer-pointer.png" />
                    </div>
                </div>
                <div class="min">
                    <div class="t-tertiary">Min Errors</div>
                    <div class="min-label">0</div>
                </div>
                <div class="max">
                    <div class="t-tertiary">Max Errors</div>
                    <div class="max-label">200</div>
                </div>
                <div class="clearfix"></div>
            </div>
        </section>
    </div>
</article>

CSS

div.widget-canvas div.value {
  font-size: 48px;
  text-align: center;
  line-height: 36px;
}

div.widget-canvas .scale {
  width: 143px;
  height: 65px;
  margin-left: 27px;
  background: url(https://d28q98t4icjy0h.cloudfront.net/assets/icons-sced228de5c-269bfee7198436f43fe1f1efdf4d274c.png) no-repeat;
  background-position: 0 -343px;
  position: relative;
  margin: 50px auto 10px auto;
}

div.min {
  float: left;
}

div.min-label {
  color: #28b779;
}

div.max {
  float: right;
  text-align: right;
}

div.max-label {
  color: #d84a38;
  text-align: right;
}

div.widget-canvas .main-stat {
  font-size: 60px;
  height: 60px;
  line-height: 60px;
}

div.widget-canvas .secondary-stat {
  font-size: 48px;
  height: 48px;
  margin: 30px 0 0;
  position: relative;
}

div.widget-canvas .pointer-canvas {
  width: 48px;
  height: 48px;
  left: 50px;
  position: relative;
  top: 30px;
}

div.widget-canvas img.pointer {
  -moz-transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  -o-transform: rotate(-90deg);
  -ms-transform: rotate(-90deg);
}

【问题讨论】:

    标签: javascript


    【解决方案1】:

    只需使用以下内容:

    // calculate angle based on proportion
    var degree = 180 * error_rate / max;
    
    // rotate pointer container but not image
    var img = $('.pointer-canvas');
    

    演示: http://jsfiddle.net/2uj5n/1/

    【讨论】:

      【解决方案2】:

      如果您想要一个公式将0 to 200 转换为-90 to 90 那么:

      0 is -90 degrees
      100 is 0 degrees
      200 is 90 degress
      

      所以

      function rotation(input) {
          return (input-100) * .9;
      }
      

      【讨论】:

      • 嗯,那会是 90 而不是 0?
      • 如果输入为 100,则结果为 (100-100) * .9 = 0。您说“为简单起见,假设值为 100,(...) 我知道在我看来它是 0 度(值 0 是 -90 度,200 是 +90 度)(...)“
      • 啊,我以为您出于某种原因使用最大值(200)作为旋转函数的参数。我不确定这是否适用于任何价值,因为它似乎很难为100 价值工作,就是这样。
      • 嗯,100 是中心的值,0.9 是最大输入值 (200) 与以度为单位的最大值 (180) 之间的比率。您可以在那里使用可配置的值:return (input-(MAX-MIN)/2) * (180/MAX);
      猜你喜欢
      • 2014-07-24
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2010-09-26
      相关资源
      最近更新 更多