Elijah Murray
Elijah Murray

Reputation: 2172

Resize HTML5 Canvas Circle onclick

Trying to double the size of a circle onclick(). Doesn't seem to be working?

HTML

<canvas id="drawing" width="600px" height="600px" onclick="resize()"></canvas>

Javascript

        window.onload = function() {
            canvas=document.getElementById("drawing");
            context=canvas.getContext("2d");
            var centerX = canvas.width / 2;
            var centerY = canvas.height / 2;
            var radius = 70;

            context.beginPath();
            context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
            context.fillStyle = 'green';
            context.fill();
        }

        function resize () {
            context.fillStyle= "#701be0"; // This changes the rectangle to blue
            context.fill();
            context.scale(10.5, 2.5); 
        }

Upvotes: 0

Views: 5351

Answers (2)

Elijah Murray
Elijah Murray

Reputation: 2172

Here's a jsFiddle for exactly what I needed, in case anyone needs help.

http://jsfiddle.net/elijahmurray/fHv82/1/

x=200;
y=200;    
size=100;
radius=30;

function animate() {
reqAnimFrame =  window.mozRequestAnimationFrame    || //get framerate
                window.webkitRequestAnimationFrame ||
                window.msRequestAnimationFrame     ||
                window.oRequestAnimationFrame
            ;
reqAnimFrame(animate);


if(radius <= 200) {
    radius +=3; 
} //increase size by 1 per frame

    draw();
}

function draw() {
var canvas  = document.getElementById("ex1");
var context = canvas.getContext("2d");

  context.beginPath();
  context.arc(x, y, radius, 0, 2 * Math.PI, false);
  context.fillStyle = 'green';
  context.fill();
}
animate();

Upvotes: 1

dimusic
dimusic

Reputation: 4133

You need to redraw your circle again and also keep in mind that context.scale() will also scale its position so i wouldn't recommend to do it this way. You can just draw a new circle over with bigger radius.

jsFiddle - http://jsfiddle.net/DSFMq/

Upvotes: 2

Related Questions