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

  • |

    Migrating WordPress Multisite from Media Temple to SiteGround

    A few years ago, I wrote about domain mapping using WordPress Multisite on Media Temple. This year, I’ve been consolidating all of my hosted websites to a single SiteGround account and the very Multisite instance I wrote about needed to be moved over. I had been avoiding it but the time had come, especially since…

  • | |

    Creating a Responsive Gutenberg Price Table

    Last week I worked on an upcoming tutorial for a popular online publication on how to style the Gutenberg Columns block (I’ll be sure to send that along when it comes out). As as result, I decided to experiment to see what you could reasonable do, and came up with this Gutenberg Price Table: https://codepen.io/jcasabona/pen/RYvEYd. In…

  • | | |

    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…

  • My Eclipse Tutorial at Theme Forest

    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!Theme Forest posted my tutorial on using Eclipse with PDT to make a nice PHP IDE. Check it out here

  • | |

    Deleting 3 Million Spam Comments from a WordPress Database

    Over at the Crowd Favorite blog, I wrote up a post about an interesting problem I solved recently. I laid out everything you need to know there, but it involves downloading a HUGE database and putting WP-CLI to good use. If you haven’t used it before and you do development with WordPress, it’s super valuable….