마스킹

Masking

다른 레이어의 투명도나 밝기를 읽어 이미지가 보일 영역을 제한합니다.

다른 이름: Layer maskMask

WebGPU 를 지원하는 브라우저에서 움직입니다

이 기기에서는 데모를 그리지 못해 정지 화면을 보여 줍니다

React
// WebGPU 를 지원하는 브라우저에서만 그립니다.
// /samples/… 는 이 사이트의 샘플 파일입니다. 내 프로젝트의 이미지·영상 경로로 바꾸세요.
import { Shader, Circle, ImageTexture } from 'shaders/react'

export default function ShaderEffect() {
  return (
    <Shader style={{ width: '100%', height: 320 }} disableTelemetry>
      <Circle
        id="reveal"
        radius={{
          type: "auto-animate",
          mode: "ping-pong",
          outputMin: 0.25,
          outputMax: 0.6,
          speed: 0.5
        }}
        softness={0.2}
        visible={false} />
      <ImageTexture
        maskSource="reveal"
        objectFit="cover"
        url="/samples/photo.jpg" />
    </Shader>
  )
}
Vue
<!-- WebGPU 를 지원하는 브라우저에서만 그립니다. -->
<!-- /samples/… 는 이 사이트의 샘플 파일입니다. 내 프로젝트의 이미지·영상 경로로 바꾸세요. -->
<script setup lang="ts">
import { Shader, Circle, ImageTexture } from 'shaders/vue'
</script>

<template>
  <Shader style="width: 100%; height: 320px" :disable-telemetry="true">
    <Circle
      id="reveal"
      :radius="{
        type: 'auto-animate',
        mode: 'ping-pong',
        outputMin: 0.25,
        outputMax: 0.6,
        speed: 0.5
      }"
      :softness="0.2"
      :visible="false"/>
    <ImageTexture
      mask-source="reveal"
      object-fit="cover"
      url="/samples/photo.jpg"/>
  </Shader>
</template>
Svelte
<!-- WebGPU 를 지원하는 브라우저에서만 그립니다. -->
<!-- /samples/… 는 이 사이트의 샘플 파일입니다. 내 프로젝트의 이미지·영상 경로로 바꾸세요. -->
<script>
  import { Shader, Circle, ImageTexture } from 'shaders/svelte'
</script>

<Shader style="width: 100%; height: 320px" disableTelemetry>
  <Circle
    id="reveal"
    radius={{
      type: "auto-animate",
      mode: "ping-pong",
      outputMin: 0.25,
      outputMax: 0.6,
      speed: 0.5
    }}
    softness={0.2}
    visible={false} />
  <ImageTexture
    maskSource="reveal"
    objectFit="cover"
    url="/samples/photo.jpg" />
</Shader>
Solid
// WebGPU 를 지원하는 브라우저에서만 그립니다.
// /samples/… 는 이 사이트의 샘플 파일입니다. 내 프로젝트의 이미지·영상 경로로 바꾸세요.
import { Shader, Circle, ImageTexture } from 'shaders/solid'

export default function ShaderEffect() {
  return (
    <Shader style={{ width: '100%', height: '320px' }} disableTelemetry>
      <Circle
        id="reveal"
        radius={{
          type: "auto-animate",
          mode: "ping-pong",
          outputMin: 0.25,
          outputMax: 0.6,
          speed: 0.5
        }}
        softness={0.2}
        visible={false} />
      <ImageTexture
        maskSource="reveal"
        objectFit="cover"
        url="/samples/photo.jpg" />
    </Shader>
  )
}
JavaScript
// WebGPU 를 지원하는 브라우저에서만 그립니다.
// /samples/… 는 이 사이트의 샘플 파일입니다. 내 프로젝트의 이미지·영상 경로로 바꾸세요.
// <canvas id="canvas" style="width:100%;height:320px"></canvas> 가 페이지에 있어야 한다
import { createShader } from 'shaders/js'

const shader = await createShader(document.getElementById("canvas"), {
  components: [
    {
      type: 'Circle',
      id: 'reveal',
      props: {
        radius: {
          type: 'auto-animate',
          mode: 'ping-pong',
          outputMin: 0.25,
          outputMax: 0.6,
          speed: 0.5
        },
        softness: 0.2,
        visible: false,
      },
    },
    {
      type: 'ImageTexture',
      props: {
        maskSource: 'reveal',
        objectFit: 'cover',
        url: '/samples/photo.jpg',
      },
    }
  ]
}, { disableTelemetry: true })

속성(props) · Circle

값을 바꾸면 위 데모와 코드 탭에 바로 반영됩니다. 설명은 라이브러리 레지스트리의 영어 원문(MIT)입니다.

이름값기본값범위·선택지설명(원문)
originOrigin코드에서 지정center—Reference edge the center position is measured from (center default). Lets you pin the shape relative to a corner or the canvas centre.
colorColor#ffffff—The color of the circle
radiusRadius데모에서 자동으로 오가는 값 — 움직이면 그 값에 고정됩니다10 – 2 (step 0.01)The radius of the circle. A value of one (1) is sets the circle to fit the canvas.
softnessSoftness00 – 1 (step 0.01)Edge softness. Lower values like zero (0) are sharp, higher values like one (1) are softer.
centerCenter Position
x0.5y0.5
{"x":0.5,"y":0.5}x, y: 0 – 1The center point of the circle
strokeThicknessStroke Thickness00 – 0.5 (step 0.01)The thickness of the stroke outline. Zero (0) means no stroke.
strokeColorStroke Color#000000—The color of the stroke outline
strokePositionStroke Positioncenteroutside · center · insidePosition of the stroke relative to the circle edge
colorSpaceColor Blendinglinearlinear · oklch · oklab · hsl · hsv · lchColor space for blending fill and stroke colors in soft edges

모든 컴포넌트 공통: blendMode · opacity · visible · maskSource/maskType · boundingBox · id

마스크는 스텐실이나 종이 오리기 틀처럼, 이미지의 어느 부분을 보여 줄지 정합니다. 원형 마스크를 쓰면 사각형 이미지는 그대로 둔 채 원 안에서만 보이게 할 수 있습니다.

마스크 레이어에 `id`를 주고 `visible: false`로 숨긴 다음, 대상 레이어의 `maskSource`에 그 id를 연결합니다. `maskType`은 `alpha`, `alphaInverted`, `luminance`, `luminanceInverted` 중에서 투명도 또는 밝기를 기준으로 할지 정합니다.

숨긴 마스크 레이어도 참조할 수 있으므로 별도 화면에 드러나지 않습니다. 투명한 부분을 기준으로 자를지 색 밝기로 자를지 먼저 고르고, 마스크 id가 중복되거나 대상과 연결되지 않으면 기대한 모양이 나오지 않습니다.

언제 쓰나

사진을 원형·별 모양으로 잘라 쓸 때, 그라디언트를 글자나 도형 안에 드러낼 때, 애니메이션으로 서서히 나타나는 영역을 만들 때 적합합니다. 단순한 사각형 자르기나 전체 투명도 조절만 필요하면 마스크 대신 기본 레이아웃·`opacity`를 쓰세요.

페이지로 열기 ↗