|
| 1 | +import { PointLayer } from '@antv/l7'; |
| 2 | +import type { TestCase } from '../../types'; |
| 3 | +import { CaseScene } from '../../utils'; |
| 4 | + |
| 5 | +export const iconfont: TestCase = async (options) => { |
| 6 | + const scene = await CaseScene({ |
| 7 | + ...options, |
| 8 | + mapConfig: { |
| 9 | + center: [120.5, 30.45], |
| 10 | + style: 'dark', |
| 11 | + zoom: 8.1, |
| 12 | + }, |
| 13 | + }); |
| 14 | + |
| 15 | + const config = { |
| 16 | + iconSize: 28, |
| 17 | + iconFill: 'white' as 'black' | 'white' | 'red' | 'blue' | 'green', |
| 18 | + backgroundColor: '#E81A1A', |
| 19 | + backgroundPadding: 2, |
| 20 | + backgroundRadius: 4, |
| 21 | + backgroundShape: 'circle' as 'rect' | 'circle' | 'circle-rect', |
| 22 | + backgroundFill: 'red' as 'red' | 'green' | 'blue' | 'yellow' | 'white', |
| 23 | + stroke: '#F8A3A3', |
| 24 | + strokeWidth: 0, |
| 25 | + }; |
| 26 | + |
| 27 | + const backgroundColorMap = { |
| 28 | + red: '#E81A1A', |
| 29 | + green: '#18C964', |
| 30 | + blue: '#1677FF', |
| 31 | + yellow: '#F5C400', |
| 32 | + white: '#FFFFFF', |
| 33 | + } as const; |
| 34 | + |
| 35 | + const iconColorMap = { |
| 36 | + black: '#000000', |
| 37 | + white: '#FFFFFF', |
| 38 | + red: '#F5222D', |
| 39 | + blue: '#1677FF', |
| 40 | + green: '#18C964', |
| 41 | + } as const; |
| 42 | + |
| 43 | + const fontFamily = 'iconfont'; |
| 44 | + // 从 iconfont.cn 上拷贝的字体文件链接 |
| 45 | + const fontPath = 'https://at.alicdn.com/t/font_2534097_ao9soua2obv.woff2?t=1622021146076'; |
| 46 | + |
| 47 | + // 注册字体 |
| 48 | + const fontLoaded = new Promise<void>((resolve) => { |
| 49 | + scene.once('fontloaded', () => { |
| 50 | + resolve(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + scene.addFontFace(fontFamily, fontPath); |
| 54 | + |
| 55 | + // 注册图标 |
| 56 | + scene.addIconFonts([ |
| 57 | + ['smallRain', ''], |
| 58 | + ['middleRain', ''], |
| 59 | + ['hugeRain', ''], |
| 60 | + ['sun', ''], |
| 61 | + ['cloud', ''], |
| 62 | + ]); |
| 63 | + |
| 64 | + // 数据 |
| 65 | + const originData = [ |
| 66 | + { |
| 67 | + lng: 120.5, |
| 68 | + lat: 30.2, |
| 69 | + iconType: 'sun', |
| 70 | + iconColorKey: 'icon', |
| 71 | + temperature: '22℃', |
| 72 | + weather: '晴', |
| 73 | + }, |
| 74 | + { |
| 75 | + lng: 120.2, |
| 76 | + lat: 30.5, |
| 77 | + iconType: 'cloud', |
| 78 | + iconColorKey: 'icon', |
| 79 | + temperature: '24℃', |
| 80 | + weather: '多云', |
| 81 | + }, |
| 82 | + { |
| 83 | + lng: 120.6, |
| 84 | + lat: 30.8, |
| 85 | + iconType: 'smallRain', |
| 86 | + iconColorKey: 'icon', |
| 87 | + temperature: '21℃', |
| 88 | + weather: '小雨', |
| 89 | + }, |
| 90 | + ]; |
| 91 | + |
| 92 | + const pointParser = { |
| 93 | + parser: { |
| 94 | + type: 'json' as const, |
| 95 | + x: 'lng', |
| 96 | + y: 'lat', |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + const referenceLayer = new PointLayer({ zIndex: 10 }) |
| 101 | + .source(originData, pointParser) |
| 102 | + .shape('circle') |
| 103 | + .size(3) |
| 104 | + .active(false) |
| 105 | + .color('#FF4D4F') |
| 106 | + .style({ opacity: 1 }); |
| 107 | + |
| 108 | + const pointIconFontLayer = new PointLayer({}) |
| 109 | + .source(originData, pointParser) |
| 110 | + .shape('iconType', 'text') // 使用 iconType 字段来指定图标名称 |
| 111 | + .size(config.iconSize) |
| 112 | + .color('iconColorKey', [iconColorMap[config.iconFill]]) |
| 113 | + .style({ |
| 114 | + textAnchor: 'bottom', // 图标中心点对齐到坐标位置 |
| 115 | + fontFamily, |
| 116 | + iconfont: true, // 开启 iconfont 模式 |
| 117 | + backgroundColor: config.backgroundColor, |
| 118 | + backgroundPadding: config.backgroundPadding, |
| 119 | + backgroundRadius: config.backgroundRadius, |
| 120 | + backgroundShape: config.backgroundShape, |
| 121 | + stroke: config.stroke, |
| 122 | + strokeWidth: config.strokeWidth, |
| 123 | + textAllowOverlap: true, |
| 124 | + }); |
| 125 | + |
| 126 | + scene.addLayer(referenceLayer); |
| 127 | + |
| 128 | + await fontLoaded; |
| 129 | + scene.addLayer(pointIconFontLayer); |
| 130 | + |
| 131 | + iconfont.extendGUI = (gui) => { |
| 132 | + const sizeController = gui |
| 133 | + .add(config, 'iconSize', 18, 48, 1) |
| 134 | + .name('图标大小') |
| 135 | + .onChange((value: number) => { |
| 136 | + pointIconFontLayer.size(value); |
| 137 | + scene.render(); |
| 138 | + }); |
| 139 | + |
| 140 | + const iconColorController = gui |
| 141 | + .add(config, 'iconFill', Object.keys(iconColorMap)) |
| 142 | + .name('图标颜色') |
| 143 | + .onChange((value: keyof typeof iconColorMap) => { |
| 144 | + config.iconFill = value; |
| 145 | + pointIconFontLayer.color('iconColorKey', [iconColorMap[value]]); |
| 146 | + scene.render(); |
| 147 | + }); |
| 148 | + |
| 149 | + const paddingController = gui |
| 150 | + .add(config, 'backgroundPadding', 0, 24, 1) |
| 151 | + .name('背景 Padding') |
| 152 | + .onChange((value: number) => { |
| 153 | + pointIconFontLayer.style({ backgroundPadding: value }); |
| 154 | + scene.render(); |
| 155 | + }); |
| 156 | + |
| 157 | + const radiusController = gui |
| 158 | + .add(config, 'backgroundRadius', 0, 24, 1) |
| 159 | + .name('背景圆角') |
| 160 | + .onChange((value: number) => { |
| 161 | + pointIconFontLayer.style({ backgroundRadius: value }); |
| 162 | + scene.render(); |
| 163 | + }); |
| 164 | + |
| 165 | + const shapeController = gui |
| 166 | + .add(config, 'backgroundShape', ['rect', 'circle', 'circle-rect']) |
| 167 | + .name('背景形状') |
| 168 | + .onChange((value: 'rect' | 'circle' | 'circle-rect') => { |
| 169 | + pointIconFontLayer.style({ backgroundShape: value }); |
| 170 | + scene.render(); |
| 171 | + }); |
| 172 | + |
| 173 | + const fillController = gui |
| 174 | + .add(config, 'backgroundFill', Object.keys(backgroundColorMap)) |
| 175 | + .name('背景填充色') |
| 176 | + .onChange((value: keyof typeof backgroundColorMap) => { |
| 177 | + const backgroundColor = backgroundColorMap[value]; |
| 178 | + config.backgroundColor = backgroundColor; |
| 179 | + pointIconFontLayer.style({ backgroundColor }); |
| 180 | + scene.render(); |
| 181 | + }); |
| 182 | + |
| 183 | + const strokeWidthController = gui |
| 184 | + .add(config, 'strokeWidth', 0, 4, 0.5) |
| 185 | + .name('描边宽度') |
| 186 | + .onChange((value: number) => { |
| 187 | + pointIconFontLayer.style({ strokeWidth: value }); |
| 188 | + scene.render(); |
| 189 | + }); |
| 190 | + |
| 191 | + return [ |
| 192 | + sizeController, |
| 193 | + iconColorController, |
| 194 | + paddingController, |
| 195 | + radiusController, |
| 196 | + shapeController, |
| 197 | + fillController, |
| 198 | + strokeWidthController, |
| 199 | + ]; |
| 200 | + }; |
| 201 | + |
| 202 | + return scene; |
| 203 | +}; |
0 commit comments