调整HTML5 Canvas Circle的大小onclick

时间:2013-02-07 01:02:52

标签: javascript html5 scale

尝试将圆圈的大小加倍onclick()。似乎没有用?

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

2 个答案:

答案 0 :(得分:1)

你需要重新绘制你的圆圈,并记住context.scale()也会缩放它的位置,所以我不建议这样做。你可以用更大的半径绘制一个新的圆圈。

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

答案 1 :(得分:1)

如果有人需要帮助,这里有一个我需要的jsFiddle。

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