From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../strokestyle/index.html | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 files/ja/web/api/canvasrenderingcontext2d/strokestyle/index.html (limited to 'files/ja/web/api/canvasrenderingcontext2d/strokestyle') diff --git a/files/ja/web/api/canvasrenderingcontext2d/strokestyle/index.html b/files/ja/web/api/canvasrenderingcontext2d/strokestyle/index.html new file mode 100644 index 0000000000..38b61f3afb --- /dev/null +++ b/files/ja/web/api/canvasrenderingcontext2d/strokestyle/index.html @@ -0,0 +1,128 @@ +--- +title: CanvasRenderingContext2D.strokeStyle +slug: Web/API/CanvasRenderingContext2D/strokeStyle +tags: + - API + - Canvas + - CanvasRenderingContext2D + - Property + - Reference +translation_of: Web/API/CanvasRenderingContext2D/strokeStyle +--- +
{{APIRef}}
+ +

Canvas 2D API のCanvasRenderingContext2D.strokeStyle プロパティは、図形の輪郭に使用する色、グラデーション、またはパターンを指定します。デフォルト値は #000 (黒色)です。

+ +
+

輪郭と塗りつぶしのスタイル例については、 canvas チュートリアルスタイルと色を適用する をご覧ください。

+
+ +

構文

+ +
ctx.strokeStyle = color;
+ctx.strokeStyle = gradient;
+ctx.strokeStyle = pattern;
+
+ +

オプション

+ +
+
color
+
CSS の {{cssxref("<color>")}} として解析される {{domxref("DOMString")}} 。
+
gradient
+
{{domxref("CanvasGradient")}} オブジェクト (線形または放射状のグラデーション) 。
+
pattern
+
{{domxref("CanvasPattern")}} オブジェクト (繰り返し画像) 。
+
+ +

+ +

図形の輪郭色を変更する

+ +

このサンプルでは青色の輪郭を矩形に適用します。

+ +

HTML

+ +
<canvas id="canvas"></canvas>
+
+ +

JavaScript

+ +
var canvas = document.getElementById('canvas');
+var ctx = canvas.getContext('2d');
+
+ctx.strokeStyle = 'blue';
+ctx.strokeRect(10, 10, 100, 100);
+
+ +

実行結果

+ +

{{ EmbedLiveSample('Changing_the_stroke_color_of_a_shape', 700, 160) }}

+ +

ループを使用した複数の輪郭色の作成

+ +

この例では2つの for ループと、 {{domxref("CanvasRenderingContext2D.arc", "arc()")}} メソッドを使用して、それぞれが異なる輪郭色を持つ円のグリッドを描画します。 このためには、2つの変数 ij を円ごとに固有となるRGB色を生成するために使用し、また緑色と青色の値だけを変更するようにします (赤色は固定値とします) 。

+ + + +
var ctx = document.getElementById('canvas').getContext('2d');
+
+for (let i = 0; i < 6; i++) {
+  for (let j = 0; j < 6; j++) {
+    ctx.strokeStyle = `rgb(
+        0,
+        ${Math.floor(255 - 42.5 * i)},
+        ${Math.floor(255 - 42.5 * j)})`;
+    ctx.beginPath();
+    ctx.arc(12.5 + j * 25, 12.5 + i * 25, 10, 0, Math.PI * 2, true);
+    ctx.stroke();
+  }
+}
+
+ +

結果はこのようになります:

+ +

{{EmbedLiveSample("Creating_multiple_stroke_colors_using_loops", "180", "180", "https://mdn.mozillademos.org/files/253/Canvas_strokestyle.png")}}

+ +

仕様

+ + + + + + + + + + + + + + +
仕様書策定状況コメント
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-strokestyle", "CanvasRenderingContext2D.strokeStyle")}}{{Spec2('HTML WHATWG')}} 
+ +

ブラウザー実装状況

+ + + +

{{Compat("api.CanvasRenderingContext2D.strokeStyle")}}

+ + + +

WebKit および Blinkベースのブラウザの場合、このプロパティに加えて、非標準で非推奨のメソッド ctx.setStrokeColor() が実装されています。

+ +
setStrokeColor(color, optional alpha);
+setStrokeColor(grayLevel, optional alpha);
+setStrokeColor(r, g, b, a);
+setStrokeColor(c, m, y, k, a);
+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf