繁体   English   中英

调整HTML5 Canvas Circle的大小onclick

[英]Resize HTML5 Canvas Circle onclick

尝试将圆圈的大小加倍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); 
        }

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

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

这里有一个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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM