Próbuję utworzyć okrągły pasek postępu (choć nie byłby to już pasek, prawda?). Wokół tego cricle są cienkie paski prostopadłe do koła. Problem polega na tym, że mój kod nie generuje prętów w równym odstępie. Oto kod i wizerunek wyniku: Tworzenie okręgu pasków z płótnem HTML5, ale odstęp między kreskami jest nierówny
function MH5PB(canvasId, //the id of the canvas to draw the pb on
value, //a float value, representing the progress(ex: 0.3444)
background, //the background color of the pb(ex: "#ffffff")
circleBackground, //the background color of the bars in the circles
integerColor, //the color of the outer circle(or the int circle)
floatColor //the color of the inner circle(or the float circle)
)
{
var canvas = document.getElementById(canvasId);
var context = canvas.getContext("2d");
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var radius = Math.min(canvasWidth, canvasHeight)/2;
var numberOfBars = 72;
var barThickness = 2;
//margin from the borders, and also the space between the two circles
var margin = parseInt(radius/12.5) >= 2 ? parseInt(radius/12.5) : 2;
//the thickness of the int circle and the float circle
var circleThickness = parseInt((radius/5) * 2);
//the outer radius of the int circle
var intOuterRadius = radius - margin;
//the inner radius of the int circle
var intInnerRadius = radius - margin - circleThickness;
//the outer radius of the float circle
var floatOuterRadius = intOuterRadius - margin - circleThickness;
//the inner radius of the float circle
var floatInnerRadius = floatOuterRadius - circleThickness;
//draw a bar, each degreeStep degrees
var intCircleDegreeStep = 5;
// ((2 * Math.PI * intOuterRadius)/(barThickness + 10)) //
// this area is the total number of required bars //
// to fill the intCircle.1px space between each bar//
var floatCircleDegreeStep = 360/((2 * Math.PI * floatOuterRadius)/(barThickness + 10));
context.lineWidth = barThickness;
context.strokeStyle = circleBackground;
//draw the bg of the outer circle
for(i = 90; i < 450; i+=intCircleDegreeStep)
{
//since we want to start from top, and move cw, we have to map the degree
//in the loop
cxOuter = Math.floor(intOuterRadius * Math.cos(i) + radius);
cyOuter = Math.floor(intOuterRadius * Math.sin(i) + radius);
cxInner = Math.floor(intInnerRadius * Math.cos(i) + radius);
cyInner = Math.floor(intInnerRadius * Math.sin(i) + radius);
context.moveTo(cxOuter, cyOuter);
context.lineTo(cxInner, cyInner);
context.stroke();
}
}
EDIT: Oh, a także linie nie są wygładzane. Wiesz dlaczego? Należy również wyjaśnić, że ten pasek postępu składa się z dwóch części. Zewnętrzny okrąg (widoczny na załączonym obrazku) i wewnętrzny okrąg. Zewnętrzny okrąg jest wielkością całkowitej części wartości procentowej (to jest 45 w 45,98%), a wewnętrzny okrąg jest wielkością niecałkowitej części wartości procentowej (to jest 98 w 45,98%). Stąd teraz wiesz, czym jest intCircle i floatCircle :)
tylko małe przypuszczenie tutaj: Twój numer barów nie może być lubiany przez rzeczy, więc to kondensacyjny je produkować dobre kółko? Spekulacja –
Dzięki za zainteresowanie Ian. Cóż, nie jestem pewien. Podejrzewam, że ma to coś wspólnego z funkcjami sinus i cosinus, wiesz. Ponieważ tworzą one podwójną wartość, ilość dodawana do następnych współrzędnych jest nierównomierna. Ale nie wiem, jak to naprawić: D A przy okazji, dzięki za edycję;) –
Nigdy wcześniej nie rysowałem niczego w JS, więc niestety nie pomogę tutaj. Weź jednak udział w przeglądzie. :) –