2

How to resize image already drawed in canvas?

I tried this with no luck (image is showing, but it doesn't resize):

var drawBall = function(mouseX, mouseY){
    ballimg = new Image();
    ballimg.onload = function(){
        ctx.drawImage(ballimg, mouseX-25, mouseY-25);
        ballimg.height = 5;
        throwed = true;

    };
    ballimg.src = "ball2.png";
};

1 Answer 1

3

You can not resize an object which is drawn on the canvas.

What you need to do is redraw your object.

clear the context where the old Ball is located ctx.clearRect(x,y,x2,y2) and draw a new Ball with the new size.

If you want to animate things on the canvas. The way you do that is to keep track of all your objects and redraw the canvas(every single object) for every frame.

Sign up to request clarification or add additional context in comments.

3 Comments

How can I increase speed in redrawing?
that depends on what you want to to. if your want to redraw every second or so you can use the setInterval function w3schools.com/jsref/met_win_setinterval.asp or you can manually trigger a redraw on mouseclick event or something
also best to use requestAnimationFrame than setInterval for better performance. here is a polyfill made by paul irish ... paulirish.com/2011/requestanimationframe-for-smart-animating

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.