Jinuss's blog Jinuss's blog
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

前端可视化
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 核心基类

  • Control控件篇

  • Geom几何图形篇

  • Layer图层篇

  • Renderer篇

  • Feature篇

  • style样式篇

    • Style类
    • Fill类
    • Stroke类
    • Text类
      • 概述
      • 源码分析
        • Text类源码实现
        • Text类的构造函数
        • Text类的主要方法
      • 总结
    • ImageStyle类
    • Icon类
    • RegularShape类
    • Circle类
    • IconImage类
    • IconImageCache类
  • 《Openlayers源码》笔记
  • style样式篇
东流
2025-01-20
目录

Text类

# 概述

Text 类是 Openlayers 中用于配置和绘制文本标注的类。它提供了多种选项来控制文本的外观、位置、旋转、缩放、填充、背景、边框等属性。该类通常用于地图上的标注对象,允许开发者自定义文本样式。

# 源码分析

# Text类源码实现

Text类源码实现如下:

class Text {
  constructor(options) {
    options = options || {};
    this.font_ = options.font;
    this.rotation_ = options.rotation;
    this.rotateWidthView_ = options.rotateWithView;
    this.scale_ = options.scale;
    this.scaleArray_ = toSize(options.scale !== undefined ? options.scale : 1);
    this.text_ = options.text;
    this.textAlign_ = options.textAlign;
    this.justify_ = options.justify;
    this.repeat_ = options.repeat;
    this.textBaseline_ = options.textBaseline;
    this.fill_ =
      options.fill !== undefined
        ? options.fill
        : new Fill({ color: DEFAULT_FILL_COLOR });
    this.maxAngle_ =
      options.maxAngle !== undefined ? options.maxAngle : Math.PI / 4;

    this.placement_ =
      options.placement !== undefined ? options.placement : "point";

    this.overflow_ = !!options.overflow;
    this.stroke_ = options.stroke !== undefined ? options.stroke : null;
    this.offsetX_ = options.offsetX !== undefined ? options.offsetX : 0;

    this.offsetY_ = options.offsetY !== undefined ? options.offsetY : 0;

    this.backgroundFill_ = options.backgroundFill
      ? options.backgroundFill
      : null;

    this.backgroundStroke_ = options.backgroundStroke
      ? options.backgroundStroke
      : null;

    this.padding_ = options.padding === undefined ? null : options.padding;

    this.declutterMode_ = options.declutterMode;
  }

  clone() {
    const scale = this.getScale();
    return new Text({
      font: this.getFont(),
      placement: this.getPlacement(),
      repeat: this.getRepeat(),
      maxAngle: this.getMaxAngle(),
      overflow: this.getOverflow(),
      rotation: this.getRotation(),
      rotateWithView: this.getRotateWithView(),
      scale: Array.isArray(scale) ? scale.slice() : scale,
      text: this.getText(),
      textAlign: this.getTextAlign(),
      justify: this.getJustify(),
      textBaseline: this.getTextBaseline(),
      fill: this.getFill() ? this.getFill().clone() : undefined,
      stroke: this.getStroke() ? this.getStroke().clone() : undefined,
      offsetX: this.getOffsetX(),
      offsetY: this.getOffsetY(),
      backgroundFill: this.getBackgroundFill()
        ? this.getBackgroundFill().clone()
        : undefined,
      backgroundStroke: this.getBackgroundStroke()
        ? this.getBackgroundStroke().clone()
        : undefined,
      padding: this.getPadding() || undefined,
      declutterMode: this.getDeclutterMode(),
    });
  }
  getOverflow() {
    return this.overflow_;
  }
  getFont() {
    return this.font_;
  }
  getMaxAngle() {
    return this.maxAngle_;
  }
  getPlacement() {
    return this.placement_;
  }
  getRepeat() {
    return this.repeat_;
  }
  getOffsetX() {
    return this.offsetX_;
  }
  getOffsetY() {
    return this.offsetY_;
  }
  getFill() {
    return this.fill_;
  }
  getRotateWithView() {
    return this.rotateWithView_;
  }
  getRotation() {
    return this.rotation_;
  }
  getScale() {
    return this.scale_;
  }
  getScaleArray() {
    return this.scaleArray_;
  }
  getStroke() {
    return this.stroke_;
  }
  getText() {
    return this.text_;
  }
  getTextAlign() {
    return this.textAlign_;
  }
  getJustify() {
    return this.justify_;
  }
  getTextBaseline() {
    return this.textBaseline_;
  }
  getBackgroundFill() {
    return this.backgroundFill_;
  }
  getBackgroundStroke() {
    return this.backgroundStroke_;
  }
  getPadding() {
    return this.padding_;
  }
  getDeclutterMode() {
    return this.declutterMode_;
  }
  setOverflow(overflow) {
    this.overflow_ = overflow;
  }
  setFont(font) {
    this.font_ = font;
  }
  setMaxAngle(maxAngle) {
    this.maxAngle_ = maxAngle;
  }
  setOffsetX(offsetX) {
    this.offsetX_ = offsetX;
  }
  setOffsetY(offsetY) {
    this.offsetY_ = offsetY;
  }
  setPlacement(placement) {
    this.placement_ = placement;
  }
  setRepeat(repeat) {
    this.repeat_ = repeat;
  }
  setRotateWithView(rotateWithView) {
    this.rotateWithView_ = rotateWithView;
  }
  setFill(fill) {
    this.fill_ = fill;
  }
  setRotation(rotation) {
    this.rotation_ = rotation;
  }
  setScale(scale) {
    this.scale_ = scale;
    this.scaleArray_ = toSize(scale !== undefined ? scale : 1);
  }
  setStroke(stroke) {
    this.stroke_ = stroke;
  }
  setText(text) {
    this.text_ = text;
  }
  setTextAlign(textAlign) {
    this.textAlign_ = textAlign;
  }
  setJustify(justify) {
    this.justify_ = justify;
  }
  setTextBaseline(textBaseline) {
    this.textBaseline_ = textBaseline;
  }
  setBackgroundFill(fill) {
    this.backgroundFill_ = fill;
  }
  setBackgroundStroke(stroke) {
    this.backgroundStroke_ = stroke;
  }
  setPadding(padding) {
    this.padding_ = padding;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

# Text类的构造函数

Text类构造函数用于初始化 Text 实例的各种属性。options 参数包含多个可配置选项,这些选项控制文本标注的外观和行为。每个选项有其默认值,具体如下:

  • font: 字体样式,指定文本的字体(如字体大小、类型等)。

  • rotation: 文本旋转角度,以弧度为单位。

  • rotateWithView: 是否随着地图视图的旋转而旋转文本。

  • scale: 文本的缩放比例。

  • scaleArray: 转换 scale 为一个数组,提供更细粒度的控制。

  • text: 显示的文本内容。

  • textAlign: 文本的水平对齐方式(例如:left,center, right)。

  • justify: 是否根据文本内容来自动调整对齐。

  • repeat: 文本是否重复。

  • textBaseline: 文本的垂直对齐方式(例如:top, middle, bottom)。

  • fill: 文本的填充颜色,默认为 DEFAULT_FILL_COLOR。

  • maxAngle: 文本最大旋转角度,默认是 Math.PI / 4(45 度)。

  • placement: 文本的放置方式(point 或 line),决定文本是放在点标注上还是沿线条绘制。

  • overflow: 如果为 true,文本会溢出边界,否则会被裁剪。

  • stroke: 文本的边框样式。

  • offsetX 和 offsetY: 文本相对于其位置的偏移量。

  • backgroundFill: 文本背景的填充颜色。

  • backgroundStroke: 文本背景的边框样式。

  • padding: 文本背景的内边距。

  • declutterMode: 是否启用去重模式,避免文本重叠

# Text类的主要方法

  • clone方法:该方法用于创建 Text 类实例的副本。它会复制当前 Text 对象的所有属性,并生成一个新的 Text 实例。这样,可以在不修改原始对象的情况下重复使用相同的文本配置

其余的方法主要分为两类:set**和get**方法,前者用于设置Text对象的属性,后者用于获取对象的属性。

# 总结

本文介绍了Text类,主要是理解Text类的各种属性表示的含义以及主要的方法即可。

编辑 (opens new window)
上次更新: 2025/01/21, 08:26:05
Stroke类
ImageStyle类

← Stroke类 ImageStyle类→

最近更新
01
GeoJSON
05-08
02
Circle
04-15
03
CircleMarker
04-15
更多文章>
Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式