---
title: "MaplibreMaskLayer"
description: "Region mask that dims everything outside a polygon area to highlight it."
canonical_url: "https://maplibre.mhaibaraai.cn/en/docs/layers/mask"
---
# MaplibreMaskLayer

> Region mask that dims everything outside a polygon area to highlight it.

## Introduction

`MaplibreMaskLayer` builds an inverted mask with the whole world as the outer ring and the outer rings of the target area as holes. `data` accepts Polygon, MultiPolygon, Feature, and FeatureCollection. `outline` also draws the area boundary, and data changes are applied incrementally through `setData`.

> [!NOTE]
> 
> Holes inside the target area (such as enclaves) are not masked and stay highlighted with the area.

## Usage

```vue [MaskLayerExample.vue]
<script setup lang="ts">
import type { Polygon } from 'geojson'

withDefaults(defineProps<{ opacity?: number, outline?: boolean }>(), {
  opacity: 0.5,
  outline: true
})

// 示意区域：北京二环附近的多边形
const area: Polygon = {
  type: 'Polygon',
  coordinates: [[
    [116.35, 39.87], [116.44, 39.86], [116.45, 39.9], [116.44, 39.95],
    [116.39, 39.96], [116.35, 39.95], [116.34, 39.91], [116.35, 39.87]
  ]]
}
</script>

<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MaplibreMap :options="{ style: 'https://tiles.openfreemap.org/styles/positron', center: [116.4, 39.91], zoom: 11.5 }">
      <MaplibreMaskLayer :data="area" :opacity="opacity" :outline="outline" outline-color="#3b82f6" :outline-width="2" />
    </MaplibreMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MaplibreMaskLayer component
 */
interface MaplibreMaskLayerProps {
  /**
   * 目标区域：Polygon / MultiPolygon / Feature / FeatureCollection；区域自身的孔洞（飞地）不参与遮罩
   */
  data: GeoJSON.Polygon | GeoJSON.MultiPolygon | GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties> | GeoJSON.FeatureCollection<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>;
  /**
   * 图层 id 前缀；省略时自动生成，变更需配合 `:key` 重建
   */
  layerId?: string | undefined;
  /**
   * 遮罩颜色
   * @default '#000'
   */
  color?: string | undefined;
  /**
   * 遮罩不透明度
   * @default 0.5
   */
  opacity?: number | undefined;
  /**
   * 是否描绘目标区域边界
   * @default false
   */
  outline?: boolean | undefined;
  /**
   * 边界颜色
   * @default '#fff'
   */
  outlineColor?: string | undefined;
  /**
   * 边界宽度（像素）
   * @default 1
   */
  outlineWidth?: number | undefined;
  /**
   * 插入到该图层之前
   */
  beforeId?: string | undefined;
}
```

## Changelog

See commit history for [src/runtime/components/layers/MaskLayer.vue](https://github.com/mhaibaraai/movk-maplibre/commits/main/src/runtime/components/layers/MaskLayer.vue).


## Sitemap

See the full [sitemap](https://maplibre.mhaibaraai.cn/sitemap.md) for all pages.
