0
ここではほとんど問題はありません。 2Dキャンバスで作業し、四角形の2Dキャンバスにマウスボタンをドラッグして長方形を作成することができます。この矩形は柔軟で、マウスをドラッグすることで任意の方向に移動できます。2Dキャンバスの任意の側面にあるオブジェクトをドラッグする方法
この矩形は、Image1にあるものとまったく同じように見えるはずです。この四角形を上から下にマウスをドラッグして作成すると完璧に見えますが、これを左に回すと、Image2が見えます。コーナーの角はボックスの外にあり、奇妙です。これは、カーソルを上側に移動した場合も同じです(Image3を参照)。
この矩形は、すべての面でImage1のように見えます。誰でも助けてください、どうすればこの問題を解決できますか?
Image1 - 希望する画像。どちらのマウスカーソルポイントでも問題ありません。
function drawBox(x, y, w, h, crosshairSize, detailWidth, fill, detailCol) {
ctx.globalAlpha = 0.3;
function drawCross(x, y, s, w) { // draw crosshair s is size, w is width
const hw = w/2; // half width
ctx.beginPath();
ctx.lineTo(x - hw, y - hw);
ctx.lineTo(x - hw, y - s);
ctx.lineTo(x + hw, y - s);
ctx.lineTo(x + hw, y - hw);
ctx.lineTo(x + s, y - hw);
ctx.lineTo(x + s, y + hw);
ctx.lineTo(x + hw, y + hw);
ctx.lineTo(x + hw, y + s);
ctx.lineTo(x - hw, y + s);
ctx.lineTo(x - hw, y + hw);
ctx.lineTo(x - s, y + hw);
ctx.lineTo(x - s, y - hw);
ctx.closePath();
ctx.fill()
}
function drawCorner(x, y, dx, dy, s, w) { // draw crosshair s is size, w is width
// dx and dy are directions
const hw = w/2; // half width
ctx.beginPath();
ctx.lineTo(x, y);
ctx.lineTo(x + dx * s, y);
ctx.lineTo(x + dx * s, y + dy * w);
ctx.lineTo(x + dx * w, y + dy * w);
ctx.lineTo(x + dx * w, y + dy * s);
ctx.lineTo(x, y + dy * s);
ctx.closePath();
ctx.fill();
}
ctx.fillStyle = fill;
ctx.fillRect(x, y, w, h);
ctx.fillStyle = detailCol;
drawCross(x + w/2, y + h/2, crosshairSize, detailWidth);
drawCorner(x, y, 1, 1, crosshairSize * 2, detailWidth);
drawCorner(x + w, y, -1, 1, crosshairSize * 2, detailWidth);
drawCorner(x + w, y + h, -1, -1, crosshairSize * 2, detailWidth);
drawCorner(x, y + h, 1, -1, crosshairSize * 2, detailWidth);
}` //end of function`
//calling drawBox function
drawBox(startposition.x, startposition.y, width * 2, height * 2, crosshairSize, 1, "#6E97B1", '#0055FE');