あなたが得ている効果は、どのようにキャンバスが線の形を描くのかという特性のようです。線の厚さの半分が形状の内側に描かれ、半分の厚さは形状の外側に描かれる。 1つの方法は、丸い形と境界を別々のパスとして描画することです。あなたの例でこれを行うための変更を以下に示します。これは不規則な形ではもっと難しくなります。
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var topLeftCornerX = 188;
var topLeftCornerY = 50;
var width = 200;
var height = 100;
var linewidth = 10;
context.globalAlpha = 0.5;
context.beginPath();
context.moveTo(topLeftCornerX, topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY+height);
context.lineTo(topLeftCornerX,topLeftCornerY+height);
context.closePath();
context.fillStyle = "#FF0000";
context.fill();
context.beginPath();
context.moveTo(topLeftCornerX-linewidth/2, topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY+height+linewidth/2);
context.lineTo(topLeftCornerX-linewidth/2,topLeftCornerY+height+linewidth/2);
context.closePath();
context.lineWidth = linewidth;
context.strokeStyle = "#00FF00";
context.stroke();
返信ありがとうございます。しかし、私の場合、私はいつも複雑な道を描く必要があります。私が見つけた1つの解決策は、最初に要素を塗りつぶし、次に "globalCompositeOperation = copy"を使って線を引くことです。しかし、私はキャンバスで1つだけの形をしたいときにのみ役立ちます。しかし私の場合、私はすでにキャンバスに多くの図形を描いています。他の図形の上に別の図形を描く必要があります。私もそれを描画することができますが、私は一時的なキャンバスを使用し、それに新しい図形を描画する必要がありますし、 "drawImage"を使用して、メインキャンバスに図形を描く必要があります。しかし、私は一時的なキャンバスを望んでいません。 – Parth