<b>Practice Set 14.1: Draw an X</b>
<p>In this practice problem, you will use CanvasRenderingContext2D methods to draw lines between opposite corners to make an X. See the Javascript comments for guidance.</p>
<div>Canvas
<br/>
<canvas id="c1" width="300" height="200"></canvas>
</div>
// get the canvas and context objects
var canvas = document.getElementById("c1");
var ctx = canvas.getContext('2d');
// We can draw on this 'ctx' object. Let's set some styles
ctx.strokeStyle = "red";
ctx.lineWidth = "2";
ctx.beginPath(); // initialize a new drawing path
ctx.moveTo(0, 0); // move the pen to where we'll start
ctx.lineTo(299, 199); // create a line with the pen
ctx.stroke();
ctx.beginPath(); // initialize a new drawing path
ctx.moveTo(-0, -0); // move the pen to where we'll start
ctx.lineTo(199, 299); // create a line with the pen
ctx.stroke();
// Remember from the example in
// video 14.3 how to draw lines, or check the MDN docs:
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineTo
// In steps:
// call beginPath() to start the drawing path
// call moveTo() to position the pen at the starting point at one corner
// call lineTo() to define a line to the opposite corner
// call stroke() to draw it
// Do this twice so that two corner-to-corner lines cross in the middle - it'll
// look like a big red 'x'
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.