用画布绘制的星爆形状看起来非常像素化(锯齿)
我正在尝试使用画布创建“星爆”效果,但线段出现令人难以置信的像素化。我做错了什么吗?
var rays = 40;
var canvas = $("header canvas")[0];
var ctx = canvas.getContext("2d");
var centre = [canvas.width*0.2,canvas.height*0.90];
var radius = Math.sqrt(Math.pow(canvas.width,2)+Math.pow(canvas.height,2));
var colours = ["red","white"];
var segment = 2*Math.PI/rays;
for(var i=0;i<rays;i++){
ctx.beginPath();
ctx.moveTo(centre[0], centre[1]);
ctx.arc(
centre[0],
centre[1],
radius,
segment * i,
segment * (i+1),
false
);
ctx.lineTo(centre[0], centre[1]);
ctx.closePath();
ctx.fillStyle = colours[i % colours.length];
ctx.fill();
}
I'm trying to create a "starburst" effect using the canvas, but the segment lines appear incredibly pixelated. Am I doing something wrong?
var rays = 40;
var canvas = $("header canvas")[0];
var ctx = canvas.getContext("2d");
var centre = [canvas.width*0.2,canvas.height*0.90];
var radius = Math.sqrt(Math.pow(canvas.width,2)+Math.pow(canvas.height,2));
var colours = ["red","white"];
var segment = 2*Math.PI/rays;
for(var i=0;i<rays;i++){
ctx.beginPath();
ctx.moveTo(centre[0], centre[1]);
ctx.arc(
centre[0],
centre[1],
radius,
segment * i,
segment * (i+1),
false
);
ctx.lineTo(centre[0], centre[1]);
ctx.closePath();
ctx.fillStyle = colours[i % colours.length];
ctx.fill();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用矢量图形(即 SVG)而不是画布。它将消除像素化线条的问题。
更具体地说,如果您使用 Raphael Javascript 库,您的代码可能几乎相同。
额外的好处是 Raphael 还可以在旧版本的 IE 中运行,因为它可以检测到 SVG 不可用时并切换到使用 VML。
I'd suggest using vector graphics (ie SVG) instead of canvas. It will eliminate your problems of pixellated lines.
More specifically, if you use the Raphael Javascript library, your code could be virtually the same.
The added bonus is that Raphael also works in older versions of IE, as it can detect and switch to using VML if SVG isn't available.
抗锯齿由浏览器完成。现在,您的代码在 Firefox 4 和 IE9 中看起来非常出色,但在 webkit 浏览器中看起来很差。
chrome/safari 的做法有一些优点,像这样,看起来不错在 chrome 上可以,但在 Firefox 4 上很差。
人们要求规范编写者提供布尔控制抗锯齿功能,但他反对这个想法。
Anti-aliasing is done by the browser. Right now your code will look excellent in Firefox 4 and IE9 but poor in webkit browsers.
There are plusses to the way chrome/safari do it, like this, which will look good on chrome but poor on Firefox 4.
People have asked the spec writer for a boolean controlling anti-aliasing, and he was opposed to the idea.