图层效果简介

尝试一下在线预览

此示例演示如何将 bloom 效果应用于 FeatureLayer。用户还可以通过更改相应滑块上的值来调整 bloom 参数。光晕效果使图层中的要素具有发光效果。

从 4.18 开始,您可以将效果应用于 2D MapView 中的所有图层。此强大功能允许您将类似 css 过滤器的功能应用于图层,以提高地图的制图质量。可以通过两种不同的方式设置效果。它可以设置为字符串或对象数组。

将效果设置为字符串

效果可以链接在一起,由空格字符分隔。效果按其设置的顺序应用。设置为字符串时,效果将应用于所有比例。此示例演示如何将效果设置为字符串。

将效果设置为对象数组

某些效果,例如 bloomdrop-shadow 对比例敏感。应使用与比例相关的效果来微调或控制不同比例下效果的参数,以便产生所需的效果。可以从比例相关效果设置为对象数组,您可以在其中指定比例和该比例的效果值。设置与比例相关的效果时,API 将在比例之间插值效果。例如,如果在一个比例上设置 opacity(0%),在另一个比例上设置 opacity(100%),API 将在两个比例之间插入不透明度值。效果的类型和顺序应在所有尺度上保持一致,以便可以插值。如果类型和顺序不一致,效果将设置为 null,并在控制台中显示警告。请参阅将投影效果应用于 layerview示例如何设置与比例相关的效果。

工作原理

此地图显示了活火山的位置。加载 web 地图时,bloom 效果将应用于火山图层,并具有以下参数。

 
1
layer.effect = "bloom(1.5, 0.5px, 0.1)";

用户可以选中或取消选中 Apply bloom effect to volcanoes 复选框以将 bloom 效果应用或从图层中移除效果。应用效果后,用户可以通过更改滑块的值来更改 bloom 效果参数。这样可以很好地了解光晕效果参数的工作原理。

                 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// this function is called when user clicks the applyBloom check box
// or when the slider values are changed.
// it will apply the bloom effect and adjust its parameters or
// it will remove the bloom effect when the user unchecks applyBloom checkbox
function updateEffects() {
  // set the layer effect to null if the user unchecked the applyBloom checkbox
  if (!chkApplyBloom.checked) {
    layer.effect = null;
    return;
  }
  // apply the effect and adjust its parameters if the user checked the applyBloom
  // check box or updated slider values.
  const intensity = intensitySlider.values[0];
  const blur = blurSlider.values[0];
  const threshold = thresholdSlider.values[0];
  layer.effect = `bloom(${intensity}, ${blur}px, ${threshold})`;
}

请查看下表,了解 bloom 参数的说明。

参数说明
强度绽放效果的强度。此值可以是百分比或数字。默认值为 1。值越高,光越亮。不允许使用负值。
半径确定绝对长度的模糊半径。默认值为 0。不允许使用负值。使半径内的要素保持不变。
阈值确定颜色在绽放或发光之前必须有多亮。可接受的值为 0%-100% 或 0-1。默认值为 0。

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.