Pen Settings



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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


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


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.


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.


                <canvas id="myCanvas" width="500" height="300" style="border: solid 2px lightgrey;"></canvas>




 * Created on 2015-04-20.
var canvas;
var context;
var x = 200; //start position
var y = 100; //start position
var mx = 2; //x coordinate translation
var my = 4; //y coordinate translation
var WIDTH = 500; //canvas boundaries
var HEIGHT = 300; //canvas boundaries

window.onload = init(); //start the ball

function init() {
	canvas = document.getElementById("myCanvas"); //assign the canvas to a variable
	context = canvas.getContext("2d"); //Create context method to enable draw methods
	// return setInterval(draw, 20); //call the draw function every 20 milliseconds

function circle(x, y, r) {
	context.beginPath(); //starts the drawing path
	context.arc(x, y, r, 0, Math.PI * 2, true); //define the size and shape of circle
	context.fill(); //fill the circle to display it

function draw() {

	//parameters are the x and y coordinates and the radius(size)
	circle(x, y, 20);

	 * Up to here should draw a circle in the bottom right corner
	 * The circle gets redrawn every 20 milliseconds
	 * But there is no change in position

	//Check to see if the ball is a side of the canvas
	if (x + mx > WIDTH || x + mx < 0) {
		mx = -mx; //change the x direction
	//check to see if the ball is at the top or bottom of the canvas
	if (y + my > HEIGHT || y + my < 0) {
		my = -my; //change the y direction

	x += mx; //change the next x coordinate of the ball
	y += my; //change the next y coordinate of the ball

	 * Now the ball moves and bounces around in the canvas
	 * However it leaves behind a trail
	 * We need to clear the canvas before each move


function clear() {
	//remove any drawings from the canvas
	context.clearRect(0, 0, WIDTH, HEIGHT);