最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - building a color wheel in html5 - Stack Overflow

programmeradmin2浏览0评论

I am just learning some details about html5 canvas, and in the progress, I am trying to build a simple color wheel by wedges (build a 1 degree wedge at a time and add it up to 360 degree). However, I am getting some weird marks on the gradient as shown in the following image:

.

Here is the fiddle that produced the colorwheel: /

In particular, this is the 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 anyone can point out what I am doing wrong or if there is a better way to acplish what I am attempting to do it would be much appreciated :)

I am just learning some details about html5 canvas, and in the progress, I am trying to build a simple color wheel by wedges (build a 1 degree wedge at a time and add it up to 360 degree). However, I am getting some weird marks on the gradient as shown in the following image:

.

Here is the fiddle that produced the colorwheel: http://jsfiddle/53JBM/

In particular, this is the 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 anyone can point out what I am doing wrong or if there is a better way to acplish what I am attempting to do it would be much appreciated :)

Share Improve this question edited Aug 16, 2013 at 4:36 FurtiveFelon asked Aug 16, 2013 at 4:29 FurtiveFelonFurtiveFelon 15.2k27 gold badges79 silver badges97 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Is this enough to you, please check

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

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

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>

Similar approach, just for the 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./dersimn/jquery-colorwheel

发布评论

评论列表(0)

  1. 暂无评论