【问题标题】:Openlayers: Gradient glow on LineStringOpenlayers:LineString 上的渐变发光
【发布时间】:2020-02-03 16:39:46
【问题描述】:

我有一个渲染 LineStrings 的层,并试图对线条应用发光效果。我创建的样式使用自定义渲染器来创建垂直于每个线段的渐变笔触:

const glow_style = new Style({
  renderer: (_coords, state) => {
    const ctx = state.context;
    const coords = _coords as Coordinate[];
    ctx.lineWidth = 25;
    for (let i = 1; i < coords.length; i++) {
      const start = coords[i - 1];
      const end = coords[i];
      const [grd_start, grd_end] = getPerpendicularPoints(start, end, ctx.lineWidth);
      const grd = ctx.createLinearGradient(grd_start[0], grd_start[1], grd_end[0], grd_end[1]);
      grd.addColorStop(0, '#ffffff00');
      grd.addColorStop(0.5, 'white');
      grd.addColorStop(1, '#ffffff00');
      ctx.strokeStyle = grd;
      ctx.beginPath();
      ctx.moveTo(start[0], start[1]);
      ctx.lineTo(end[0], end[1]);
      ctx.stroke();
    }
  }
});

这种样式适用于完全直线,但在拐角处分解,因为渐变不能很好地连接线段之间。如果ctx.lineCap 保留为butt,则渐变在拐角处不连续。如果设置为round,则线段会接触,但由于重叠,渐变会变得不连续。以下是每个示例:

我有哪些选项可以沿整个 LineString 创建平滑渐变?

【问题讨论】:

  • 需要将垂直点调整为角度内外的真正交点,如stackoverflow.com/questions/57421223/…
  • 我看不出调整垂直点会有什么帮助。任何调整都不会破坏渐变与实线的对齐吗?
  • 该代码用于平行线。我现在可以看到它不适合带有渐变的单行。

标签: typescript openlayers transparency


【解决方案1】:

通过绘制宽度减小的线条来渲染整个线串而不将其分成段会更简单。对于平滑渐变,每条线的不透明度应定义为剩余透明度的分数。它甚至可以作为 OpenLayers 样式数组来完成:

var steps = 13;
var styles = [];
for (var i = 0; i < steps; i++) {
    styles.push(
        new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: [255, 255, 255, 1/(steps - i)],
                width: (steps-i)*2 - 1
            })
        })
    );
}

【讨论】:

  • 你又做了一次,迈克。感谢您提供这种替代视角。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多