How to script Canvas?

Posted on 2014-08-27
Last Modified: 2014-08-28
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!
Question by:ladesigner2014
    1 Comment
    LVL 35

    Accepted Solution

    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:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    A Change in PHP Behavior with Session Write Short Circuit ( (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now