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);
相关推荐
后台开发者Ethan20 分钟前
setState组件更新
前端·javascript·react
谙忆102426 分钟前
大文件上传实战:前端分片、断点续传、秒传与并发控制怎么落地
前端
轻动琴弦1 小时前
VoltTool —— 一站式在线开发者工具平台,69+实用工具免费使用
前端·chrome
就叫飞六吧1 小时前
不同站点iframe嵌套种cookie、localstorege.set() 关系
前端
星栈1 小时前
LiveView 怎么测:从组件测试到端到端,我终于不再只敢手点页面了
前端·前端框架·elixir
蜡台1 小时前
uniapp vue2 转 vue3
前端·javascript·uni-app·vue3·vue2
Web4Browser1 小时前
Headless 浏览器自动化适合哪些任务?巡检、截图和提交动作要分级
运维·前端·自动化
用户059540174461 小时前
把前端内存泄漏排查从2小时压到5分钟,我们把它集成进了CI
前端·css
IMPYLH1 小时前
HTML 的 <dd> 元素
前端·html
WindfallSheng2 小时前
从Vibe Coding到Spec Coding:一套可落地的AI-SDD企业级研发实战工程
javascript·vue.js