---
title: CanvasRenderingContext2D.fillStyle
slug: Web/API/CanvasRenderingContext2D/fillStyle
tags:
  - Canvas
  - CanvasRenderingContext2D
translation_of: Web/API/CanvasRenderingContext2D/fillStyle
---
<div>{{APIRef}}</div>

<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.fillStyle</code></strong> 是Canvas 2D API 使用内部方式描述颜色和样式的属性。默认值是 <code>#000</code> (黑色)。</p>

<p>参见  <a href="/en-US/docs/Web/API/Canvas_API/Tutorial">Canvas Tutorial</a> 的 <a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors">Applying styles and color</a> 章节。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var><em>ctx</em>.fillStyle = color;
</var><var><em>ctx</em>.fillStyle = gradient;
</var><var><em>ctx</em>.fillStyle = pattern;</var>
</pre>

<h3 id="选项">选项</h3>

<dl>
 <dt><code>color</code></dt>
 <dd>{{domxref("DOMString")}} 字符串,被转换成 CSS {{cssxref("&lt;color&gt;")}} 颜色值.</dd>
 <dt><code>gradient</code></dt>
 <dd>{{domxref("CanvasGradient")}} 对象 (线性渐变或者放射性渐变).</dd>
 <dt><code>pattern</code></dt>
 <dd>{{domxref("CanvasPattern")}} 对象 (可重复图像)。</dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="Using_the_fillStyle_property" name="Using_the_fillStyle_property">使用 <code>fillStyle</code> 属性设置不同的颜色</h3>

<p>这是一段简单的代码片段,使用 <code>fillStyle</code> 属性设置不同的颜色。</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);
</pre>

<p>修改下面的代码并在线查看canvas的变化:</p>

<div class="hidden">
<h6 id="Playable_code" name="Playable_code">Playable code</h6>

<pre class="brush: html">&lt;canvas id="canvas" width="400" height="200" class="playable-canvas"&gt;&lt;/canvas&gt;
&lt;div class="playable-buttons"&gt;
  &lt;input id="edit" type="button" value="Edit" /&gt;
  &lt;input id="reset" type="button" value="Reset" /&gt;
&lt;/div&gt;
&lt;textarea id="code" class="playable-code"&gt;
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 100, 100);&lt;/textarea&gt;
</pre>

<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}

reset.addEventListener("click", function() {
  textarea.value = code;
  drawCanvas();
});

edit.addEventListener("click", function() {
  textarea.focus();
})

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
</pre>
</div>

<p>{{ EmbedLiveSample('Playable_code', 700, 360) }}</p>

<h3 id="fillStyle_使用_for_循环的例子"><code>fillStyle</code> 使用 <code>for</code> 循环的例子</h3>

<p>在这个例子中, 我们使用两个 <code>for</code> 循环绘制一个矩形表格,每个单元格都有不同的颜色。 最终的结果图像看起来像屏幕截图,其实没有令人惊叹的事情发生。我们使用两个变量 i 和 j 为每一个单元格生成唯一的RGB颜色,并且只改变红色和绿色的数值。蓝色通道的值是固定不变的。通过修改这些通道,你能生成各种各样的调色板。通过逐步地增加,你能实现类似 Photoshop 的调色板。</p>

<div class="hidden">
<pre class="brush: html">&lt;canvas id="canvas" width="150" height="150"&gt;&lt;/canvas&gt;</pre>
</div>

<pre class="brush: js">var ctx = document.getElementById('canvas').getContext('2d');
for (var i=0;i&lt;6;i++){
  for (var j=0;j&lt;6;j++){
    ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' +
                     Math.floor(255-42.5*j) + ',0)';
    ctx.fillRect(j*25,i*25,25,25);
  }
}
</pre>

<p>结果看起来像是这样的:</p>

<p>{{EmbedLiveSample("A_fillStyle_example_with_for_loops", 160, 160, "https://mdn.mozillademos.org/files/5417/Canvas_fillstyle.png")}}</p>

<h2 id="规格描述">规格描述</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-fillstyle", "CanvasRenderingContext2D.fillStyle")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.CanvasRenderingContext2D.fillStyle")}}</p>

<div id="compat-desktop"></div>

<h3 id="WebKitBlink-specific_注解">WebKit/Blink-specific 注解</h3>

<ul>
 <li>支持 WebKit- 和 Blink-based 的浏览器中, 有一个不标准的、被反对的方法 <code>ctx.setFillColor()</code> 已经实现。

  <pre class="brush: js">setFillColor(color, optional alpha);
setFillColor(grayLevel, optional alpha);
setFillColor(r, g, b, a);
setFillColor(c, m, y, k, a);
</pre>
 </li>
</ul>

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/zh-CN/docs/Web/API/Canvas_API">Canvas API</a></li>
 <li>接口定义, {{domxref("CanvasRenderingContext2D")}}</li>
 <li>此属性使用的值:
  <ul>
   <li>{{cssxref("&lt;color&gt;")}} CSS数据类型</li>
   <li>{{domxref("CanvasGradient")}} 对象</li>
   <li>{{domxref("CanvasPattern")}} 对象</li>
  </ul>
 </li>
</ul>