vanilladocs
vanillapunch.com
API / core/animation

core/animation

애니메이션 처리기.

  • 원본 core/animation.js 를 옮긴 것이다. 프레임(Frame) 목록을 초당 프레임 수(animation_speed)로 넘기고, 반복하지 않으면 마지막 프레임에서 멈추고 완료 콜백을 부른다.
  • 원본도 컴포넌트가 아니라 그냥 객체다. 노드에 붙지 않고, 들고 있는 쪽(컴포넌트 등)이 tick 을 불러 주고 get_current_image / get_current_rect 로 지금 프레임을 꺼내 Sprite 에 넣는다.
  • 원본의 set_frames 계열은 배열이 아니거나 비면 그냥 돌아간다. 러스트 판은 타입이 정해져 있어서 빈 목록만 거른다. (set_frames 는 원본처럼 빈 목록도 받는다)
  • 프레임 번호는 usize 다. 원본 gotoAndPlay(-1) 처럼 음수를 0 으로 맞추는 일은 타입이 대신한다.

Animation handler.

  • A port of the original core/animation.js. Advances a list of frames (Frame) at a frames-per-second rate (animation_speed), and if not looping, stops at the last frame and calls the completion callback.
  • In the original it is also a plain object, not a component. It is not attached to a node; the holder (a component or such) calls tick and takes the current frame out with get_current_image / get_current_rect to put it in a Sprite.
  • The original's set_frames family simply returns if given a non-array or an empty one. In the Rust version the types are fixed, so only empty lists are filtered. (set_frames accepts an empty list as in the original)
  • Frame numbers are usize. The type takes care of clamping negatives to 0, which the original gotoAndPlay(-1) does.

Animationstruct traits Default

짝Counterpart: vanilla.js Animation소스source

애니메이션 처리기.

  • 프레임(Frame) 목록을 초당 프레임 수로 넘긴다. 노드에 붙지 않으므로 들고 있는 쪽이 tick 을 불러 주고 지금 프레임을 Sprite 에 건다.
  • 처음은 프레임 없음, 초당 10 프레임, 반복, 정지 상태.

Animation handler.

  • Advances a list of frames (Frame) at a frames-per-second rate. It is not attached to a node, so the holder calls tick and sets the current frame on a Sprite.
  • Starts with no frames, 10 frames per second, looping, stopped.
예제Example스프라이트 시트 네 칸을 돌려 Sprite 에 걸기Cycle four cells of a sprite sheet and set them on a Sprite
use vanilla::{Animation, Image, Rect, Sprite};

let sheet = Image::create_from_pixels(64, 16, vec![255u8; 64 * 16 * 4]);
let rects: Vec<Rect> = (0..4).map(|frame_index| Rect::create(16.0 * frame_index as f64, 0.0, 16.0, 16.0)).collect();
let mut animation = Animation::new();
animation.set_frames_from_rects(sheet, &rects);
animation.set_animation_speed(8.0);
animation.play();
animation.tick(0.25);
assert_eq!(animation.get_current_frame_index(), 2);

let mut sprite = Sprite::new();
sprite.set_frame(animation.get_current_frame());
assert_eq!(sprite.get_image_rect(), Rect::create(32.0, 0.0, 16.0, 16.0));
생성자constructorpub fn new() -> Animation#0.3.2 부터since 0.3.2소스source

생성. (초당 10 프레임, 반복, 정지 상태)

Constructor. (10 frames per second, looping, stopped)

반환ReturnsAnimation 프레임이 없는 새 처리기.A new handler with no frames.

짝Counterpart: vanilla.js Animation.constructor

메서드methodpub fn tick(&mut self, time_delta: f64)#0.3.2 부터since 0.3.2소스source

갱신. (쌓인 시간만큼 프레임을 넘긴다. 한 번에 여러 프레임을 넘길 수 있다)

  • 재생 중이 아니거나, 프레임이 없거나, 초당 프레임 수가 0 이하면 아무것도 하지 않는다. 한 프레임에 못 미친 시간은 다음 tick 으로 넘긴다.
  • 끝을 지나면 반복이면 처음으로 돌고, 아니면 마지막 프레임에서 멈추고 완료 콜백을 부른다.

Update. (advances frames by the accumulated time. Can advance several frames at once)

  • Does nothing if not playing, if there are no frames, or if the frames per second is 0 or less. Time short of one frame carries over to the next tick.
  • When passing the end, goes back to the start if looping; otherwise stops at the last frame and calls the completion callback.
time_deltaf64
지난 시간. (초)Elapsed time. (seconds)

짝Counterpart: vanilla.js Animation.tick

예제Example한 번에 두 프레임 넘기고 처음으로 돌기Advance two frames at once and wrap to the start
use vanilla::{Animation, Image, Rect};

let sheet = Image::create_from_pixels(48, 16, vec![255u8; 48 * 16 * 4]);
let rects = [Rect::create(0.0, 0.0, 16.0, 16.0), Rect::create(16.0, 0.0, 16.0, 16.0), Rect::create(32.0, 0.0, 16.0, 16.0)];
let mut animation = Animation::new();
animation.set_frames_from_rects(sheet, &rects);
animation.set_animation_speed(4.0);
animation.play();
animation.tick(0.125);
assert_eq!(animation.get_current_frame_index(), 0);
animation.tick(0.375);
assert_eq!(animation.get_current_frame_index(), 2);
animation.tick(0.25);
assert_eq!(animation.get_current_frame_index(), 0);
메서드methodpub fn play(&mut self)#0.3.2 부터since 0.3.2소스source

재생. (이미 재생 중이면 아무것도 하지 않는다)

Plays. (does nothing if already playing)

짝Counterpart: vanilla.js Animation.play

메서드methodpub fn pause(&mut self)#0.3.2 부터since 0.3.2소스source

일시정지. (지금 프레임과 쌓인 시간을 그대로 둔다)

Pause. (keeps the current frame and the accumulated time as they are)

짝Counterpart: vanilla.js Animation.pause

메서드methodpub fn resume(&mut self)#0.3.2 부터since 0.3.2소스source

재개. (멈춘 프레임에서 이어 간다)

Resume. (continues from the paused frame)

짝Counterpart: vanilla.js Animation.resume

메서드methodpub fn stop(&mut self)#0.3.2 부터since 0.3.2소스source

정지. (첫 프레임으로 되돌린다. 멈춰 있어도 되돌린다)

Stop. (returns to the first frame. Returns to it even when already stopped)

짝Counterpart: vanilla.js Animation.stop

메서드methodpub fn set_frames(&mut self, frames: Vec<Frame>)#0.3.2 부터since 0.3.2소스source

프레임 설정. (설정한 뒤 정지한다)

Sets the frames. (stops after setting)

framesVec<Frame>
차례대로 보일 프레임 목록. (빈 목록도 받는다)List of frames shown in order. (an empty list is also accepted)

짝Counterpart: vanilla.js Animation.setFrames

메서드methodpub fn set_frames_from_images(&mut self, images: &[Rc<Image>])#0.3.2 부터since 0.3.2소스source

프레임 설정. (낱장의 스프라이트 이미지 목록, 비면 무시한다)

Sets the frames. (a list of single sprite images, ignored if empty)

images&[Rc<Image>]
프레임마다 쓸 그림 목록. (프레임마다 그 그림 전체가 영역이다)List of images used per frame. (each frame's region is the whole image)

짝Counterpart: vanilla.js Animation.setFramesFromImages

메서드methodpub fn set_frames_from_rects(&mut self, image: Rc<Image>, rects: &[Rect])#0.3.2 부터since 0.3.2소스source

프레임 설정. (스프라이트 시트 한 장과 칸 영역 목록, 비면 무시한다)

Sets the frames. (one sprite sheet and a list of cell regions, ignored if empty)

imageRc<Image>
스프라이트 시트 그림.Sprite sheet image.
rects&[Rect]
프레임마다 쓸 시트 안의 칸 영역 목록. (픽셀, 시트 왼쪽 위 기준)List of cell regions in the sheet used per frame. (pixels, relative to the sheet's top left)

짝Counterpart: vanilla.js Animation.setFramesFromRects

메서드methodpub fn set_animation_speed(&mut self, animation_speed: f64)#0.3.2 부터since 0.3.2소스source

초당 프레임 수 설정. (예: 60 이면 초당 이미지가 60 번 바뀐다. 0 이하면 넘기지 않는다)

Sets the frames per second. (e.g. 60 changes the image 60 times per second. Does not advance if 0 or less)

animation_speedf64
초당 프레임 수. (처음 10)Frames per second. (initially 10)

짝Counterpart: vanilla.js Animation.setAnimationSpeed

메서드methodpub fn set_loop(&mut self, is_loop: bool)#0.3.2 부터since 0.3.2소스source

반복 여부 설정.

Sets whether to loop.

is_loopbool
true 면 끝에서 처음으로 돌고, false 면 마지막 프레임에서 멈춘다. (처음 true)If true, wraps from the end to the start; if false, stops at the last frame. (initially true)

짝Counterpart: vanilla.js Animation.setLoop

메서드methodpub fn set_on_complete(&mut self, callback: impl FnMut() + 'static)#0.3.2 부터since 0.3.2소스source

애니메이션 완료 콜백 설정. (반복하지 않을 때 마지막 프레임에 닿으면 불린다)

  • 다시 부르면 앞의 콜백을 바꾼다. 멈춘 뒤에는 다시 재생해 끝에 닿을 때까지 불리지 않는다.

Sets the animation completion callback. (called when reaching the last frame while not looping)

  • Calling again replaces the previous callback. After stopping, it is not called until playing again reaches the end.
callbackimpl FnMut() + 'static
끝에 닿을 때 부를 함수. (tick 안에서 불린다)Function called when reaching the end. (called inside tick)

짝Counterpart: vanilla.js Animation.setOnComplete

예제Example한 번 돌고 끝난 것 알기Know when it has played through once
use vanilla::{Animation, Image};
use std::cell::Cell;
use std::rc::Rc;

let images = [Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4]), Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4])];
let mut animation = Animation::new();
animation.set_frames_from_images(&images);
animation.set_loop(false);
let complete_count = Rc::new(Cell::new(0));
let complete_count_in_callback = complete_count.clone();
animation.set_on_complete(move || {
	complete_count_in_callback.set(complete_count_in_callback.get() + 1);
});
animation.play();
animation.tick(1.0);
assert_eq!(complete_count.get(), 1);
assert!(!animation.is_playing());
assert_eq!(animation.get_current_frame_index(), 1);
메서드methodpub fn goto_and_play(&mut self, index: usize)#0.3.2 부터since 0.3.2소스source

특정 프레임으로 이동하여 재생한다. (번호는 프레임 수 안으로 맞춘다)

Moves to a specific frame and plays. (the index is clamped to the frame count)

indexusize
옮겨 갈 프레임 번호. (0 부터, 프레임 수 - 1 보다 크면 마지막, 프레임이 없으면 0)Frame index to move to. (from 0; the last one if greater than frame count - 1, 0 if there are no frames)

짝Counterpart: vanilla.js Animation.gotoAndPlay

메서드methodpub fn goto_and_stop(&mut self, index: usize)#0.3.2 부터since 0.3.2소스source

특정 프레임으로 이동하여 정지한다. (번호는 프레임 수 안으로 맞춘다)

Moves to a specific frame and stops. (the index is clamped to the frame count)

indexusize
옮겨 갈 프레임 번호. (0 부터, 프레임 수 - 1 보다 크면 마지막, 프레임이 없으면 0)Frame index to move to. (from 0; the last one if greater than frame count - 1, 0 if there are no frames)

짝Counterpart: vanilla.js Animation.gotoAndStop

메서드methodpub fn is_playing(&self) -> bool#0.3.2 부터since 0.3.2소스source

재생 중인지 여부 반환.

Returns whether playing.

반환Returnsbool 재생 중이면 true.true if playing.

짝Counterpart: vanilla.js Animation.isPlaying

메서드methodpub fn get_current_frame(&self) -> Option<&Frame>#0.3.2 부터since 0.3.2소스source

현재 프레임 반환. (없으면 None)

Returns the current frame. (None if none)

반환ReturnsOption<&Frame> 지금 프레임. 프레임이 없으면 None.The current frame. None if there are no frames.

짝Counterpart: vanilla.js Animation.getCurrentFrame

메서드methodpub fn get_current_image(&self) -> Option<&Rc<Image>>#0.3.2 부터since 0.3.2소스source

현재 이미지 반환.

Returns the current image.

반환ReturnsOption<&Rc<Image>> 지금 프레임의 그림. 프레임이 없으면 None.Image of the current frame. None if there are no frames.

짝Counterpart: vanilla.js Animation.getCurrentImage

메서드methodpub fn get_current_rect(&self) -> Option<Rect>#0.3.2 부터since 0.3.2소스source

현재 영역 반환.

Returns the current region.

반환ReturnsOption<Rect> 지금 프레임의 그림 안 영역. (픽셀) 프레임이 없으면 None.Region within the image of the current frame. (pixels) None if there are no frames.

짝Counterpart: vanilla.js Animation.getCurrentRect

메서드methodpub fn get_current_frame_index(&self) -> usize#0.3.2 부터since 0.3.2소스source

현재 프레임 번호 반환.

Returns the current frame number.

반환Returnsusize 지금 프레임 번호. (0 부터)Current frame index. (from 0)

짝Counterpart: vanilla.js Animation.getCurrentFrameIndex

메서드methodpub fn get_total_frame_count(&self) -> usize#0.3.2 부터since 0.3.2소스source

모든 프레임 수 반환.

Returns the total number of frames.

반환Returnsusize 프레임 수.Frame count.

짝Counterpart: vanilla.js Animation.getTotalFrameCount