Add example for justify text
This commit is contained in:
14
examples/vector-labels-justify-text.html
Normal file
14
examples/vector-labels-justify-text.html
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
layout: example.html
|
||||
title: Vector Labels - Justify Text
|
||||
shortdesc: Example of text justification within a label.
|
||||
docs: >
|
||||
This example showcases how the text can be justified within the label box.
|
||||
By default, the text is justified according to the `textAlign` option.
|
||||
However, this option justifies the label itself according to the setting.
|
||||
If one wants to change this behavior, it is possible to use the `justify` text style option.
|
||||
For more information, see the discussion:
|
||||
https://github.com/openlayers/openlayers/issues/13481
|
||||
tags: "vector, openstreetmap, label, rich-text"
|
||||
---
|
||||
<div id="map" class="map"></div>
|
||||
121
examples/vector-labels-justify-text.js
Normal file
121
examples/vector-labels-justify-text.js
Normal file
@@ -0,0 +1,121 @@
|
||||
import Collection from '../src/ol/Collection.js';
|
||||
import Feature from '../src/ol/Feature.js';
|
||||
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
||||
import Map from '../src/ol/Map.js';
|
||||
import Point from '../src/ol/geom/Point.js';
|
||||
import View from '../src/ol/View.js';
|
||||
import {
|
||||
Circle as CircleStyle,
|
||||
Fill,
|
||||
Stroke,
|
||||
Style,
|
||||
Text,
|
||||
} from '../src/ol/style.js';
|
||||
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
||||
|
||||
const features = [
|
||||
{
|
||||
geometry: new Point([-8300000, 6095000]),
|
||||
textAlign: 'left',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8150000, 6095000]),
|
||||
textAlign: 'center',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8000000, 6095000]),
|
||||
textAlign: 'right',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8300000, 6025000]),
|
||||
textAlign: 'left',
|
||||
justify: 'center',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8150000, 6025000]),
|
||||
textAlign: 'center',
|
||||
justify: 'center',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8000000, 6025000]),
|
||||
textAlign: 'right',
|
||||
justify: 'center',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8300000, 5955000]),
|
||||
textAlign: 'left',
|
||||
justify: 'left',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8150000, 5955000]),
|
||||
textAlign: 'center',
|
||||
justify: 'left',
|
||||
},
|
||||
{
|
||||
geometry: new Point([-8000000, 5955000]),
|
||||
textAlign: 'right',
|
||||
justify: 'left',
|
||||
},
|
||||
];
|
||||
|
||||
function createStyleFunction({textAlign, justify}) {
|
||||
return new Style({
|
||||
image: new CircleStyle({
|
||||
radius: 10,
|
||||
fill: new Fill({color: 'rgba(255, 0, 0, 0.1)'}),
|
||||
stroke: new Stroke({color: 'red', width: 1}),
|
||||
}),
|
||||
text: new Text({
|
||||
textAlign,
|
||||
justify,
|
||||
text: [
|
||||
'long text to emphasize justify',
|
||||
'',
|
||||
'\n',
|
||||
'',
|
||||
`textAlign: ${textAlign}`,
|
||||
'',
|
||||
'\n',
|
||||
'',
|
||||
`justify: ${justify}`,
|
||||
'',
|
||||
],
|
||||
fill: new Fill({
|
||||
color: [255, 255, 255, 1],
|
||||
}),
|
||||
backgroundFill: new Fill({
|
||||
color: [168, 50, 153, 0.8],
|
||||
}),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
const vectorPoints = new VectorLayer({
|
||||
source: new VectorSource({
|
||||
features: new Collection(
|
||||
features.map((featureOptions) => {
|
||||
const feature = new Feature({
|
||||
geometry: featureOptions.geometry,
|
||||
});
|
||||
feature.setStyle(createStyleFunction(featureOptions));
|
||||
return feature;
|
||||
})
|
||||
),
|
||||
format: new GeoJSON(),
|
||||
}),
|
||||
});
|
||||
|
||||
const map = new Map({
|
||||
layers: [
|
||||
new TileLayer({
|
||||
source: new OSM(),
|
||||
}),
|
||||
vectorPoints,
|
||||
],
|
||||
target: 'map',
|
||||
view: new View({
|
||||
center: [-8150000, 6025000],
|
||||
zoom: 8,
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user