vanilladocs
vanillapunch.com
API / core/component/shadowimage

core/component/shadowimage

그림자를 깐 그림. (vanilla.js core/component/shadowimage.js 의 ShadowImage)

  • 그림(또는 그림 안의 조각)을 노드 안에 비를 지켜 가장 크게, 가운데로 그린다.
  • ShadowText 와 같이 그림의 모양(불투명도) 밑에 그림자를 깐 모습을 한 장으로 구워 그 한 장을 그린다. 노드의 불투명도가 그림과 그림자에 한 장째로 먹는다.
  • 그림 색을 주면 그림을 그 색 한 빛의 모양으로 칠한다. (같은 자리 위에 원래 그림을 얹어 그림자만 남기는 감쌈 등)
  • 굽는 배율, 그림, 색, 그림자, 노드 크기가 바뀌면 다시 굽는다. 데스크탑의 줄이기는 겹친 넓이로 고르게 섞는다. 같은 그림(Image 의 인스턴스 식별자와 조각)과 모양을 구운 것이 있으면 나눠 쓴다. (sharedbakecache.rs, 원본은 그림마다 번호를 붙여 열쇠로 쓴다)
  • 그림 경로(image_path)는 Sprite 의 것과 같이 UI 문서(UIDocument)가 그림 대신 적고 읽는 값이다. 그리기에는 쓰지 않는다.
  • 다시 구울 때 그림의 CPU 픽셀을 읽으므로 받은 그림은 GPU 에 올린 뒤에도 픽셀을 남기게 한다. 이미 놓였으면 텍스처에서 되읽는다.
  • 웹은 원본처럼 캔버스의 drawImage(imageSmoothingQuality high)로 줄이고 색은 source-in 으로 칠한 뒤 캔버스 그림자(shadowBlur)를 깔아 굽는다. (platform/web/systemfont.rs 의 bake_shadow_image) 데스크탑은 겹친 넓이로 줄이고 상자 흐림 세 번으로 그림자를 셈한다.
  • 원본 getBakeCanvas 는 구운 캔버스 대신 구운 Image 를 돌려주는 get_bake_image 다.

Image with a shadow. (ShadowImage in vanilla.js core/component/shadowimage.js)

  • Draws the image (or a piece within the image) inside the node as large as possible while keeping its ratio, centered.
  • Like ShadowText, bakes the look of the shadow laid beneath the image's shape (opacity) into one sheet and draws that single sheet. The node's opacity applies to the image and shadow as one sheet.
  • If an image color is given, paints the image as a single-color shape of that color. (e.g. a wrap that places the original image on top at the same spot so only the shadow remains)
  • Rebakes when the bake scale, image, color, shadow or node size changes. Shrinking on desktop blends evenly by overlapping area. If a bake of the same image (the Image's instance identifier and piece) and look already exists, it is shared. (sharedbakecache.rs, the original numbers each image and uses that as the key)
  • The image path (image_path), like Sprite's, is the value the UI document (UIDocument) writes and reads in place of the image. It is not used for drawing.
  • Rebaking reads the image's CPU pixels, so the given image keeps its pixels even after being uploaded to the GPU. If they were already released, they are read back from the texture.
  • On the web, as in the original, it shrinks with the canvas drawImage (imageSmoothingQuality high), paints the color with source-in, then lays the canvas shadow (shadowBlur) and bakes. (bake_shadow_image in platform/web/systemfont.rs) Desktop shrinks by overlapping area and computes the shadow with three box blurs.
  • The original getBakeCanvas is get_bake_image, which returns the baked Image instead of the baked canvas.

ShadowImagestruct traits Default, Component

짝Counterpart: vanilla.js ShadowImage소스source

그림자를 깐 그림 컴포넌트. (그림 밑에 그 모양의 그림자를 깐 모습을 한 장으로 구워 그린다)

  • 그림(또는 그림 안의 조각)을 노드 content_size 안에 비를 지켜 가장 크게, 가운데로 놓는다.
  • 노드의 불투명도가 그림과 그림자에 한 장째로 먹는다. 그림 색을 주면 그림을 그 색 한 빛의 모양으로 칠한다.
  • 처음은 그림 없음, 그림 색 없음, 검은 그림자, 내림 0, 번짐 0.

Image with a shadow component. (bakes the look of the image with a shadow of its shape beneath it into one sheet and draws it)

  • Places the image (or a piece within the image) inside the node's content_size as large as possible while keeping its ratio, centered.
  • The node's opacity applies to the image and shadow as one sheet. If an image color is given, paints the image as a single-color shape of that color.
  • Initially no image, no image color, a black shadow, drop 0, blur 0.
예제Example아이콘 밑에 흐린 그림자 깔기Lay a blurred shadow beneath an icon
use vanilla::{Color, Image, NodeTree, ShadowImage, Vector2};

let icon = Image::create_from_pixels(32, 32, vec![255u8; 32 * 32 * 4]);
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let icon_id = node_tree.create_child(root_id, "Icon");
node_tree[icon_id].set_content_size(Vector2::create(64.0, 64.0));
let shadow_image = node_tree.add_component(icon_id, ShadowImage::new()).unwrap();
shadow_image.set_image(Some(icon.clone()), None);
shadow_image.set_shadow(Color::black(), 0.5, 2.0, 4.0);

let shadow_image = node_tree[icon_id].get_component::<ShadowImage>().unwrap();
assert!(shadow_image.get_image().is_some());
assert!(icon.is_pixels_retained());
assert!(shadow_image.get_bake_image().is_none());
생성자constructorpub fn new() -> ShadowImage#0.4.0 부터since 0.4.0소스source

생성.

Constructor.

반환ReturnsShadowImage 그림이 없고 검은 그림자(내림 0, 번짐 0)인 새 컴포넌트.New component with no image and a black shadow (drop 0, blur 0).

짝Counterpart: vanilla.js ShadowImage.constructor

메서드methodpub fn draw_shadow_image(&mut self, transform: &NodeTransform, graphic: &mut Graphic)#0.4.0 부터since 0.4.0소스source

출력.

  • 그림이 없거나, 조각이나 노드 크기가 0 이하면 그리지 않는다.
  • 굽는 배율, 그림, 조각, 색, 그림자, 노드 크기가 지난번과 다르면 다시 굽는다. 그림의 CPU 픽셀을 아직 되읽지 못했으면 이번에는 그리지 않고 다음에 다시 굽는다.
  • 구운 한 장은 그림자 여백만큼 넓어서 그림 자리보다 왼쪽 위로 여백만큼 당겨 그린다.

Draw.

  • Does not draw if there is no image, or if the piece or node size is 0 or less.
  • Rebakes if the bake scale, image, piece, color, shadow or node size differs from last time. If the image's CPU pixels have not been read back yet, it does not draw this time and rebakes next time.
  • The baked sheet is wider by the shadow margin, so it is drawn pulled up and left from the image position by the margin.
transform&NodeTransform
그릴 노드 트랜스폼. (content_size 를 읽는다)Node transform to draw on. (reads content_size)
graphic&mut Graphic
그릴 그래픽. (노드 트랜스폼이 걸린 상태, 굽는 배율과 텍스처 되읽기에 쓴다)Graphic to draw to. (with the node transform applied, used for the bake scale and texture readback)
메서드methodpub fn set_image(&mut self, image: Option<Rc<Image>>, image_rect: Option<Rect>)#0.4.0 부터since 0.4.0소스source

그림 설정. (조각을 주지 않으면 그림 전체)

  • 받은 그림은 다시 구울 때 픽셀을 읽으므로 GPU 에 올린 뒤에도 CPU 픽셀을 남기게 한다. (Image::set_pixels_retained(true))

Sets the image. (the whole image if no piece is given)

  • Rebaking reads the pixels of the given image, so it keeps its CPU pixels even after being uploaded to the GPU. (Image::set_pixels_retained(true))
imageOption<Rc<Image>>
그릴 그림. (None 이면 그리지 않는다)Image to draw. (does not draw if None)
image_rectOption<Rect>
그림 안의 조각. (픽셀, 그림 왼쪽 위 기준, None 이면 그림 전체)Piece within the image. (pixels, relative to the image's top left, the whole image if None)

짝Counterpart: vanilla.js ShadowImage.setImage

예제Example시트의 오른쪽 칸만 그리기Draw only the right cell of a sheet
use vanilla::{Image, Rect, ShadowImage};

let sheet = Image::create_from_pixels(64, 32, vec![255u8; 64 * 32 * 4]);
let mut shadow_image = ShadowImage::new();
shadow_image.set_image(Some(sheet), Some(Rect::create(32.0, 0.0, 32.0, 32.0)));
assert_eq!(shadow_image.get_image_rect(), Some(Rect::create(32.0, 0.0, 32.0, 32.0)));

shadow_image.set_image(None, None);
assert!(shadow_image.get_image().is_none());
assert_eq!(shadow_image.get_image_rect(), None);
메서드methodpub fn get_image(&self) -> Option<&Rc<Image>>#0.4.0 부터since 0.4.0소스source

그림 반환.

Returns the image.

반환ReturnsOption<&Rc<Image>> 그리는 그림. 정하지 않았으면 None.The image being drawn. None if not set.

짝Counterpart: vanilla.js ShadowImage.getImage

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

그림 안의 조각 반환. (None 이면 그림 전체)

Returns the piece within the image. (the whole image if None)

반환ReturnsOption<Rect> 그림 안의 조각. (픽셀, None 이면 그림 전체)Piece within the image. (pixels, the whole image if None)

짝Counterpart: vanilla.js ShadowImage.getImageRect

메서드methodpub fn set_image_color(&mut self, image_color: Option<Color>)#0.4.0 부터since 0.4.0소스source

그림을 한 빛으로 칠할 색 설정. (None 이면 그림 그대로)

Sets the color to paint the image in a single color. (the image as is if None)

image_colorOption<Color>
그림 모양을 칠할 색. (알파는 그림 알파에 곱한다, None 이면 그림 색 그대로, 처음 None)Color to paint the image shape with. (alpha is multiplied by the image alpha, the image colors as is if None, initially None)

짝Counterpart: vanilla.js ShadowImage.setImageColor

메서드methodpub fn get_image_color(&self) -> Option<Color>#0.4.0 부터since 0.4.0소스source

그림을 한 빛으로 칠할 색 반환.

Returns the color for painting the image in a single color.

반환ReturnsOption<Color> 그림 모양을 칠할 색. 그림 색 그대로면 None.Color to paint the image shape with. None if the image colors are kept as is.

짝Counterpart: vanilla.js ShadowImage.getImageColor

메서드methodpub fn set_image_path(&mut self, image_path: &str)#0.4.0 부터since 0.4.0소스source

그림 경로 설정. (UI 문서가 적고 읽는다)

Sets the image path. (written and read by the UI document)

image_path&str
그림 경로. (적어 두기만 하고 그림을 읽지 않는다)Image path. (only recorded, the image is not loaded)

짝Counterpart: vanilla.js ShadowImage.setImagePath

메서드methodpub fn get_image_path(&self) -> &str#0.4.0 부터since 0.4.0소스source

그림 경로 반환. (정하지 않았으면 빈 글. UI 문서는 Sprite 처럼 imagePath 로 따로 적으므로 반사 저장 속성에서 뺀다)

Returns the image path. (empty string if not set. The UI document writes it separately as imagePath like Sprite, so it is excluded from reflection saved properties)

반환Returns&str 그림 경로. (정하지 않았으면 빈 글)Image path. (empty string if not set)

짝Counterpart: vanilla.js ShadowImage.getImagePath

메서드methodpub fn set_shadow(&mut self, shadow_color: impl Into<Color>, shadow_opacity: f64, shadow_offset: f64, shadow_blur: f64)#0.4.0 부터since 0.4.0소스source

그림자 설정. (색, 불투명도, 아래로 내려가는 만큼, 번지는 만큼, 원본 setShadow(색, 불투명도, 내려가는 만큼, 번지는 만큼) 과 같다)

Shadow settings. (color, opacity, downward offset, blur amount; same as the original setShadow(color, opacity, downward offset, blur amount))

shadow_colorimpl Into<Color>
그림자 색. (Color 또는 "#rrggbb" 같은 문자열, 색의 알파는 읽지 않는다, 처음 검정)Shadow color. (a Color or a string like "#rrggbb"; the alpha of the color is not read; black at first)
shadow_opacityf64
그림자 불투명도. (0 ~ 1, 처음 1)Shadow opacity. (0 to 1, 1 at first)
shadow_offsetf64
그림자가 아래로 내려가는 만큼. (노드 좌표, 처음 0)How far the shadow drops down. (node coordinates, initially 0)
shadow_blurf64
그림자가 번지는 만큼. (노드 좌표, 0 보다 작으면 0, 처음 0)How far the shadow blurs. (node coordinates, 0 if less than 0, initially 0)

짝Counterpart: vanilla.js ShadowImage.setShadow

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

구운 그림 반환. (아직 굽지 않았으면 None, 원본 getBakeCanvas)

Returns the baked image. (None if not baked yet, the original getBakeCanvas)

반환ReturnsOption<&Rc<Image>> 마지막으로 구운 그림. (그림자 여백을 둘러 굽는 배율로 구운 한 장) 아직 굽지 않았으면 None.The last baked image. (one sheet baked at the bake scale with the shadow margin around it) None if not baked yet.