2017-07-27 129 views
3

我正在使用d3版本3,我正在嘗試使用d3的動畫緩動功能。根據函數的定義,它有一個返回值如何獲取d3.ease的返回值?

function ease(type: 'cubic-in'): (t: number) => number; 

我期待一個數字作爲我的計算中使用它的返回值。但是,現在我怎樣才能將易用性函數的返回值轉換爲數字。 有沒有其他辦法可以達到這個目的?

謝謝。

+0

我編輯了問題的標題,以更好地解釋我相信,是你的問題。如果我錯了,就把它推回去。 –

+0

這是絕對正確的。萬分感謝。 @GerardoFurtado :) – Tums

回答

3

d3.ease已經返回一個數字。

你只需要你想要的值傳遞給d3.ease

d3.ease(type)(value); 

這裏是一個演示與"cubic-in",如你的問題:

function ease(value) { 
 
    return d3.ease("cubic-in")(value); 
 
} 
 

 
console.log("ease for 0.1: " + ease(4)); 
 
console.log("ease for 0.5: " + ease(0.5)); 
 
console.log("ease for 0.8: " + ease(0.8)); 
 
console.log("ease for 0.95: " + ease(0.95));
<script src="https://d3js.org/d3.v3.min.js"></script>

請請注意傳遞的值和返回值介於0和之間。根據API

的緩動函數取入域[0,1]當前參數化時間值t,並將其映射到以類似的範圍內的其他值。

+0

非常感謝:) – Tums

+0

請問你能指導我這個問題嗎? https://stackoverflow.com/questions/45472246/how-to-use-timer-in-d3-v3謝謝。 – Tums

+1

您需要'd3.timeout'而不是'd3.timer'。我現在不能寫回答,如果幾個小時內沒有人回答,我會告訴你如何去做。 –