我正在嘗試使用觸控板縮放 SVG 圓(通過向上/向下移動兩個手指)并且變換的原點必須是游標的位置。縮放第一次表現良好,并且對于每一次其他嘗試,圓圈都會改變位置(不應該)。如果游標位于圓內并靠近其周邊,則可以清楚地看到這一點。下面是代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" content="width=device-width">
<style>
.container
{
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="container">
<svg id="svg" height="600" width="600">
<circle cx="300" cy="300" r="300" stroke="black" stroke-width="3" fill="white"/>
</svg>
</div>
<script>
let scale = 1;
const e = document.getElementById("svg");
function wheelZoom(event)
{
event.preventDefault();
scale = event.deltaY * -0.01;
scale = Math.min(Math.max(.5, scale), 2);
x = 100*(event.clientX-e.getBoundingClientRect().x)/e.getBoundingClientRect().width;
y = 100*(event.clientY-e.getBoundingClientRect().y)/e.getBoundingClientRect().height;
e.style.transformOrigin = `${x}% ${y}%`;
e.style.transform = `scale(${scale})`;
}
e.addEventListener("wheel", wheelZoom);
</script>
</body>
</html>
uj5u.com熱心網友回復:
我不確定以下幾點:
- 你不希望圓圈改變位置是什么意思
- 還有你是否真的想要整個 SVG 縮放,或者只是 SVG 內的圓圈
在下面的演示中,我保持 SVG 不變,但根據滾動滑鼠滾輪時您在 SVG 內部的位置縮放圓圈
希望這是你所追求的。
// let scale = 1;
const svg = document.getElementById("svg");
const circle = document.querySelector("svg circle");
// Circle transform. Inits to 1:1 scale (called an "identity transform").
var circleTransform = svg.createSVGMatrix(); // start
svg.addEventListener("wheel", wheelZoom);
function wheelZoom(event)
{
event.preventDefault();
// Get the mouse position as SVG coordinates
var coords = convertScreenCoordsToSvgCoords(event.clientX, event.clientY);
// Calculate an appropriate scale adjustment
var scale = 1.0 (event.deltaY * 0.001);
// To scale around the mouse coords, first we transform the coordinate
// system so that the origin is at the mouse coords.
circleTransform = circleTransform.translate(coords.x, coords.y);
// Then we apply the scale
circleTransform = circleTransform.scale(scale, scale);
// Finally we move the coordinate system back to where it was
circleTransform = circleTransform.translate(-coords.x, -coords.y);
// Now we need to update the circle's transform
var transform = svg.createSVGTransform(); // An SVGTransform DOM object...
transform.setMatrix(circleTransform); // set to the new circleTransform...
circle.transform.baseVal.initialize(transform); // and used to update the circle transform property
}
function convertScreenCoordsToSvgCoords(x, y) {
var pt = svg.createSVGPoint(); // An SVGPoint SVG DOM object
pt.x = x;
pt.y = y;
// getScreenCTM tells us the combined transform that determines where
// the circle is rendered. Including any viewBox.
// We use the inverse of that to convert the mouse X and Y to their
// equivalent values inside the SVG.
pt = pt.matrixTransform(circle.getScreenCTM().inverse());
return {'x': pt.x, 'y': pt.y};
}
svg {
background-color: linen;
}
<div class="container">
<svg id="svg" height="600" width="600">
<circle cx="300" cy="300" r="300" stroke="black" stroke-width="3" fill="white"/>
</svg>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/374146.html
標籤:javascript html css svg css 转换
上一篇:在magento2admin中存盤的專案排序值小計在哪個表上?
下一篇:SVG中嵌套類的CSS選擇器
