假设我有两个值:600和470
我要尝试的是指定一组时间,比如说0和100(如果为25fps,则为4秒),然后获取每个“帧”的“补间”值。
var base = 600;
var target = 470;
var start = 0;
var end = 100;
var values = [];
for (var i = start; i < end; i++) {
// not sure where to begin with the math here
// so i'll just asssign it to i, but obviously it's wrong
var value = i;
values.push(value);
}
// Once complete, id expect:
// values[0] === 600;
// values[99] === 470;
我该如何实现?
答案 0 :(得分:3)
计算增量(在数组中每个项目之间),然后可以通过将每个i
映射到base + increment * i
一次创建所有数组:
const base = 600;
const target = 470;
const start = 0;
const end = 100;
const increment = (target - base) / (end - start - 1);
const values = Array.from(
{ length: end - start },
(_, i) => base + increment * i
);
console.log(values[0]);
console.log(values[99]);
console.log(values);
答案 1 :(得分:0)
我为这个问题提供了一个简单的解决方案。
添加了两个变量:
差异
费率
基本值减去额定差值即可得到阶跃值。
var base = 600;
var target = 470;
var start = 0;
var end = 100;
var values = [];
var difference = base-target;
for (var i = start; i < end; i++) {
var rate = (i/(end-1));
var value = base - (difference*rate);
values.push(value);
}
// Once complete, id expect:
// values[0] === 600;
// values[99] === 470;
console.log(values);