VectorStyle.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import LTBaseObject from './KBaseObject'
  2. import XYZ from 'ol/source/XYZ'
  3. import Tile from 'ol/layer/Tile'
  4. import Common from './Common'
  5. import Style from "ol/style/Style";
  6. import Fill from "ol/style/Fill";
  7. import Stroke from "ol/style/Stroke";
  8. import Text from "ol/style/Text";
  9. import Icon from "ol/style/Icon";
  10. import Circle from "ol/style/Circle";
  11. /**
  12. * @description 样式类
  13. */
  14. class VectorStyle{
  15. constructor(){
  16. this.cachePointSimpleStyle = {}
  17. }
  18. getLineStyle(fillColor, strokeColor, strokeWidth){
  19. let style = new Style({
  20. stroke: new Stroke({
  21. color: strokeColor,
  22. width: strokeWidth || 1,
  23. }),
  24. });
  25. return style;
  26. }
  27. getPolygonStyle(fillColor, strokeColor, strokeWidth){
  28. let style = new Style({
  29. fill: new Fill({
  30. color: fillColor
  31. }),
  32. stroke: new Stroke({
  33. color: strokeColor,
  34. width: strokeWidth || 1,
  35. }),
  36. });
  37. return style;
  38. }
  39. getPointStyle(src, scale, anchor, text, font, textColor){
  40. let textObj = null;
  41. text && (textObj = new Text({
  42. text:text,
  43. stroke: new Stroke({
  44. color: textColor,
  45. width: 1,
  46. }),
  47. fill: new Fill({
  48. color: textColor,
  49. }),
  50. font:font
  51. }))
  52. let style = new Style({
  53. image: new Icon({
  54. text:textObj,
  55. src:src,
  56. scale:scale,
  57. anchor:anchor,
  58. })
  59. });
  60. return style
  61. }
  62. /**
  63. *
  64. * @param radius 半径
  65. * @param fillColor 填充颜色
  66. * @param strokeColor 边框颜色
  67. * @param strokeWidth 边框宽度
  68. * @returns {Style}
  69. */
  70. getPointSimpleStyle(radius, fillColor, strokeColor, strokeWidth){
  71. let key = radius + fillColor + strokeColor + strokeWidth
  72. if(this.cachePointSimpleStyle[key]){
  73. return this.cachePointSimpleStyle[key]
  74. }
  75. let style = new Style({
  76. image: new Circle({
  77. radius: radius, // 半径
  78. stroke: new Stroke({ // 边界样式
  79. color: strokeColor, // 边界颜色
  80. width: strokeWidth // 边界宽度
  81. }),
  82. fill: new Fill({ // 填充样式
  83. color: fillColor // 填充颜色
  84. })
  85. })
  86. });
  87. this.cachePointSimpleStyle[key] = style
  88. return style
  89. }
  90. /**
  91. *
  92. * @param text
  93. * @param fillColor 填充颜色
  94. * @param strokeColor 边框颜色
  95. * @param strokeWidth 边框宽度
  96. * @returns {Style}
  97. */
  98. getPointTextStyle(text, fillColor, strokeColor, strokeWidth, fontSize){
  99. let style = new Style({
  100. text: new Text({
  101. text:text,
  102. stroke: new Stroke({
  103. color: strokeColor,
  104. width: strokeWidth,
  105. }),
  106. fill: new Fill({
  107. color: fillColor,
  108. }),
  109. font: fontSize+"px sans-serif"
  110. }),
  111. });
  112. return style
  113. }
  114. }
  115. export default VectorStyle