Draw Cherries using HTML5 Canvas

I’m teaching a class on 2D Animation this semester and opted to use web technologies such as HTML5 Canvas, CSS Transitions, and Javascript. In this tutorial we are going to use the canvas element to draw 2 cherries with stems. I’m having my class use CodePen for their assignments, so this tutorial kind of assumes that. If you want to see the final product, head on over here.

Define Canvas:

First we will define our canvas area. I gave it an ID of “cherries.” It’s also important to note the width and the height. Apply these with CSS may give you varies (or really weird looking) results. Add this to the HTML section:


The rest of our code will be javascript. First we define the canvas in JS and tell it we will be working in a 2D space:

var canvas = document.getElementById('cherries');
var context = canvas.getContext('2d');

Draw first Cherry:

For any object on the canvas, we need to tell it where start our drawing. Here, we’re going to find the center(ish) of the canvas for both the starting point of our cherry and its stem. We will also define the size of the cherry. Anything we draw will start with context.beginPath() to let canvas know we will start drawing:

context.beginPath();
var centerX1 = canvas.width / 3;
var centerY1 = canvas.height / 2;
var radius = 30; 

Since we want the stem to show ‘behind’ the cherry, we draw that first, using a bezierCurve. This will start at our center, then draw a line to the midpoints we define. Here, we have 3 points (x1, y1, x2, y2, x3, y3):

context.moveTo(centerX1, centerY1);
context.bezierCurveTo(280, 30, 320, 40, 340, 50);
context.lineWidth = 1;

context.strokeStyle = '#006600';
context.stroke();

Now we draw the cherry by drawing a red circle with a black border. We will use canvas’ arc function to draw a full, closed circle. There’s a bit of math we need to use to make this work. There is a nice explanation of the arc function over at HTML5 Canvas Tutorials.

context.beginPath();
context.arc(centerX1, centerY1, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'red';
context.fill();
context.lineWidth= 1;
context.strokeStyle= '#000000';
context.stroke();

Draw 2nd Cherry:

Now time for the 2nd cherry. We want that to just touch the first cherry, while making it clear there are 2 objects. We we do here is slightly change the starting X and Y points to be a bit more to the right, and lower. The radius will stay the same.

var centerX2 = canvas.width / 2.4;
var centerY2 = canvas.height / 1.8;
var radius = 30;

Once again, we draw the stem first, follow the same path as above so they end up at the same x3, y3.

context.beginPath();
context.moveTo(centerX2, centerY2);
context.bezierCurveTo(280, 30, 320, 40, 340, 50);
context.lineWidth = 1;

// line color
context.strokeStyle = '#006600';
context.stroke();

And the 2nd cherry:

context.beginPath();
context.arc(centerX2, centerY2, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'red';
context.fill();

context.lineWidth = 1;
context.strokeStyle = '#000000';
context.stroke();

Now let’s give it a text label:

context.font = '40pt helvetica';
context.fillStyle = 'red';
context.fillText('Cherries!', 150, 340);

Conclusion

And there you have it! 2 cherries, with the stems. I’m sure I’ll get more refined at this process as I go along; I’m doing a bit of learning along with my students, but I think this demonstrates nicely some of things you can do with canvas.

Similar Posts

  • | | |

    Trying Flexbox at Casabona.Org

    If you’ve visited Casabona.Org this week, you noticed that the homepage got a bit of a refresh; I decided to add more color, have more concise and focus content, and tried to give visitors an idea of what I do as soon as they get to the site. One new thing I tried was using…

  • | |

    Rock-Solid WordPress 3.0 Themes using Custom Post Types

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!The good people over at Nettuts+ have published a tutorial I wrote for them called Rock-Solid WordPress 3.0 Themes using Custom…

  • |

    How to Create a FREE Landing Page with ConvertKit

    ConvertKit has recently opened up a free plan for people to build landing pages and forms so they can start building their email lists. And while usually you need to refer a friend or pay to see those subscribers and send emails, in this video, you’ll get a special link that will unlock 100 subscribers…

  • | |

    WordPress Multisite Domain Mapping on Media Temple (dv)

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!I recently stood up a WordPress Network/Multisite that I wanted to use for several different client “Quick Sites” – simple WordPress…

  • |

    Adding the Media Uploader in WordPress

    …without including the Editor. In the Admin. That was a really long title, so I hope you don’t feel mislead! I was recently working on a project that required a Custom Post Type without the editor, but needed the Media Uploader. Note: This is not a full-blown tutorial. The purpose of this post is to…