xnano.time
xnano.time
¶
xnano.time
A canvas's time: how long it has run, whether it is stopped, and the formulas that follow it.
Classes:
-
Time–The time a canvas runs on.
Time
¶
The time a canvas runs on.
Get it from Canvas.time or from context.time inside a
hook. Everything that moves follows it: animations, effects, formulas,
hook timers (every and after),
fixed-step updates and the musical beat. Any thread may read it. Used in arithmetic it is
the formula for seconds, so canvas.time / 2 is a value
that follows the clock. stop freezes all of them while input
keeps arriving, and step moves a frozen clock one frame at a
time.
elapsed, delta and fps are numbers you read now; seconds and
milliseconds are formulas that a placement keeps following. A
Frame carries the same numbers as elapsed_seconds and
delta_seconds. fps here is the rate measured over recent frames, while
Canvas.fps is the most frames the canvas draws per second.
Attributes:
-
elapsed(float) –Seconds since the canvas started, not counting time spent stopped.
-
elapsed_milliseconds(float) –The same, in milliseconds.
-
delta(float) –Seconds between the two most recent frames.
-
frame(int) –The number of steps the clock has counted.
-
fps(float) –The frame rate measured over recent frames.
-
is_stopped(bool) –Whether time is frozen.
-
is_realtime(bool) –Whether time follows the wall clock. A canvas from
xnano.new()withoutrealtime=Truemoves only when you move it. -
seconds(NumericValue) –A formula for
elapsed, recomputed every frame. -
milliseconds(NumericValue) –A formula for
elapsed_milliseconds.
Examples:
import xnano
with xnano.new(width=20, height=1) as canvas:
ball = canvas.write('o')
ball.x = (canvas.width - 1) * ((canvas.time.seconds / 2) % 1.0)
canvas.step(delta_seconds=1.0)
assert canvas.time.elapsed == 1.0
canvas.time.stop()
assert canvas.time.is_stopped
canvas.time.step(delta_seconds=0.5)
assert canvas.time.elapsed == 1.5
Create the canvas's time. Use canvas.time instead of calling this.
Parameters:
Methods:
-
stop–Freeze time.
-
resume–Let time run on from where
stopfroze it. -
step–Move time by
delta_seconds, one frame by default, and draw exactly one frame.
Source code in xnano/time.py
elapsed
property
¶
elapsed: float
Seconds since the canvas started, not counting time spent stopped.
elapsed_milliseconds
property
¶
elapsed_milliseconds: float
Milliseconds since the canvas started, not counting time spent stopped.
frame
property
¶
frame: int
The number of steps the clock has counted, including steps that drew nothing.
is_realtime
property
¶
is_realtime: bool
Whether time follows the wall clock instead of moving only when you move it.
seconds
property
¶
seconds: NumericValue
A formula for the elapsed seconds, recomputed every frame.
milliseconds
property
¶
milliseconds: NumericValue
A formula for the elapsed milliseconds, recomputed every frame.
stop
¶
Freeze time.
Animations, effects, formulas, timers, fixed-step updates and the musical beat stand
still. The canvas still draws and input still arrives, so a key can
resume it, and step moves it one
frame at a time. Stopping a stopped canvas does nothing.
Raises:
-
TerminalClosedError–The canvas is closed.
Source code in xnano/time.py
resume
¶
Let time run on from where stop froze it.
Resuming a canvas that is not stopped does nothing.
Raises:
-
TerminalClosedError–The canvas is closed.
Source code in xnano/time.py
step
¶
Move time by delta_seconds, one frame by default, and draw exactly one frame.
This is Canvas.step; see it for every case.
Parameters:
-
delta_seconds(float | None, default:None) –How far to move, above zero; one frame of the canvas's
fpswhenNone.
Returns: