繁体   English   中英

如何用圆弧平滑在画布中绘制的圆的边缘?

[英]How to smooth out edges of circles drawn in canvas with arc?

我正在使用弧线在html5画布中绘制圆,但是边缘粗糙且不光滑。 我正在寻找使它们平滑的方法。 堆积的溢出要求我写更多的东西,所以我的代码与文本的比率更好

(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||   window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
}());

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

createCircle(100, 150, '85', '675');
createCircle(300, 150, '70', '479');
createCircle(500, 150, '91', '715');
createCircle(700, 150, '58', '334');


function createCircle(x, y, temp, hash, callback) {
  var radius = 75;
  var endPercent = parseInt(temp, 10);
  var curPerc = 0;
  var counterClockwise = false;
  var circ = Math.PI * 2;
  var quart = Math.PI / 2;

  context.strokeStyle ='#006600';
  context.lineWidth = 10;
  context.lineCap = "round";
  var offset = quart;

function doText(context, x, y, temp, hash) {
    context.lineWidth = 10;  
    if(parseInt(temp, 10) > 90)
        context.fillStyle = '#ad2323';
    else if(parseInt(temp, 10) > 82)
        context.fillStyle = '#ffcc33';
    else
        context.fillStyle = '#006600';

    context.font = "28px sans-serif";
    context.fillText(temp + 'º', x - 20, y + 10);
    context.fillText(hash + 'KH/s', x - 50, y - 90);
}

function animate(current) {
    context.lineWidth = 10;
    if(curPerc > 89)
        context.strokeStyle = '#ad2323';
    else if(curPerc > 81)
        context.strokeStyle = '#ffcc33';

    context.beginPath();
    context.arc(x, y, radius, offset, ((circ) * current) + offset , false);
    context.stroke();
    context.closePath();      
    curPerc++;
    if (curPerc < endPercent) {
       requestAnimationFrame(function () {
            animate(curPerc / 100);
        });
    } 
    else {
        doText(context, x, y, temp, hash);
        if (callback) callback.call();
    }
 }
 animate();
}

JSFiddle = http://jsfiddle.net/uhVj6/712/

您要绘制笔划多次,因此它们会相互绘制。 您需要清除旧的弧形笔触所在的区域,然后绘制新的弧形笔触

context.clearRect(x - radius - context.lineWidth, 
                  y - radius - context.lineWidth, 
                  radius * 2 + (context.lineWidth*2), 
                  radius * 2 + (context.lineWidth*2));
context.beginPath();
context.arc(x, y, radius, offset, ((circ) * current) + offset , false);
context.stroke();
context.closePath();

JSFiddle

暂无
暂无

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

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