HTML5 canvas -- draw text and circle inside one function?

Is it possible to combine these two functions below into one function that draws the circle and the text ... inside of one function?  Please provide JavaScript code.

Do I call beginPath( ) every time I introduce a new element to the canvas?

I'm still new to HTML5 canvas and context / path.

  function drawCircle(myCircle, context) {           
            context.beginPath();
            context.arc(myCircle.x, myCircle.y, myCircle.radius, myCircle.startAngle, myCircle.endAngle, myCircle.counterClockwise);
            context.fillStyle = myCircle.color;            
            context.fill();
            context.strokeStyle = '#000';     
            context.stroke();            
        }

        function drawText(myCircle, context, text) {          
            context.beginPath();          
            context.fillStyle = '#000';          
            context.fill();
            context.font = "40px Georgia";
            context.fillText(myCircle.useletter, myCircle.x - 10, myCircle.y + 10);
            context.strokeStyle = '#000';  
            context.stroke();                    
        }

Open in new window

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
If you were drawing another shape, you would need to close the first path with context.closePath() then begin a new path with context.beginPath(). Otherwise what you see as a second shape would be perceived as a continuation of the first shape by the rendering engine. With text however you don't need to begin a new path. You should still close the first path though.
var canvas = document.getElementById('myCanvas'); 
var context = canvas.getContext('2d');
var myCircle = {
            x: 300,
            y: 300,
            radius: 50,
            startAngle: 0,
            endAngle: Math.PI * 2,
            counterClockwise: false            
        };

	drawCircle(myCircle, context, "EE");

  function drawCircle(myCircle, context, text) {           
            context.beginPath();
            context.arc(myCircle.x, myCircle.y, myCircle.radius, myCircle.startAngle, myCircle.endAngle, myCircle.counterClockwise);
            context.fillStyle = myCircle.color;            
            context.fill();
            context.strokeStyle = '#000';     
            context.stroke();
	   context.closePath();
           context.fillStyle = '#999';
            context.font = "40px Georgia";
            context.fillText(text, myCircle.x - 10, myCircle.y + 10);
            context.strokeStyle = '#000';  
            context.stroke();           
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.