boyfromnorth
boyfromnorth

Reputation: 1013

clearing the canvas for reloading image?

What am I doing wrong here??

I am writing an image to canvs using html5 image tag. what i want it to replace the last image onclick of the new image.. I am able to load image but on click of next image it overlaps the last loaded image.. Here is the code.. I'm trying to use clearRect() function

           function drawImage(imageObj){

            var stage = new Kinetic.Stage("container", 578, 500);
            var layer = new Kinetic.Layer();
            var x = stage.width / 2 - 200 / 2;
            var y = stage.height / 2 - 137 / 2;
            var width = 200;
            var height = 137;

            // darth vader
            var darthVaderImg = new Kinetic.Shape(function(){
                var context = this.getContext();

                context.clearRect(x,y,width,height);
                context.drawImage(imageObj, x, y, width, height);
                // draw invisible detectable path for image
                context.beginPath();
                context.rect(x, y, width, height);
                context.closePath(); 


          });

Upvotes: 0

Views: 2767

Answers (2)

Knights
Knights

Reputation: 1477

Use this function that I created

Call it this way -- clearCanvasGrid(/id of the canvas/)

function clearCanvasGrid(canvasname){
            var canvas = document.getElementById(canvasname); //because we are looping //each location has its own canvas ID
            var context = canvas.getContext('2d');
            //context.beginPath();

            // Store the current transformation matrix
            context.save();

            // Use the identity matrix while clearing the canvas
            context.setTransform(1, 0, 0, 1, 0, 0);
            context.clearRect(0, 0, canvas.width, canvas.height);

            // Restore the transform
            context.restore(); //CLEARS THE SPECIFIC CANVAS COMPLETELY FOR NEW DRAWING

        }

Upvotes: 0

Çağdaş Bozman
Çağdaş Bozman

Reputation: 2540

Check this post.

canvas.width = canvas.width; 

should clear your canvas.

Upvotes: 1

Related Questions