Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
	<h1>My Canvas Drawing Board</h1>
	<canvas id="myCanvas" width="800"
		height="550" style="border:5px solid red;">
	</canvas>
	<div class='pt-3 pb-3'>
		<button class="btn btn-info" id="clear">Clear Canvas</button>
	</div>
		<div class="formcontainer"> 
			<form class="rectSquare">
				<h2 class="pt-3">Draw a Rectange or Square.</h2>
				<div class="form-group">
					<label for="x">X coordinate:</label>
					<input id="x" class="form-control" type="number" placeholder="Number from 10 - 200">
				</div>

				<div class="form-group">
					<label for="y">Y coordinates:</label>
					<input id="y" class="form-control" type="number" placeholder="Number from 10 - 200">
				</div>

				<div class="form-group">
					<label for="width">Width: Value from 1 - 200:</label>
					<input id="width" class="form-control" type="number" placeholder="20">
				</div>

				<div class="form-group">
					<label for="height">Height: Value from 1 - 200:</label>
					<input id="height" class="form-control" type="number" placeholder="20">
				</div>

				<div class="form-group">
					<label for="color">Colour: Type a color, red, blue, green, yellow. or HEX Value #efefef, or you can get a color from <a target="_blank" href="http://htmlcolorcodes.com">here</a></label>
					<input id="color" class="form-control" type="text" placeholder="red or #ff0000">
				</div>
				<input type="submit" id="submitRect" class="btn btn-success" value="Draw Rectangle/Square">
			</form>
			
			
			<form class="circle">
				<h2 class="pt-3">Draw a Circle</h2>
				<div class="form-group">
					<label for="centerx">centerX coordinate:</label>
					<input id="centerx" class="form-control" type="number" placeholder="Number from 1 - 770">
				</div>
				<div class="form-group">
					<label for="centerx">centerY coordinate:</label>
					<input id="centery" class="form-control" type="number" placeholder="Number from 1 - 500">
				</div>
				<div class="form-group">
					<label for="radius">Radius coordinate:</label>
					<input id="radius" class="form-control" type="number" placeholder="Number from 1 - 200">
				</div>
				<div class="form-group">
					<label for="circleColor">Colour: Type a color, red, blue, green, yellow. or HEX Value </label>
					<input id="circleColor" class="form-control" type="text" placeholder="red or #ff0000">
				</div>
				<input type="submit" id="submitCircle" class="btn btn-success" value="Draw Circle">
			</form>
		</div>
</div>
	
	
              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

html,body {
	background-color: #efefef;
	font-size: 16px;
}
h1 {
	padding: .5em 0;
}
h2 {
	font-size: 1.2em;
}
#myCanvas {
	margin: 1em 0;
	width: 100%;
	background-color: black;
}

.formcontainer {
	display:flex;
	justify-content: space-between;
}

.rectSquare {
	width: 45%;

}

.circle {
	width: 45%;
}
              
            
!

JS

              
                var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d'); 

let x, y, width, height, color; 

$('#submitRect').on('click', function(e){
	e.preventDefault();
	x = $('#x').val();
	y = $('#y').val();
	width = $('#width').val();
   height = $('#height').val();
	color = $('#color').val();
	
	// Call function to create a shape.
	createRect(x,y,width,height, color)
});

$('#submitCircle').on('click', function(e){
	e.preventDefault();
	x = $('#centerx').val();
	y = $('#centery').val();
	r = $('#radius').val();
	cColor = $('#circleColor').val();
	console.log(cColor) 
	
	// Call function to create a shape.
	createCircle(x,y,r,cColor);
});

document.getElementById('clear').addEventListener('click', function() {
        c.clearRect(0, 0, canvas.width, canvas.height);
      }, false);

function createRect(x, y, w, h, color){
	c.beginPath();
	c.rect(x,y,w,h);
	c.fillStyle = color;
	c.fill();
	c.lineWidth = 2;
	c.strokeStyle = 'white';
	c.stroke();
}

function createCircle(x,y,r,cColor) {
    // draw the colored region
      c.beginPath();
						//X,Y, Radius, startAngle, endAngle, isAntiClockwise
      c.arc(x, y, r, 0, 2 * Math.PI, true);
      c.fillStyle = cColor;
   	c.fill();
 
    // draw the stroke
    	c.lineWidth = 2;
   	c.strokeStyle = "#66CC01";
    	c.stroke();
}




              
            
!
999px

Console