若要為 Canvas 文字加上描邊效果,可使用 strokeStyle 設定筆觸顏色,再用 strokeText() 繪製文字輪廓。
context.strokeStyle = "blue";
context.strokeText("Hello World!", x, y);
描邊效果通常會搭配 lineWidth 使用,線寬越大,文字輪廓越明顯。若同時使用 fillText() 與 strokeText(),可以做出填色加描邊的立體文字效果。
context.font = "60pt Calibri";
context.lineWidth = 3;
context.strokeStyle = "blue";
context.strokeText("Hello World!", 80, 110);
實務應用中,描邊文字常用於標題、海報、遊戲介面或需要提高辨識度的視覺設計。