我嘗試使用
uj5u.com熱心網友回復:
顯示變數w,h您將看到回傳了哪些小變數。為了增加它們,我為此增加了它們,* 5我使用了 fitBounds,它現在應該縮放到查看器視窗,同時可以縮放。
為了能夠也多次點擊變焦多,我換map.getMaxZoom () - 1到map.getMaxZoom () - 2
var map = L.map("image-map", {
minZoom: 1, // tried 0.1,-4,...
maxZoom: 4,
center: [0, 0],
zoom: 2,
crs: L.CRS.Simple
});
function getMeta(url) {
const img = new Image();
img.addEventListener("load", function () {
var w = this.naturalWidth * 5;
var h = this.naturalHeight * 5;
var southWest = map.unproject([0, h], map.getMaxZoom() - 2);
var northEast = map.unproject([w, 0], map.getMaxZoom() - 2);
var bounds = new L.LatLngBounds(southWest, northEast);
// add the image overlay,
// so that it covers the entire map
L.imageOverlay(img.src, bounds).addTo(map);
// tell leaflet that the map is exactly as big as the image
// map.setMaxBounds(bounds);
map.fitBounds(bounds);
});
img.src = url;
}
getMeta("https://fractalcitta.github.io/assets/images/bhikkhu-patimokkha.svg");
你不需要增加,(w, h) * 5但只需更改為map.getMaxZoom () - 4
您應該始終使用 svg 做的一件更重要的事情是優化這些檔案。我總是使用這個網站 - svgomg
第二個版本寬度異步/承諾----------
let map = L.map("map", {
crs: L.CRS.Simple,
minZoom: 1,
maxZoom: 4,
});
function loadImage(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.addEventListener("load", () => resolve(img));
img.addEventListener("error", reject);
img.src = url;
});
}
async function getImageData(url) {
const img = await loadImage(url);
return { img, width: img.naturalWidth, height: img.naturalHeight };
}
async function showOnMap(url) {
const { img, width, height } = await getImageData(url);
const southWest = map.unproject([0, height], map.getMaxZoom() - 4);
const northEast = map.unproject([width, 0], map.getMaxZoom() - 4);
const bounds = new L.LatLngBounds(southWest, northEast);
L.imageOverlay(img.src, bounds).addTo(map);
map.fitBounds(bounds);
}
showOnMap(
"https://fractalcitta.github.io/assets/images/bhikkhu-patimokkha.svg"
);
問題的第三種方法,我希望是最后一種;)
你需要對這里發生的事情做一些描述。我們通過featch我們注入已隱藏的div獲得SVGwidth/height設定0
然后我們使用getBBox()屬性來獲取從注射SVG精確的尺寸。
我沒有map.unproject在這個例子中使用。要獲得邊界的確切尺寸,滿足以下條件就足夠了:
const bounds = [
[0, 0], // padding
[width, height], // image dimensions
];
以下所有代碼:
<div id="map"></div>
<div id="svg" style="position: absolute; bottom: 0; left: 0; width: 0; height: 0;"></div>
let map = L.map("map", {
crs: L.CRS.Simple,
minZoom: -4,
maxZoom: 1,
});
const url =
"https://fractalcitta.github.io/assets/images/bhikkhu-patimokkha.svg";
async function fetchData(url) {
try {
const response = await fetch(url);
const data = await response.text();
return data;
} catch (err) {
console.error(err);
}
}
fetchData(url)
.then((svg) => {
const map = document.getElementById("svg");
map.insertAdjacentHTML("afterbegin", svg);
})
.then(() => {
const svgElement = document.getElementById("svg");
const { width, height } = svgElement.firstChild.getBBox();
return { width, height };
})
.then(({ width, height }) => {
const img = new Image();
img.src = url;
const bounds = [
[0, 0], // padding
[width, height], // image dimensions
];
L.imageOverlay(img.src, bounds).addTo(map);
map.fitBounds(bounds);
});
uj5u.com熱心網友回復:
好的,所以最后我使用了@Grzegorz T. 的代碼,并稍作修改。添加一個常量 H = 100;扮演任意高度的角色,然后使用瀏覽器給出的尺寸比計算寬度 W。(比例相同,不同瀏覽器的SVG尺寸不同)
這是代碼:
const W = 100; // this allow to get the correct ratio
// then it dosen't depend on browser giving different dimentions.
let map = L.map("image-map", {
crs: L.CRS.Simple,
minZoom: 1,
maxZoom: 4,
});
function loadImage(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.addEventListener("load", () => resolve(img));
img.addEventListener("error", reject);
img.src = url;
});
}
async function getImageData(url) {
const img = await loadImage(url);
return { img, width: img.naturalWidth, height: img.naturalHeight };
}
async function showOnMap(url) {
const { img, width, height } = await getImageData(url);
const H = W * width / height; // hopefuly height is not = 0 (mozilla ?)
const southWest = map.unproject([0, H], map.getMaxZoom() - 4);
const northEast = map.unproject([W, 0], map.getMaxZoom() - 4);
const bounds = new L.LatLngBounds(southWest, northEast);
L.imageOverlay(img.src, bounds).addTo(map);
map.fitBounds(bounds);
}
showOnMap(
"https://fractalcitta.github.io/assets/images/bhikkhu-patimokkha.svg"
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/394444.html
上一篇:在svg上填充波形
