基於flash-marker.js 的地圖標註閃爍代碼調試

来源:https://www.cnblogs.com/webgis-mc/archive/2019/03/01/10455731.html
-Advertisement-
Play Games

修改網上流傳的flash-marker.js 調用代碼 ...


修改網上流傳的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);

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • javascript變數 javascript數據類型 javascript運算符 javascript流程語句 javascript數組 javascript字元串函數 javascript函數基礎 javascript基礎DOM操作 javascript windows對象 javascript ...
  • flex對齊 flex對齊方式與主軸和交叉軸所在的方向有關,而flex-direction是控制方向的。 主軸 justify-content justify-content對齊方式共有5種對齊方式: flex-start :主軸起點邊緣開始,從左向右。 flex-end :主軸終點邊緣開始,從右向 ...
  • $broadcast: $broadcast事件是由父組件發起,所有子組件都會收到此廣播事件,除非事件被手動取消。事件廣播的順序為廣度優先搜索順序,如上圖,如果頁面Page_Index發起一個$broadcast事件,那麼按先後順序依次接收到該事件的組件為:ComA、ComB、ComC、ComD、C ...
  • 主要部分: 花括弧,方括弧,冒號和逗號 花括弧保存對象 方括弧裝載數組 名稱和值用冒號隔開 對象通過逗號隔開 一個好用的json格式校驗網址:https://www.json.cn/ ...
  • 近幾年,隨著 Web 開發逐漸成熟,前後端分離的架構設計越來越被眾多開發者認可,使得前端和後端可以專註各自的職能,降低溝通成本,提高開發效率。 在前後端分離的開發模式下,前端和後端工程師得以並行工作。當遇到前端界面展示需要的數據,而後端對應的介面還沒有完成開發的情況時,需要一個數據源來保證前端工作的 ...
  • 在本次Wepack 4教程,我們會聚焦通過壓縮輸出內容,來提升你應用的用戶體驗。這意味著,生產環境需要一套不同的做法。今天,我們將通過mode參數來講述Webpack內置的優化功能。開始吧! ...
  • 1、js支持重載嗎?雖然js 本身並沒有函數重載,但是可以用arguments來模擬重載,函數名相同,參數不同,arguments的length屬性,獲取參數個數,索引屬性獲取參數值 2、什麼是作用域鏈對象?專門保存了函數對象可用變數的位置的對象(棧)都有預設指向window對象地址。3、什麼是閉包 ...
  • Web前端 CSS必備知識點 基本內容,類選擇符,id選擇符,偽類,偽元素,結構,繼承,特殊性,層疊,元素分類,顏色,長度,url,文本,字體,邊框,塊級元素,浮動元素,內聯元素,定位。 鏈接: link標簽: 樣式: 指示引入多個外部樣式表的鏈接 註解 內聯樣式: 規範: 選擇符 + 聲明 偽類和 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...