// define global color constants
var COLORS = {
BLUE: '#022961',
GRAY: '#BBBBBB'
};
// get the canvas element by its id
var canvas = document.getElementById('canvas');
// get a "context" from the canvas
var context = canvas.getContext('2d');
// set the fill color to blue
context.fillStyle = COLORS.BLUE;
// draw a rectangle on the canvas
context.fillRect(100, 100, 50, 50); // (x, y, width, height)
// get the image element by its id
var logo = document.getElementById('maverickLogo');
// draw the image on the canvas
context.drawImage(logo, 200, 200); // (image, x, y)
// draw a nifty 3d-looking graph on the screen
for (var i = 0; i < 10; i++) {
// "reset" the current path
context.beginPath();
// set the stroke color
if (i % 2 == 0) { // if i is even
// set the stroke color to blue
context.strokeStyle = COLORS.BLUE;
} else { // otherwise (i is odd)
// set the stroke color to gray
context.strokeStyle = COLORS.GRAY;
}
// draw the bottom-left portion of the graph
context.moveTo(0, i * 40);
context.lineTo((i + 1) * 40, 400);
// draw the top-right portion of the graph
context.moveTo(i * 40, 0);
context.lineTo(400, (i + 1) * 40);
// render the lines to the canvas
context.stroke();
}
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.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
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!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.