antv-l7/stories/Layers/components/Text.tsx

157 lines
4.1 KiB
TypeScript
Raw Normal View History

import { PointLayer, Scene } from '@antv/l7';
2019-12-26 22:36:50 +08:00
import { GaodeMap, Mapbox } from '@antv/l7-maps';
2020-01-08 17:54:23 +08:00
import * as dat from 'dat.gui';
2020-02-14 12:08:28 +08:00
import * as React from 'react';
2019-12-26 22:36:50 +08:00
// @ts-ignore
2019-11-01 15:36:20 +08:00
import data from '../data/data.json';
2019-12-26 22:36:50 +08:00
export default class TextLayerDemo extends React.Component {
// @ts-ignore
2019-11-01 15:36:20 +08:00
private scene: Scene;
2020-01-08 17:54:23 +08:00
private gui: dat.GUI;
2019-11-01 15:36:20 +08:00
public componentWillUnmount() {
this.scene.destroy();
2020-01-08 17:54:23 +08:00
if (this.gui) {
this.gui.destroy();
}
2019-11-01 15:36:20 +08:00
}
2019-12-26 22:36:50 +08:00
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json',
);
const pointsData = await response.json();
const scene = new Scene({
id: 'map',
2020-04-02 19:53:36 +08:00
map: new GaodeMap({
2019-12-26 22:36:50 +08:00
center: [120.19382669582967, 30.258134],
pitch: 0,
style: 'dark',
zoom: 3,
}),
});
// scene.on('loaded', () => {
const pointLayer = new PointLayer({})
.source(pointsData.list, {
parser: {
type: 'json',
x: 'j',
y: 'w',
},
})
2021-11-13 14:09:40 +08:00
.shape(['s', 'm'], (...args) => args.map((i) => `${i}\n`).join(''))
2020-02-14 15:59:03 +08:00
// .shape('circle')
2020-04-02 19:53:36 +08:00
.size(18)
2020-02-14 12:08:28 +08:00
.filter('t', (t) => {
2020-03-11 14:49:39 +08:00
return t < 5;
2020-02-14 12:08:28 +08:00
})
2020-03-11 14:49:39 +08:00
.color('#f00')
2019-12-26 22:36:50 +08:00
.style({
2020-03-11 14:49:39 +08:00
textAllowOverlap: false,
2019-12-30 18:10:21 +08:00
// fontWeight: 200,
// textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
// textOffset: [0, 0], // 文本相对锚点的偏移量 [水平, 垂直]
// spacing: 2, // 字符间距
// padding: [1, 1], // 文本包围盒 padding [水平,垂直],影响碰撞检测结果,避免相邻文本靠的太近
2020-03-10 10:40:33 +08:00
stroke: '#fff', // 描边颜色
2020-04-02 19:53:36 +08:00
strokeWidth: 1, // 描边宽度
2019-12-30 18:10:21 +08:00
// strokeOpacity: 1.0,
2019-12-26 22:36:50 +08:00
});
2019-11-01 15:36:20 +08:00
scene.addLayer(pointLayer);
this.scene = scene;
2020-01-08 17:54:23 +08:00
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
field: 'w',
2020-03-11 14:49:39 +08:00
strokeWidth: 0,
2020-12-15 01:24:46 +08:00
fontWeight: 500,
2020-03-11 14:49:39 +08:00
stroke: '#fff',
2020-02-14 12:08:28 +08:00
textAllowOverlap: false,
opacity: 1,
2020-03-11 14:49:39 +08:00
size: 8,
color: '#fff',
halo: 0.5,
gamma: 2.0,
2020-01-08 17:54:23 +08:00
};
const rasterFolder = gui.addFolder('文本可视化');
2020-01-08 17:54:23 +08:00
rasterFolder
.add(styleOptions, 'field', ['w', 's', 'l', 'm', 'j', 'h'])
2020-01-08 17:54:23 +08:00
.onChange((anchor: string) => {
pointLayer.shape(anchor, 'text');
2020-01-08 17:54:23 +08:00
scene.render();
});
rasterFolder
2020-02-14 15:59:03 +08:00
.add(styleOptions, 'strokeWidth', 0, 10)
.onChange((strokeWidth: number) => {
2020-03-11 14:49:39 +08:00
pointLayer.style({
strokeWidth,
2020-02-14 15:59:03 +08:00
});
// pointLayer.setData(pointsData.list.slice(0, strokeWidth));
2020-02-14 12:08:28 +08:00
scene.render();
});
2020-03-11 14:49:39 +08:00
rasterFolder.add(styleOptions, 'size', 5, 30).onChange((size: number) => {
pointLayer.size(size);
// pointLayer.setData(pointsData.list.slice(0, strokeWidth));
scene.render();
});
rasterFolder.add(styleOptions, 'gamma', 0, 10).onChange((gamma: number) => {
pointLayer.style({
gamma,
});
// pointLayer.setData(pointsData.list.slice(0, strokeWidth));
scene.render();
});
rasterFolder.add(styleOptions, 'halo', 0, 10).onChange((halo: number) => {
pointLayer.style({
halo,
});
// pointLayer.setData(pointsData.list.slice(0, strokeWidth));
scene.render();
});
2020-02-14 12:08:28 +08:00
rasterFolder
.add(styleOptions, 'textAllowOverlap', 0, 10)
.onChange((textAllowOverlap: boolean) => {
pointLayer.style({
2020-02-14 12:08:28 +08:00
textAllowOverlap,
});
scene.render();
});
rasterFolder
.add(styleOptions, 'opacity', 0, 1)
.onChange((opacity: number) => {
pointLayer.style({
opacity,
});
scene.render();
2020-02-14 12:08:28 +08:00
setTimeout(() => {
scene.render();
}, 10);
});
2020-02-14 12:08:28 +08:00
rasterFolder.addColor(styleOptions, 'color').onChange((color: string) => {
pointLayer.color(color);
scene.render();
});
2019-12-26 22:36:50 +08:00
// });
2019-11-01 15:36:20 +08:00
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}