How to script Canvas?

scripting help needed for this specific URL:

Simply, I am trying to script a clearCanvas drawing feature via a clearRect function. Can anyone let me know how to do this?

What I think are the snippets needed to script this are as follows, but I do not know how the JavaScript area gets resolved within the web page in order to clear Canvas1:

<a href="#canvas1">clearCanvas</a>

function clearCanvas() {            
            context.clearRect(0, 0, canvas.width, canvas.height);

Any scripting solution that properly "runs" on the JSFIDDLE site - and clears the drawing on the Canvas area - would be greatly appreciated. Thank you!
Who is Participating?
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.

Robert SchuttSoftware EngineerCommented:
On the Sketch website there is an example using an eraser tool ( ) but unfortunately that's probably not what you want.
<a href="#tools_sketch" data-tool="eraser">Eraser</a>

Open in new window

Using that you need to click on the canvas to erase it and seems a strange way for a user to work. So I decided to go and try to find you a direct solution.

I'm not sure if any part of the code you posted is 'mandatory' but I've tried using it as you provided and built around that. It could be done more efficiently code-wise but a user will not see any difference.

step 1: set an id on the link:
<a id="clearCanvas1" href="#canvas1">clearCanvas</a>

Open in new window

step 2: add this code below the existing javascript:
    $('#clearCanvas1').on('click', function(){
        var cnv = $('#canvas1').get(0);
        var ctx = cnv.getContext('2d');
        clearCanvas(cnv, ctx); // note, this erases the canvas but not the drawing history!
        $('#canvas1').sketch().actions = []; // found it... probably not a very nice solution though

function clearCanvas(canvas, context) {            
    context.clearRect(0, 0, canvas.width, canvas.height);

Open in new window

As you can see in the comments in the code, after clearing the canvas there was a problem with the old lines returning when you start drawing again because the Sketch plugin saves a complete history of actions. After some research I found you can clear this history (line 6 in the code above) but there may be a much better way.

I saved a new version of your fiddle:

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
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
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.