0

我在这里做错了什么?

我正在使用 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(); 


          });
4

2 回答 2

1

检查这篇文章

canvas.width = canvas.width; 

应该清除你的画布。

于 2012-02-24T12:35:29.963 回答
0

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

        }
于 2012-11-18T09:25:29.860 回答