--- title: CanvasRenderingContext2D.fill() slug: Web/API/CanvasRenderingContext2D/fill tags: - API - Canvas - CanvasRenderingContext2D translation_of: Web/API/CanvasRenderingContext2D/fill ---
La méthode CanvasRenderingContext2D
.fill()
de l'API Canvas 2D remplit le chemin courant ou donné avec la couleur de fond en cours, en utilisant la règle de remplissage extérieur/intérieur non nul ou la règle de remplissage pair/impair.
void ctx.fill([fillRule]); void ctx.fill(path[, fillRule]);
fillRule
"nonzero
": La règle de remplissage extérieur/intérieur non nul, qui est la règle par défaut."evenodd"
: La règle de remplissage pair/impair.path
fill
Ceci est un simple snippet de code qui utilise la méthode fill
pour remplir un chemin.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.rect(10, 10, 100, 100); ctx.fill();
Éditez le code ci-dessous pour voir vos changements apportés au canvas en direct:
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> ctx.rect(10, 10, 100, 100); ctx.fill();</textarea>
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);
{{ EmbedLiveSample('Playable_code', 700, 360) }}
fillRule
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(96.50,50.00); ctx.bezierCurveTo(96.50,62.84,0.22,99.82,50.74,47.17); ctx.bezierCurveTo(100.18,0.58,62.84,96.50,50.00,96.50); ctx.bezierCurveTo(24.32,96.50,3.50,75.68,3.50,50.00); ctx.bezierCurveTo(3.50,24.32,24.32,3.50,50.00,3.50); ctx.bezierCurveTo(75.68,3.50,96.50,24.32,96.50,50.00); ctx.closePath(); ctx.fillStyle = "lightblue"; ctx.lineWidth = 2; ctx.fill("nonzero"); ctx.stroke();
Éditez le code ci-dessous pour voir vos changements apportés au canvas en direct:
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> ctx.beginPath(); ctx.moveTo(96.50,50.00); ctx.bezierCurveTo(96.50,62.84,0.22,99.82,50.74,47.17); ctx.bezierCurveTo(100.18,0.58,62.84,96.50,50.00,96.50); ctx.bezierCurveTo(24.32,96.50,3.50,75.68,3.50,50.00); ctx.bezierCurveTo(3.50,24.32,24.32,3.50,50.00,3.50); ctx.bezierCurveTo(75.68,3.50,96.50,24.32,96.50,50.00); ctx.closePath(); ctx.fillStyle = "lightblue"; ctx.lineWidth = 2; ctx.fill("nonzero"); ctx.stroke();</textarea>
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);
{{EmbedLiveSample('Playable_code_2', 700, 360)}}
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-fill", "CanvasRenderingContext2D.fill")}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.CanvasRenderingContext2D.fill")}}