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);
相关推荐
Csvn25 分钟前
Monorepo 迁移血泪史:从 Multi-Repo 到 Turborepo,这 3 个坑我帮你踩完了
前端
星栈42 分钟前
Dioxus 多页面怎么做:`dioxus-router`、嵌套路由、`Outlet` 和页面组织,一篇给你讲顺
前端·rust·前端框架
用户987409238871 小时前
用 Remotion + edge-tts 打造中文教学视频全自动流水线
前端
风骏时光牛马1 小时前
Less前端工程化实战:变量混合器与项目样式分层落地
前端
假如让我当三天老蒯1 小时前
Options API(选项式 API) 和 Composition API(组合式 API)
前端·vue.js·面试
SameX1 小时前
iOS 独立开发实践:用 MapKit + 像素渲染实现 Citywalk 轨迹地图 App「雁过留痕」
前端
_柳青杨1 小时前
一文吃透 Node.js 事件循环:从原理到 Node 20+ 重大变更
javascript·后端
skyey1 小时前
页面加载时,深色模式闪白的问题解决
前端
IT_陈寒1 小时前
Java 并行流把我坑惨了,这6小时加班值了
前端·人工智能·后端
anOnion11 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计