我想創建一個小型控制器,有點像汽車裏的車速表,只是它只能跨越180度。如何計算旋轉圖像的正確位置偏移量?
要呈現控件我使用兩個圖像文件。第一個只是測量儀的背景,顯示了一個從0度的綠色變爲180度的紅色的半圓。第二個圖像是一個垂直箭頭,高度與半圓半徑大致相同,略低於背景圖像的高度。
我使用以下標記來呈現控制:
<div class="gauge" id="@Model.ClientID">
<img class="gauge" alt="" src="@Url.Content("~/images/icons/gauge-bg.png")" />
<img class="arrow" alt="" src="@Url.Content("~/images/icons/gauge-arrow.png")" />
</div>
的圖像被以使它們重疊相對定位在div內部。背景圖像是120x63像素,箭頭圖像是12x58像素(WxH),但如果解決問題更容易,可以進行調整。在應用任何旋轉之前,箭頭圖像會直線向上。
我有一個百分比(從0到100),我轉化爲箭頭圖像(從-90至90),它看起來像這樣的旋轉角度:
// input value for the offset calculations (see below)
public double ArrowRotationDegrees
{
// 0 for 0%, 90 for 50% and 180 for 100%
get { return 180.0 * Percent/100; }
}
// input value for the jquery rotate plugin as our base arrow points vertically up
public double ArrowRotationDegreesRelative // -90 for 0%, 0 for 50% and 90 for 100%
{
// -90 for 0%, 0 for 50% and 90 for 100%
get { return ArrowRotationDegrees - 90; }
}
爲了執行旋轉我正在使用jquery-rotate,它似乎總是圍繞圖像的中心旋轉。
<script type="text/javascript">
$(document).ready(function() {
var arrow = $('#@Model.ClientID').find('img.arrow');
arrow.rotate(@Model.ArrowRotationDegreesRelative);
arrow.css({left: @Model.ArrowLeftShiftPixels, top: @Model.ArrowTopShiftPixels});
});
</script>
但是我怎麼計算重新定位旋轉的圖像正確的偏移,使得箭頭總是從背景圖像的確切中心底點?
更新 - 解
基於埃裏克J.答案我能調整我的代碼,並獲得工作的結果,而不改變標記:
public int ArrowLeftShiftPixels // used to position rotated image correctly horizontally
{
// default offset (no rotation) is left:55
get { return 55 - GetArrowLeftShift(ArrowRotationDegrees); }
}
public int ArrowTopShiftPixels // used to position rotated image correctly vertically
{
// default offset (no rotation) is top:5
// formula output is shifted (max appears where min should be); add 29 to reverse this effect
get { return 34 - GetArrowTopShift(ArrowRotationDegrees); }
}
public int GetArrowLeftShift(double degrees)
{
var result = (58.0/2) * Math.Cos(degrees * Math.PI/180);
return (int) Math.Round(result, 0);
}
public int GetArrowTopShift(double degrees)
{
var result = (58.0/2) * Math.Sin(degrees * Math.PI/180);
return (int) Math.Round(result, 0);
}
感謝所有幫助和建議!
起初,我無法完成這項工作,直到我創建了工作/預期輸出值表並將其與計算結果進行比較。 TopShift值會以某種方式發生變化(最小值出現在最大值出現的位置),但一旦發現問題很容易進行補償。謝謝! – 2012-03-15 00:06:16