当前位置:   article > 正文

openlayers4中闪烁点的实现_openlayers如何让点上的icon动起来

openlayers如何让点上的icon动起来

概述:

本文讲述如何在Openlayers4中实现闪烁点。


效果:



代码:

1、flash-marker.js闪烁点扩展

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.FlashMarker = factory());
  5. }(this, (function () { 'use strict';
  6. /**
  7. * @author lzugis
  8. * @Date 2017-09-29
  9. * */
  10. function CanvasLayer(options) {
  11. this.options = options || {};
  12. this.paneName = this.options.paneName || 'labelPane';
  13. this.zIndex = this.options.zIndex || 0;
  14. this._map = options.map;
  15. this._lastDrawTime = null;
  16. this.show();
  17. }
  18. CanvasLayer.prototype.initialize = function () {
  19. var map = this._map;
  20. var canvas = this.canvas = document.createElement('canvas');
  21. var ctx = this.ctx = this.canvas.getContext('2d');
  22. canvas.style.cssText = 'position:absolute;' + 'left:0;' + 'top:0;' + 'z-index:' + this.zIndex + ';';
  23. this.adjustSize();
  24. this.adjustRatio(ctx);
  25. map.getViewport().appendChild(canvas);
  26. var that = this;
  27. map.getView().on('propertychange',function(){
  28. $(canvas).hide();
  29. });
  30. map.on("moveend",function(){
  31. $(canvas).show();
  32. that.adjustSize();
  33. that._draw();
  34. });
  35. };
  36. CanvasLayer.prototype.adjustSize = function () {
  37. var size = this._map.getSize();
  38. var canvas = this.canvas;
  39. canvas.width = size[0];
  40. canvas.height = size[1];
  41. canvas.style.width = canvas.width + 'px';
  42. canvas.style.height = canvas.height + 'px';
  43. };
  44. CanvasLayer.prototype.adjustRatio = function (ctx) {
  45. var backingStore = ctx.backingStorePixelRatio || ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  46. var pixelRatio = (window.devicePixelRatio || 1) / backingStore;
  47. var canvasWidth = ctx.canvas.width;
  48. var canvasHeight = ctx.canvas.height;
  49. ctx.canvas.width = canvasWidth * pixelRatio;
  50. ctx.canvas.height = canvasHeight * pixelRatio;
  51. ctx.canvas.style.width = canvasWidth + 'px';
  52. ctx.canvas.style.height = canvasHeight + 'px';
  53. ctx.scale(pixelRatio, pixelRatio);
  54. };
  55. CanvasLayer.prototype.draw = function () {
  56. var self = this;
  57. var args = arguments;
  58. clearTimeout(self.timeoutID);
  59. self.timeoutID = setTimeout(function () {
  60. self._draw();
  61. }, 15);
  62. };
  63. CanvasLayer.prototype._draw = function () {
  64. var map = this._map;
  65. var size = map.getSize();
  66. var center = map.getView().getCenter();
  67. if (center) {
  68. var pixel = map.getPixelFromCoordinate(center);
  69. this.canvas.style.left = pixel[0] - size[0] / 2 + 'px';
  70. this.canvas.style.top = pixel[1] - size[1] / 2 + 'px';
  71. this.options.update && this.options.update.call(this);
  72. }
  73. };
  74. CanvasLayer.prototype.getContainer = function () {
  75. return this.canvas;
  76. };
  77. CanvasLayer.prototype.show = function () {
  78. this.initialize();
  79. this.canvas.style.display = 'block';
  80. };
  81. CanvasLayer.prototype.hide = function () {
  82. this.canvas.style.display = 'none';
  83. };
  84. CanvasLayer.prototype.setZIndex = function (zIndex) {
  85. this.canvas.style.zIndex = zIndex;
  86. };
  87. CanvasLayer.prototype.getZIndex = function () {
  88. return this.zIndex;
  89. };
  90. var global = typeof window === 'undefined' ? {} : window;
  91. var requestAnimationFrame = global.requestAnimationFrame || global.mozRequestAnimationFrame || global.webkitRequestAnimationFrame || global.msRequestAnimationFrame || function (callback) {
  92. return global.setTimeout(callback, 1000 / 60);
  93. };
  94. function Marker(opts) {
  95. this.city = opts.name;
  96. this.location = [opts.lnglat[0], opts.lnglat[1]];
  97. this.color = opts.color;
  98. this.type = opts.type || 'circle';
  99. this.speed = opts.speed || 0.15;
  100. this.size = 0;
  101. this.max = opts.max || 20;
  102. }
  103. Marker.prototype.draw = function (context) {
  104. context.save();
  105. context.beginPath();
  106. switch (this.type) {
  107. case 'circle':
  108. this._drawCircle(context);
  109. break;
  110. case 'ellipse':
  111. this._drawEllipse(context);
  112. break;
  113. default:
  114. break;
  115. }
  116. context.closePath();
  117. context.restore();
  118. this.size += this.speed;
  119. if (this.size > this.max) {
  120. this.size = 0;
  121. }
  122. };
  123. Marker.prototype._drawCircle = function (context) {
  124. var pixel = this.pixel||map.getPixelFromCoordinate(this.location);
  125. context.strokeStyle = this.color;
  126. context.moveTo(pixel[0] + pixel.size, pixel[1]);
  127. context.arc(pixel[0], pixel[1], this.size, 0, Math.PI * 2);
  128. context.stroke();
  129. };
  130. Marker.prototype._drawEllipse = function (context) {
  131. var pixel = this.pixel || map.getPixelFromCoordinate(this.location);
  132. var x = pixel[0],
  133. y = pixel[1],
  134. w = this.size,
  135. h = this.size / 2,
  136. kappa = 0.5522848,
  137. // control point offset horizontal
  138. ox = w / 2 * kappa,
  139. // control point offset vertical
  140. oy = h / 2 * kappa,
  141. // x-start
  142. xs = x - w / 2,
  143. // y-start
  144. ys = y - h / 2,
  145. // x-end
  146. xe = x + w / 2,
  147. // y-end
  148. ye = y + h / 2;
  149. context.strokeStyle = this.color;
  150. context.moveTo(xs, y);
  151. context.bezierCurveTo(xs, y - oy, x - ox, ys, x, ys);
  152. context.bezierCurveTo(x + ox, ys, xe, y - oy, xe, y);
  153. context.bezierCurveTo(xe, y + oy, x + ox, ye, x, ye);
  154. context.bezierCurveTo(x - ox, ye, xs, y + oy, xs, y);
  155. context.stroke();
  156. };
  157. function FlashMarker(map, dataSet) {
  158. var animationLayer = null,
  159. width = map.getSize()[0],
  160. height = map.getSize()[1],
  161. animationFlag = true,
  162. markers = [];
  163. var addMarker = function addMarker() {
  164. if (markers.length > 0) return;
  165. markers = [];
  166. for (var i = 0; i < dataSet.length; i++) {
  167. markers.push(new Marker(dataSet[i]));
  168. }
  169. };
  170. //上层canvas渲染,动画效果
  171. var render = function render() {
  172. var animationCtx = animationLayer.canvas.getContext('2d');
  173. if (!animationCtx) {
  174. return;
  175. }
  176. if (!animationFlag) {
  177. animationCtx.clearRect(0, 0, width, height);
  178. return;
  179. }
  180. addMarker();
  181. animationCtx.fillStyle = 'rgba(0,0,0,.95)';
  182. var prev = animationCtx.globalCompositeOperation;
  183. animationCtx.globalCompositeOperation = 'destination-in';
  184. animationCtx.fillRect(0, 0, width, height);
  185. animationCtx.globalCompositeOperation = prev;
  186. for (var i = 0; i < markers.length; i++) {
  187. var marker = markers[i];
  188. marker.draw(animationCtx);
  189. }
  190. };
  191. //初始化
  192. var init = function init() {
  193. animationLayer = new CanvasLayer({
  194. map: map,
  195. update: render
  196. });
  197. (function drawFrame() {
  198. requestAnimationFrame(drawFrame);
  199. render();
  200. })();
  201. };
  202. init();
  203. }
  204. return FlashMarker;
  205. })));

2、调用展示

a、数据格式

数据个数如下:

{
    name: '北京',
    lnglat: ['116.3', '39.9'],
    color: '#5070FF',
    type: 'circle',
    speed: 0.2,
}
b、调用方法

new FlashMarker(map, citys);

----------------------------------------------------------------------------------------------

技术博客

CSDN:http://blog.csdn.NET/gisshixisheng

博客园:http://www.cnblogs.com/lzugis/

在线教程

http://edu.csdn.Net/course/detail/799

Github

https://github.com/lzugis/

联系方式

q       q:1004740957

e-mail:niujp08@qq.com

公众号:lzugis15

Q Q 群:452117357(webgis)

             337469080(Android)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/607371
推荐阅读
相关标签
  

闽ICP备14008679号