Building a color wheel in html5

I just study some details about html5 canvas, and in the process I try to create a simple color wheel with wedges (build a wedge 1 degree at a time and add it up to 360 degrees). However, I get some weird marks on the gradient, as shown in the following image:

wierd color marks .

Here is the fiddle that created the colorwheel: http://jsfiddle.net/53JBM/

In particular, this is JS code:

var canvas = document.getElementById("picker"); var context = canvas.getContext("2d"); var x = canvas.width / 2; var y = canvas.height / 2; var radius = 100; var counterClockwise = false; for(var angle=0; angle<=360; angle+=1){ var startAngle = (angle-1)*Math.PI/180; var endAngle = angle * Math.PI/180; context.beginPath(); context.moveTo(x, y); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.closePath(); context.fillStyle = 'hsl('+angle+', 100%, 50%)'; context.fill(); } 

If someone can point out what I'm doing wrong, or if there is a better way to accomplish what I'm trying to do, that would be very helpful :)

+4
source share
3 answers

Is this enough for you, please check

 var startAngle = (angle-2)*Math.PI/180; 
+8
source

Try it, it looks great. Thanks, by the way, this is exactly what I was trying to do.

 var canvas = document.getElementById("picker"); var context = canvas.getContext("2d"); var x = canvas.width / 2; var y = canvas.height / 2; var radius = 100; var counterClockwise = false; for(var angle=0; angle<=360; angle+=1){ var startAngle = (angle-2)*Math.PI/180; var endAngle = angle * Math.PI/180; context.beginPath(); context.moveTo(x, y); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.closePath(); var gradient = context.createRadialGradient(x, y, 0, x, y, radius); gradient.addColorStop(0,'hsl('+angle+', 10%, 100%)'); gradient.addColorStop(1,'hsl('+angle+', 100%, 50%)'); context.fillStyle = gradient; context.fill(); } 
 <body> <canvas id="picker"></canvas> </body> 
+5
source

A similar approach, only for color

 var canvas = document.getElementById("picker"); var context = canvas.getContext("2d"); var x = canvas.width / 2; var y = canvas.height / 2; var radius = 50; var thickness = 0.6; for(var angle=0; angle<=360; angle+=1){ var startAngle = (angle-2)*Math.PI/180; var endAngle = angle * Math.PI/180; context.beginPath(); context.arc(x, y, (1-thickness/2)*radius, startAngle, endAngle, false); context.lineWidth = thickness*radius; context.strokeStyle = 'hsl('+angle+', 100%, 50%)'; context.stroke(); } 
 <body> <canvas id="picker"></canvas> </body> 

Edit: full project here: https://github.com/dersimn/jquery-colorwheel

0
source

Source: https://habr.com/ru/post/1497364/


All Articles