vanilladocs
vanillapunch.com
API / misc/imagescroller

misc/imagescroller

이미지 스크롤러.

  • 원본 misc/imagescroller.js 를 옮긴 것이다. 이미지 한 장을 보이는 영역(view_rect) 안에 타일처럼 깔고, 속도에 맞춰 흘려 그린다. (무한 배경)
  • 원본처럼 컴포넌트가 아니라 그냥 값이다. 들고 있는 쪽이 tick 과 draw 를 불러 준다.
  • 원본과 다른 점: 이미지는 Rc<Image> 다. 원본 setImage 가 받던 ImageAsset 은 set_image_asset 으로 넘긴다. get_scroll_position / get_scroll_speed 는 원본처럼 고칠 수 있는 참조가 아니라 값을 돌려준다. (tick 은 안에서 바로 더한다)

Image scroller.

  • A port of the original misc/imagescroller.js. Tiles one image inside the visible area (view_rect) and draws it flowing at the set speed. (infinite background)
  • Like the original, it is a plain value, not a component. The owner calls tick and draw.
  • Differences from the original:
The image is `Rc<Image>`. The ImageAsset taken by the original setImage is passed with set_image_asset.
get_scroll_position / get_scroll_speed return values, not mutable references like the original. (tick adds to them directly inside)

ImageScrollerstruct traits Clone, Default

짝Counterpart: vanilla.js ImageScroller소스source

이미지 스크롤러.

  • 스크롤 위치가 늘면 그림은 오른쪽(가로), 아래(세로)로 흘러간다.

Image scroller.

  • As the scroll position increases, the image flows right (horizontal) and down (vertical).
예제Example왼쪽으로 흐르는 배경의 첫 타일 자리Position of the first tile of a background flowing left
use vanilla::{Image, ImageScroller, Rect, Vector2};

let mut image_scroller = ImageScroller::new();
image_scroller.set_image(Some(Image::create_from_pixels(100, 100, vec![255u8; 100 * 100 * 4])));
image_scroller.set_view_rect(Rect::create(10.0, 20.0, 300.0, 200.0));
image_scroller.set_horizontal_scroll_speed(-50.0);
image_scroller.tick(1.0);
assert_eq!(image_scroller.get_scroll_position(), Vector2::create(-50.0, 0.0));
assert_eq!(image_scroller.calculate_tile_start(100.0, 100.0), Vector2::create(-40.0, 20.0));
예제Example씬에서 배경 흘려 그리기Draw a flowing background in a scene
use vanilla::{Engine, Graphic, ImageScroller, Rect};

fn create_background(engine: &Engine) -> ImageScroller {
	let mut image_scroller = ImageScroller::new();
	image_scroller.set_image(engine.load_image("examples/assets/atlas/icons.png").ok());
	image_scroller.set_view_rect(Rect::create(0.0, 0.0, 1280.0, 720.0));
	image_scroller.set_vertical_scroll_speed(40.0);
	image_scroller
}

fn draw_background(image_scroller: &mut ImageScroller, graphic: &mut Graphic, time_delta: f64) {
	image_scroller.tick(time_delta);
	image_scroller.draw(graphic);
}
생성자constructorpub fn new() -> ImageScroller#0.3.2 부터since 0.3.2소스source

생성. (이미지 없음, 영역 0, 멈춤)

Constructor. (no image, area 0, stopped)

반환ReturnsImageScroller 이미지가 없고 스크롤 위치와 속도가 0 인 스크롤러.A scroller with no image and scroll position and speed of 0.

짝Counterpart: vanilla.js ImageScroller.constructor

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

갱신. (속도만큼 스크롤 위치를 옮긴다)

Update. (moves the scroll position by the speed)

time_deltaf64
지난 프레임부터 흐른 시간. (초)Time elapsed since the last frame. (seconds)

짝Counterpart: vanilla.js ImageScroller.tick

메서드methodpub fn calculate_tile_start(&self, image_width: f64, image_height: f64) -> Vector2#0.3.2 부터since 0.3.2소스source

첫 타일의 시작 좌표. (스크롤 위치를 이미지 크기로 나눈 나머지만큼 당긴다. 좌표는 정수로 내려 틈을 막는다)

Start coordinate of the first tile. (pulled back by the remainder of the scroll position divided by the image size. Coordinates are floored to integers to prevent gaps)

image_widthf64
이미지 너비. (픽셀, 0 이면 나머지가 NaN 이다)Image width. (pixels, the remainder is NaN if 0)
image_heightf64
이미지 높이. (픽셀, 0 이면 나머지가 NaN 이다)Image height. (pixels, the remainder is NaN if 0)

반환ReturnsVector2 첫 타일의 왼쪽 위 좌표. (보이는 영역 왼쪽 위와 같거나 이미지 한 장 안쪽만큼 앞)Top left coordinate of the first tile. (equal to the top left of the visible area or up to one image ahead of it)

메서드methodpub fn draw(&self, graphic: &mut Graphic)#0.3.2 부터since 0.3.2소스source

출력. (보이는 영역으로 자르고, 1.5 픽셀씩 겹쳐 그려 타일 사이 틈을 가린다)

  • 이미지가 없거나 너비, 높이가 0 이면 그리지 않는다.

Draw. (clips to the visible area and overlaps by 1.5 pixels to hide gaps between tiles)

  • Draws nothing if there is no image or its width or height is 0.
graphic&mut Graphic
그릴 그래픽. (보이는 영역과 같은 좌표계)Graphic to draw to. (same coordinate system as the visible area)

짝Counterpart: vanilla.js ImageScroller.draw

메서드methodpub fn set_image(&mut self, image: Option<Rc<Image>>)#0.3.2 부터since 0.3.2소스source

이미지 설정. (None 이면 그리지 않는다)

Sets the image. (None draws nothing)

imageOption<Rc<Image>>
깔 이미지. None 이면 그리지 않는다.Image to tile. None draws nothing.

짝Counterpart: vanilla.js ImageScroller.setImage

메서드methodpub fn set_image_asset(&mut self, image_asset: &mut ImageAsset)#0.3.2 부터since 0.3.2소스source

이미지 애셋의 이미지로 설정. (아직 읽히지 않았으면 None)

  • 부를 때의 이미지를 한 번 가져온다. 나중에 애셋이 다 읽혀도 저절로 바뀌지 않는다.

Sets the image from an image asset. (None if not loaded yet)

  • Takes the image once at call time. It does not change automatically when the asset finishes loading later.
image_asset&mut ImageAsset
이미지를 꺼낼 애셋.Asset to take the image from.
메서드methodpub fn get_image(&self) -> Option<&Rc<Image>>#0.3.2 부터since 0.3.2소스source

이미지 반환.

Returns the image.

반환ReturnsOption<&Rc<Image>> 깔린 이미지의 참조. 없으면 None.Reference to the tiled image. None if none.

짝Counterpart: vanilla.js ImageScroller.getImage

메서드methodpub fn set_view_rect(&mut self, rect: Rect)#0.3.2 부터since 0.3.2소스source

보이는 영역 설정. (부른 쪽 좌표계)

Sets the visible area. (caller's coordinate system)

rectRect
타일을 깔고 자를 영역.Area to tile and clip to.

짝Counterpart: vanilla.js ImageScroller.setViewRect

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

보이는 영역 반환.

Returns the visible area.

반환ReturnsRect 타일을 깔고 자를 영역.Area to tile and clip to.

짝Counterpart: vanilla.js ImageScroller.getViewRect

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

세로 스크롤 속도 설정. (초당 픽셀)

Sets the vertical scroll speed. (pixels per second)

speedf64
세로 속도. (초당 픽셀, + 면 그림이 아래로 흐른다)Vertical speed. (pixels per second, + makes the image flow down)

짝Counterpart: vanilla.js ImageScroller.setVerticalScrollSpeed

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

가로 스크롤 속도 설정. (초당 픽셀)

Sets the horizontal scroll speed. (pixels per second)

speedf64
가로 속도. (초당 픽셀, + 면 그림이 오른쪽으로 흐른다)Horizontal speed. (pixels per second, + makes the image flow right)

짝Counterpart: vanilla.js ImageScroller.setHorizontalScrollSpeed

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

스크롤 속도 반환.

Returns the scroll speed.

반환ReturnsVector2 가로(x), 세로(y) 속도. (초당 픽셀, 복사본)Horizontal (x) and vertical (y) speed. (pixels per second, a copy)

짝Counterpart: vanilla.js ImageScroller.getScrollSpeed

메서드methodpub fn set_scroll_position(&mut self, position: Vector2)#0.3.2 부터since 0.3.2소스source

스크롤 위치 설정.

Sets the scroll position.

positionVector2
스크롤 위치. (픽셀, 이미지 크기로 나눈 나머지만 그림에 보인다)Scroll position. (pixels, only the remainder divided by the image size shows in the image)

짝Counterpart: vanilla.js ImageScroller.setScrollPosition

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

스크롤 위치 반환.

Returns the scroll position.

반환ReturnsVector2 지금 스크롤 위치. (픽셀, 복사본)Current scroll position. (pixels, a copy)

짝Counterpart: vanilla.js ImageScroller.getScrollPosition