修改网上流传的flash-marker.js

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.FlashMarker = factory());
}(this, (function () { 'use strict';
var map = null;
var canvas = null;
/**
* @author lzugis
* @Date 2017-09-29
* */
function CanvasLayer(options) {
this.options = options || {};
this.paneName = this.options.paneName || 'labelPane';
this.zIndex = this.options.zIndex || 0;
this._map = options.map;
map = this._map;
this._lastDrawTime = null;
this.show();
} CanvasLayer.prototype.initialize = function () {
var map = this._map;
canvas = this.canvas = document.createElement('canvas');
var ctx = this.ctx = this.canvas.getContext('2d');
canvas.style.cssText = 'position:absolute;' + 'left:0;' + 'top:0;' + 'z-index:' + this.zIndex + ';';
this.adjustSize();
this.adjustRatio(ctx);
map.getViewport().appendChild(canvas);
var that = this;
map.getView().on('propertychange',function(){
// $(canvas).hide();
// canvas.style.display="none";
});
// map.on("moveend",function(){
// // $(canvas).show();
// // canvas.style.display="block";
// that.adjustSize();
// that._draw();
// });
}; CanvasLayer.prototype.adjustSize = function () {
var size = this._map.getSize();
// var canvas = this.canvas;
canvas.width = size[0];
canvas.height = size[1];
canvas.style.width = canvas.width + 'px';
canvas.style.height = canvas.height + 'px';
}; CanvasLayer.prototype.adjustRatio = function (ctx) {
var backingStore = ctx.backingStorePixelRatio || ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
var pixelRatio = (window.devicePixelRatio || 1) / backingStore;
var canvasWidth = ctx.canvas.width;
var canvasHeight = ctx.canvas.height;
ctx.canvas.width = canvasWidth * pixelRatio;
ctx.canvas.height = canvasHeight * pixelRatio;
ctx.canvas.style.width = canvasWidth + 'px';
ctx.canvas.style.height = canvasHeight + 'px';
ctx.scale(pixelRatio, pixelRatio);
}; CanvasLayer.prototype.draw = function () {
var self = this;
var args = arguments; clearTimeout(self.timeoutID);
self.timeoutID = setTimeout(function () {
self._draw();
}, 15);
}; CanvasLayer.prototype._draw = function () {
var map = this._map;
var size = map.getSize();
var center = map.getView().getCenter();
if (center) {
var pixel = map.getPixelFromCoordinate(center);
this.canvas.style.left = pixel[0] - size[0] / 2 + 'px';
this.canvas.style.top = pixel[1] - size[1] / 2 + 'px';
this.options.update && this.options.update.call(this);
}
}; CanvasLayer.prototype.getContainer = function () {
return this.canvas;
}; CanvasLayer.prototype.show = function () {
this.initialize();
this.canvas.style.display = 'block';
}; CanvasLayer.prototype.hide = function () {
this.canvas.style.display = 'none';
}; CanvasLayer.prototype.setZIndex = function (zIndex) {
this.canvas.style.zIndex = zIndex;
}; CanvasLayer.prototype.getZIndex = function () {
return this.zIndex;
}; var global = typeof window === 'undefined' ? {} : window; var requestAnimationFrame = global.requestAnimationFrame || global.mozRequestAnimationFrame || global.webkitRequestAnimationFrame || global.msRequestAnimationFrame || function (callback) {
return global.setTimeout(callback, 1000 / 60);
}; function Marker(opts) {
this.city = opts.name;
this.location = [opts.lnglat[0], opts.lnglat[1]];
this.color = opts.color;
this.type = opts.type || 'circle';
this.speed = opts.speed || 0.15;
this.size = 0;
this.max = opts.max || 20;
} Marker.prototype.draw = function (context) {
context.save();
context.beginPath();
switch (this.type) {
case 'circle':
this._drawCircle(context);
break;
case 'ellipse':
this._drawEllipse(context);
break;
default:
break;
}
context.closePath();
context.restore(); this.size += this.speed;
if (this.size > this.max) {
this.size = 0;
}
}; Marker.prototype._drawCircle = function (context) {
var pixel = this.pixel||map.getPixelFromCoordinate(this.location);
context.strokeStyle = this.color;
context.moveTo(pixel[0] + pixel.size, pixel[1]);
context.arc(pixel[0], pixel[1], this.size, 0, Math.PI * 2);
context.stroke();
}; Marker.prototype._drawEllipse = function (context) {
var pixel = this.pixel || map.getPixelFromCoordinate(this.location);
var x = pixel[0],
y = pixel[1],
w = this.size,
h = this.size / 2,
kappa = 0.5522848, // control point offset horizontal
ox = w / 2 * kappa, // control point offset vertical
oy = h / 2 * kappa, // x-start
xs = x - w / 2, // y-start
ys = y - h / 2, // x-end
xe = x + w / 2, // y-end
ye = y + h / 2; context.strokeStyle = this.color;
context.moveTo(xs, y);
context.bezierCurveTo(xs, y - oy, x - ox, ys, x, ys);
context.bezierCurveTo(x + ox, ys, xe, y - oy, xe, y);
context.bezierCurveTo(xe, y + oy, x + ox, ye, x, ye);
context.bezierCurveTo(x - ox, ye, xs, y + oy, xs, y);
context.stroke();
}; function FlashMarker(map, dataSet) {
this.timer = null;
var that = this;
var animationLayer = null,
width = map.getSize()[0],
height = map.getSize()[1],
animationFlag = true,
markers = [];
that.width = width;
that.height = height;
this.close(); var addMarker = function addMarker() {
if (markers.length > 0) return;
markers = [];
for (var i = 0; i < dataSet.length; i++) {
markers.push(new Marker(dataSet[i]));
}
}; //上层canvas渲染,动画效果
var render = function render() {
var animationCtx = animationLayer.canvas.getContext('2d');
that.animationCtx = animationCtx;
if (!animationCtx) {
return;
} if (!animationFlag) {
animationCtx.clearRect(0, 0, width, height);
return;
} addMarker(); animationCtx.fillStyle = 'rgba(0,0,0,.95)';
var prev = animationCtx.globalCompositeOperation;
animationCtx.globalCompositeOperation = 'destination-in';
animationCtx.fillRect(0, 0, width, height);
animationCtx.globalCompositeOperation = prev; for (var i = 0; i < markers.length; i++) {
var marker = markers[i];
marker.draw(animationCtx);
}
};
//初始化
var init = function init() {
animationLayer = new CanvasLayer({
map: map,
update: render
}); (function drawFrame() {
that.timer = requestAnimationFrame(drawFrame);
render();
})();
}; init();
}
FlashMarker.prototype.close = function() {
cancelAnimationFrame(this.timer);
if(this.animationCtx){
this.animationCtx.clearRect(0, 0, this.width, this.height);
}
}
return FlashMarker; })));

调用代码

  //数据
let lnglat=[];//坐标值[x,y]
let citys = [{
name: '',
lnglat: lnglat,
color: '#5070FF',
type: 'circle',
speed: 0.5,
}];
if(this.mark){
this.mark.close();
}
this.mark = new window.FlashMarker(map, citys);

最新文章

  1. NAT123内网映射端口
  2. 如何调试PHP程序
  3. 【转载】ANSYS的APDL与C语言混合编程(实例)
  4. disabled和readonly的区别?
  5. TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
  6. WIN7启动WIFI
  7. 复利计算- 结对2.0--复利计算WEB升级版
  8. C函数之memcpy()函数用法
  9. DOM对象和JQuery对象的区别
  10. UVA 10194 Football (aka Soccer)
  11. Data Guard 之 浅析Switchover与Failover
  12. 设置EntityFramework 在开发时自动更新数据库
  13. VR全景智慧城市—你的掌上步行街
  14. linux 与 windows 挖门罗币总结
  15. 《Python黑帽子:黑客与渗透测试编程之道》 玩转浏览器
  16. java 内存分析之方法返回值二
  17. FastJson中文乱码
  18. Gitlab 社区版安装部署和维护指南
  19. mybatis之typehandles
  20. ZOJ 3707 Calculate Prime S 数论

热门文章

  1. spark_load csv to hive via hivecontext
  2. asp.net mvc里面竟然也可以用eval()
  3. c/c++技巧总结
  4. 初学makefile
  5. 【Unity3D】简要分析unity3d中剪不断理还乱的yield
  6. SpringMVC06Exception 异常处理
  7. LitePal——安卓数据库library
  8. VMware ESXi5忘记登录密码解决办法
  9. 添加egit插件
  10. 《敏捷软件开发:原则、模式与实践(C#版)》源代码下载