我正在創建一個編輯器。 我想在我的對象上的基本功能是旋轉/調整大小和翻譯。如何在調整大小期間讓我的光標與控制點同步?
我已經成功地做到了他們三人,但唯一的問題是我現在的鼠標位置不遵循控制點(該問題變得更壞下面沒有顯示其他控制點)..
您會在下面找到右側中間尺寸的示例,旋轉角度爲30度,沒有mouseY位置,請注意,當旋轉角度等於0時,鼠標完全遵循我的控制點。
是否有輕鬆的方式解決這個問題,我會走錯路嗎?
下面是jsfiddle鏈接,您可以在其中更改代碼中的旋轉角度以便自己查看JSiddle link。 (只需單擊並拖動黑色控制點來調整的對象)
//convert value of range amin to amax to the range bmin to bmax;
function imap(value, amin, amax, bmin, bmax)
{
\t if ((amax - amin))
\t \t return (value - amin) * (bmax - bmin)/(amax - amin) + bmin;
\t return (0);
};
//get mouse coordinates from the SVG element
function getMouse(el, e)
{
var pt = el.createSVGPoint();
\t pt.x = e.clientX;
\t pt.y = e.clientY;
\t var cursorpt = pt.matrixTransform(el.getScreenCTM().inverse());
\t return({x: cursorpt.x, y: cursorpt.y})
};
var controlPoint = document.getElementById("c"); //My control point element
var mouseX;
var mouseXClicked = 0;
var scaleX = 1;
var scaleY = 1;
var scaleXClicked = 1;
var control = false; // sets if resizeRightMiddle() should be executed
var rectWidth = 100; //is normally tooken with a getBBox() function
var scale = document.getElementById("scale");
function resizeRightMiddle()
{
//convert difference between original mouse X postion on click and actual X mouse position into a scale factor
\t plusX = imap(mouseX - mouseXClicked, 0, rectWidth, 0, 1);
//add converted scale factor to the original x scale value
\t resX = scaleXClicked + plusX;
\t scale.setAttribute('transform', 'scale(' + resX + ',' + scaleY + ')');
\t scaleX = resX;
}
var svg = document.getElementById("main");
// save Scale and X mouse coordinate on click
svg.addEventListener("mousedown", function(e){
\t var coord = getMouse(svg, e);
\t mouseXClicked = coord.x;
\t scaleXClicked = scaleX;
});
svg.addEventListener("mousemove", function(e){
//get mouse coordinates
\t var coord = getMouse(svg, e);
\t mouseX = coord.x;
// resize if control element has been clicked
\t if (control)
\t \t resizeRightMiddle();
});
// desactivate resize
svg.addEventListener("mouseup", function(e){
\t control = false;
});
//activate resize
controlPoint.addEventListener("mousedown", function(){
\t control = true;
});
svg {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
<div>
<svg id="main" width="1000" height="300">
\t <g transform="translate(66, 56)">
\t \t <g id="rotate" transform-origin="center" transform="rotate(30)">
\t \t \t <g id="scale">
\t \t \t <path fill="red" stroke="red" d="M 0 0 L 0 100 L 100 100 L 100 0Z" />
\t \t \t \t <rect id="c" fill="black" stroke="black" x=95 y=45 width=10 height=10 />
\t \t \t </g>
\t \t </g>
\t </g>
</svg>
</div>
您的代碼會更容易一點過目和/或原因大約有一些更好的變量名:什麼是'imap','v',一個''..., 'b ...'和'RM'? –
imap是一個映射函數,用於將值v從一個範圍amin映射到amax,將另一個範圍bmin映射到bmax。 RM是正確的中間調整大小功能,主要功能是調整形狀 – JSmith