leaflet切片变色、地图滤镜逻辑实现 colorfilter

下载leaflet插件 colorfilter

地址需魔法:Leaflet.TileLayer.ColorFilter

1.x版本的源码放这了

javascript 复制代码
/*!
  Leaflet.TileLayer.ColorFilter
  (c) 2018-2025, Claudio T. Kawakani
  A simple and lightweight Leaflet plugin to apply CSS filters on map tiles.
  https://github.com/xtk93x/Leaflet.TileLayer.ColorFilter
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).L)}(this,function(t){"use strict";const e=t.TileLayer.prototype.onAdd;t.TileLayer.include({_colorFilter:function(){let t=["blur:px","brightness:%","bright:brightness:%","bri:brightness:%","contrast:%","con:contrast:%","grayscale:%","gray:grayscale:%","hue-rotate:deg","hue:hue-rotate:deg","hue-rotation:hue-rotate:deg","invert:%","inv:invert:%","opacity:%","op:opacity:%","saturate:%","saturation:saturate:%","sat:saturate:%","sepia:%","sep:sepia:%"];return(this.options.colorFilter?this.options.colorFilter:[]).map(e=>{let i=e.toLowerCase().split(":");if(2===i.length){let e=t.find(t=>t.split(":")[0]===i[0]);if(e)return e=e.split(":"),i[1]+=/^\d+$/.test(i[1])?e[e.length-1]:"",`${e[e.length-2]}(${i[1]})`}return""}).join(" ")},updateColorFilter:function(t){this.options.colorFilter=t,this._container&&(this._container.style.filter=this._colorFilter())},onAdd:function(t){e.call(this,t),this.options.colorFilter&&(this._container.style.filter=this._colorFilter())}})});

使用

javascript 复制代码
let myFilter = [
    "blur: 0px", //应用以像素为单位的高斯模糊过滤
    "brightness: 100%", //控制平铺图像的亮度
    "contrast: 100%", //更改磁贴的颜色对比度
    "grayscale: 0%", //将图块的颜色更改为灰度
    "hue: 90deg", //在平铺颜色上应用以度为单位的色相旋转
    "opacity: 100%", //定义平铺的不透明度
    "invert: 80%", //反转平铺颜色
    "saturate: 100%", //使平铺颜色饱和
    "sepia: 10%" //将平铺颜色转换为棕褐色
];
const tileOptions = {
    tileSize: 256,
    opacity: 1,
    zIndex: 5,
    minZoom: defaultZoom.minZoom,
    maxZoom: defaultZoom.maxZoom,
    // 重点
    colorFilter: myFilter
};
tileLayer = L.tileLayer(tileUrl, tileOptions).addTo(map);
相关推荐
沙蒿同学24 分钟前
不引 Ghostscript:我用纯 Go 给 PDF 做了一场「压缩手术」
前端·后端·github
Linux_cy29 分钟前
服务器管理工具哪个好用?我用了七八个
运维·服务器·javascript·python·ssh·面板·gmssh
变与不变8061 小时前
jQuery细节以及使用应用场景
前端·javascript·jquery
SailingCoder3 小时前
【封闭结束1】端到端交付:Agent 一次请求如何走完全链路
前端·安全·ai·面试·agent·harness
风骏时光牛马5 小时前
AI多模块协同自动化工作流
前端
zhangminghuan5 小时前
前端面试应对技巧
前端·面试·职场和发展
计算机魔术师5 小时前
潜伏14年的PHP致命洞,AI跑两天就交卷了
前端
IT_陈寒5 小时前
Vue的这个响应式陷阱我居然现在才发现
前端·人工智能·后端
Python自动化直播7 小时前
智能脚本批量管理拆开看:导入、导出、重写、试听
前端·python·ai·秒播