misc/fixedstepper
고정 걸음.
Fixed step.
FixedStepperextends Object
고정 걸음.
- 프레임 간격이 들쭉날쭉해도 규칙(물리, 판의 흐름)은 늘 같은 걸음(stepSeconds)으로 돌린다.
- advance(timeDelta, stepFunction) 가 받은 시간을 쌓아 두고 걸음만큼 찰 때마다 stepFunction(stepSeconds) 을 부른다. 돌려주는 값은 남은 몫(0 ~ 1)이다. 앞 걸음과 지금 걸음 사이를 이 몫만큼 섞어 그리면 걸음이 화면 주사율과 어긋나 떨리지 않는다.
- 한 프레임에 maximumStepsPerFrame 걸음까지만 돈다. 그만큼 돌고도 남았으면 남은 시간을 버린다. (느린 기기에서 멈칫한 뒤 밀린 것을 한꺼번에 몰아 돌다 더 느려지지 않게)
- stepFunction 이 false 를 돌려주면 이번 프레임의 걸음을 거기서 멈추고 남은 시간은 쌓아 둔다. (회의, 연출처럼 잠시 붙잡을 때)
Fixed step.
- Even if frame intervals are uneven, rules (physics, round flow) always run at the same step (stepSeconds).
- advance(timeDelta, stepFunction) accumulates the received time and calls stepFunction(stepSeconds) each time a full step accumulates. The returned value is the remaining fraction (0 ~ 1). Drawing by blending the previous step and the current step by this fraction keeps the steps from jittering against the screen refresh rate.
- Runs at most maximumStepsPerFrame steps per frame. If time still remains after that many, the remaining time is discarded. (So a slow device does not get even slower by running the backlog all at once after a stall)
- If stepFunction returns false, this frame's steps stop there and the remaining time stays accumulated. (For holding briefly, as in meetings or cutscenes)
import { FixedStepper } from "./build/vanilla.js";
const fixedStepper = new FixedStepper(1 / 60, 5);
let position = 0;
const velocity = 120;
const frameDeltaList = [0.016, 0.033, 0.008, 0.050];
for (const timeDelta of frameDeltaList) {
const alpha = fixedStepper.advance(timeDelta, (stepSeconds) => {
position += velocity * stepSeconds;
});
console.log(fixedStepper.getLastStepCount(), alpha.toFixed(2), position.toFixed(2));
}생성.
Constructor.
- stepSecondsnumber
- 한 걸음의 시간. (초, 기본 1 / 60)Duration of one step. (Seconds, default 1 / 60)
- maximumStepsPerFramenumber
- 한 프레임에 도는 걸음의 상한. (기본 5)Maximum steps run per frame. (default 5)
짝Counterpart: vanilla-engine FixedStepper.new
한 걸음의 시간 설정. (초, 0 보다 커야 한다. 아니면 그대로 둔다)
Sets the duration of one step. (Seconds, must be greater than 0. Otherwise left unchanged)
- stepSecondsnumber
- 한 걸음의 시간. (초)Time of one step. (seconds)
짝Counterpart: vanilla-engine FixedStepper.set_step_seconds
한 걸음의 시간 반환. (초)
Returns the duration of one step. (Seconds)
반환Returnsnumber 한 걸음의 시간. (초)Time of one step. (seconds)
짝Counterpart: vanilla-engine FixedStepper.get_step_seconds
한 프레임에 도는 걸음의 상한 설정. (1 이상, 소수는 버린다)
Sets the maximum steps run per frame. (1 or more, fractions are discarded)
- maximumStepsPerFramenumber
- 한 프레임에 도는 걸음의 상한.Maximum number of steps run per frame.
짝Counterpart: vanilla-engine FixedStepper.set_maximum_steps_per_frame
한 프레임에 도는 걸음의 상한 반환.
Returns the maximum steps run per frame.
반환Returnsnumber 한 프레임에 도는 걸음의 상한.Maximum number of steps run per frame.
짝Counterpart: vanilla-engine FixedStepper.get_maximum_steps_per_frame
나아가기. (받은 시간만큼 걸음을 돌리고 남은 몫을 돌려준다)
Advances. (Runs steps for the received time and returns the remaining fraction)
- timeDeltanumber
- 이번 프레임의 시간. (초, 음수나 숫자가 아니면 0)Time of this frame. (Seconds, 0 if negative or not a number)
- stepFunction(stepSeconds: number) => (boolean | void)
- 한 걸음. false 를 돌려주면 이번 프레임은 거기서 멈춘다.One step. Returning false stops this frame there.
반환Returnsnumber 남은 몫. (0 ~ 1)Remaining fraction. (0 ~ 1)
짝Counterpart: vanilla-engine FixedStepper.advance
import { FixedStepper } from "./build/vanilla.js";
const fixedStepper = new FixedStepper();
let previousPosition = 0;
let currentPosition = 0;
let isPaused = false;
const step = (stepSeconds) => {
if (isPaused) {
return false;
}
previousPosition = currentPosition;
currentPosition += 60 * stepSeconds;
return true;
};
const alpha = fixedStepper.advance(0.025, step);
const drawPosition = previousPosition + (currentPosition - previousPosition) * alpha;
console.log(fixedStepper.getLastStepCount(), drawPosition);
isPaused = true;
fixedStepper.advance(0.1, step);
console.log(fixedStepper.getLastStepCount(), fixedStepper.getAlpha());남은 몫. (쌓인 시간 / 한 걸음, 0 ~ 1)
Remaining fraction. (Accumulated time / one step, 0 ~ 1)
반환Returnsnumber 남은 몫. (0 ~ 1)Remaining fraction. (0 ~ 1)
짝Counterpart: vanilla-engine FixedStepper.get_alpha
지난 advance 에서 돈 걸음 수.
Number of steps run in the last advance.
반환Returnsnumber 지난 advance 에서 돈 걸음 수.Number of steps run in the last advance.
짝Counterpart: vanilla-engine FixedStepper.get_last_step_count
쌓인 시간 비우기. (판을 새로 시작하거나 붙잡았다 풀 때)
Clears the accumulated time. (When starting a new round or releasing a hold)
짝Counterpart: vanilla-engine FixedStepper.reset