简体   繁体   中英

How to draw stroke inside a circle?

Is there an easy way to draw stroke inside a circle (without drawing 2 circles and similar workarounds)? If I do it this way:

context.beginPath();
context.arc(200, 200, 93, Math.PI / 2, Math.PI, true);
context.fillStyle = '#FF6A6A';
context.fill();
context.lineWidth = 20;
context.strokeStyle = '#FF0000';
context.stroke();

I get this:

在此输入图像描述

The stroke is partially drawn outside the figure (marked by green circles) while I need it inside.

You should change the radius to compensate for the line width:

context.beginPath();
context.arc(200, 200, 93, Math.PI / 2, Math.PI, true);
context.fillStyle = '#FF6A6A';
context.fill();
context.lineWidth = 20;
context.strokeStyle = '#FF0000';
context.beginPath();

// the radius of 93 - half the line width
context.arc(200, 200, 93-10, Math.PI / 2, Math.PI, true);

context.stroke();

JS Bin: http://jsbin.com/qamuwumiri/edit?html,js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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