清除画布重新加载图像?

时间:2012-02-24 12:21:38

标签: javascript image html5-canvas kineticjs

我在这里做错了什么?

我正在使用html5图像标记向canvs写一个图像。 我想要它取代新图像的最后一张图像.. 我能够加载图像,但点击下一个图像,它重叠最后加载的图像.. 这是代码.. 我正在尝试使用clearRect()函数

           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(); 


          });

2 个答案:

答案 0 :(得分:1)

选中post

canvas.width = canvas.width; 

应该清除你的画布。

答案 1 :(得分:0)

使用我创建的这个功能

这样称呼 - 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

        }