简体   繁体   English

使用HTML5 Canvas + KineticJS绘制完美的圆弧

[英]Drawing perfectly circular arcs with HTML5 Canvas + KineticJS

I'm currently trying to draw arcs using the KineticJS canvas framework. 我目前正在尝试使用KineticJS画布框架绘制弧线。 The issue I'm having is that these arcs are not nearly as "perfectly" circular as required. 我遇到的问题是这些弧线并不像所需的那样“完美”圆形。

They're being drawn out as follows: 他们被抽出如下:

var redArc = new Kinetic.Shape({
    drawFunc: function (canvas) {
        var ctx = canvas.getContext();
        ctx.beginPath();
        ctx.arc(x, y, r, 0, Math.PI / 2, true);    // Arc of 270°
        canvas.fillStroke(this);
    },
    x: x,
    y: y,
    stroke: 'rgb(255,0,0)',
    strokeWidth: 20,
    offset: [x, y]
});

I knew that this might have been an issue since there's no such thing as rendering a near-perfect circle on a pixel based medium without the use of anti-aliasing on the stroke 我知道这可能是一个问题,因为没有在基于像素的介质上渲染近乎完美的圆而没有在笔划上使用抗锯齿

I was wandering if this issue might be resolved by rendering with vector graphics or if there was an alternative solution? 如果这个问题可以通过使用矢量图形渲染或者是否有替代解决方案来解决,那么我在徘徊?

I've included a JSFiddle to outline this issue, the circles are being animated rotating around their axis. 我已经包含了一个JSFiddle来概述这个问题,这些圆圈正在围绕它们的轴旋转动画。 This effect is more apparent with the arc(s) appearing to "wobble" as they rotate. 当圆弧在旋转时看起来“摆动”时,这种效果更明显。

JSFiddle: http://jsfiddle.net/EHDFV/ JSFiddle: http//jsfiddle.net/EHDFV/

Could the wobble be an optical illusion. 摆动可能是一种视错觉。

Have added a stationary black circle around your animation. 在动画周围添加了一个固定的黑色圆圈。

var blackCircle = new Kinetic.Shape({
    drawFunc: function (canvas) {
        var ctx = canvas.getContext();
        ctx.beginPath();
        ctx.arc(x, y, (r + 10), 0, 2 * Math.PI, true);
        canvas.fillStroke(this);
    },
    x: x,
    y: y,
    stroke: 'rgb(0,0,0)',
    strokeWidth: 1,
    offset: [x, y]
});

Does your wobble cross the black circle at any point or leave a gap? 你的摆动是否会在任何一点穿过黑色圆圈或留下间隙?

New fiddle http://jsfiddle.net/EHDFV/1/ 新小提琴http://jsfiddle.net/EHDFV/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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