--- title: CanvasRenderingContext2D.beginPath() slug: Web/API/CanvasRenderingContext2D/beginPath tags: - API - Canvas - CanvasRenderingContext2D - Referencia - metodo translation_of: Web/API/CanvasRenderingContext2D/beginPath ---
El método CanvasRenderingContext2D
.beginPath()
del API Canvas 2D comienza una nueva ruta vaciando la lista de sub-rutas. Invoca este método cuando quieras crear una nueva ruta.
void ctx.beginPath();
beginPath
Este es solo un trozo de código el cual usa el método beginPath
.
<canvas id="canvas"></canvas>
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // First path ctx.beginPath(); ctx.strokeStyle = 'blue'; ctx.moveTo(20,20); ctx.lineTo(200,20); ctx.stroke(); // Second path ctx.beginPath(); ctx.strokeStyle = 'green'; ctx.moveTo(20,20); ctx.lineTo(120,120); ctx.stroke();
Edita el código aquí debajo y mira tus cambios actualizarse en vivo en el canvas:
<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" style="height:200px"> // First path ctx.beginPath(); ctx.strokeStyle = 'blue'; ctx.moveTo(20,20); ctx.lineTo(200,20); ctx.stroke(); // Second path ctx.beginPath(); ctx.strokeStyle = 'green'; ctx.moveTo(20,20); ctx.lineTo(120, 120); 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', 700, 460) }}
Especificación | Estatus | Comentario |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-beginpath", "CanvasRenderingContext2D.beginPath")}} | {{Spec2('HTML WHATWG')}} |
{{CompatibilityTable}}
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Soporte básico | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Característica | Android | Chrome para Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |