如何使用SVG平滑线条?

时间:2013-08-11 03:31:39

标签: svg raphael

我在看this示例。如何使用Raphael进行以下示例?

Raphael("canvas", function () {
    var win = Raphael._g.win,
        doc = win.document,
        hasTouch = "createTouch" in doc,

        M = "M",
        L = "L",
        d = "d",
        COMMA = ",",
        // constant for waiting doodle stop
        INTERRUPT_TIMEOUT_MS = hasTouch ? 100 : 1,
        // offset for better visual accuracy
        CURSOR_OFFSET = hasTouch ? 0 : -10,

        paper = this,
        path = "", // hold doodle path commands
        // this element draws the doodle
        doodle = paper.path(path).attr({
            "stroke": "rgb(255,0,0)"
        }),

        // this is to capture mouse movements
        tracker = paper.rect(0, 0, paper.width, paper.height).attr({
            "fill": "rgb(255,255,255)",
            "fill-opacity": "0.01"
        }),
        active = false, // flag to check active doodling
        repath = false, // flag to check if a new segment starts
        interrupt; // this is to connect jittery touch

    tracker.mousedown(function () {
        interrupt && (interrupt = clearTimeout(interrupt));
        active = true;
        repath = true;
    });

    tracker.mousemove(function (e, x, y) {
        // do nothing if doodling is inactive
        if (!active) {
            return;
        }

        // Fix for Raphael's touch xy bug
        if (hasTouch && 
                (e.originalEvent.targetTouches.length === 1)) {
            x = e.clientX + 
                (doc.documentElement.scrollTop || doc.body.scrollTop || 0);
            y = e.clientY + 
                (doc.documentElement.scrollLeft || doc.body.scrollLeft || 0);
            e.preventDefault();
        }

        // Insert move command for a new segment
        if (repath) {
            path += M + (x + CURSOR_OFFSET) + COMMA + 
                    (y + CURSOR_OFFSET);
            repath = false;
        }
        path += L + (x + CURSOR_OFFSET) + COMMA + 
                (y + CURSOR_OFFSET); // append line point

        // directly access SVG element and set path
        doodle.node.setAttribute(d, path);
    });

    // track window mouse up to ensure mouse up even outside
    // paper works.
    Raphael.mouseup(function () {
        interrupt && (interrupt = clearTimeout(interrupt));
        // wait sometime before deactivating doodle
        interrupt = setTimeout(function () {
            active = false;
        }, INTERRUPT_TIMEOUT_MS);
    });

以上代码是从https://stackoverflow.com/a/17781275/1595858

复制的

2 个答案:

答案 0 :(得分:0)

要通过几个点创建平滑线,请使用Raphael的Catmull-Rom扩展到SVG路径。请参阅:http://raphaeljs.com/reference.html#Paper.path

// Creating a line:

var line = paper.path("M x0 y0 R x1 y1 x2 y2 x3 y3 x4 y4");



// Adding next point:

line.attr("path", line.attr("path") + " x5 y5");

答案 1 :(得分:0)

在你的情况下(使用Raphael)平滑线条的最简单方法是使用Raphael._path2curve函数。

您正在执行的地方doodle.node.setAttribute(d, path);将其替换为以下行,从而用曲线替换所有线段。

doodle.node.setAttribute(d, Raphael._path2curve(path));

请注意,这会导致性能下降。通过实时转换到曲线的路径而不是每次都转换整个路径,这有很大的改进。