【问题标题】:How to smoothen lines using SVG?如何使用 SVG 平滑线条?
【发布时间】:2013-08-12 16:07:36
【问题描述】:

我正在查看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

【问题讨论】:

标签: svg raphael


【解决方案1】:

要创建通过多个点的平滑线,请使用 Raphael 对 SVG 路径的 Catmull-Rom 扩展。见: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");

【讨论】:

  • 我已经替换了路径 += L + (x + CURSOR_OFFSET) + COMMA + (y + CURSOR_OFFSET); // 用路径附加线点 += R + (x + CURSOR_OFFSET) + COMMA + (y + CURSOR_OFFSET);
  • 不会,曲线需要比单个 x,y 更多的控制点
【解决方案2】:

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

你做doodle.node.setAttribute(d, path);的地方换成下面的那一行,从而把所有的线段都换成曲线。

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

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

【讨论】:

  • 我将使用 Raphael 的 Catmull-Rom,但感谢您的帮助。
  • 抱歉耽搁了。卷入了某事。看到您为此使用 catmul 很有趣。你能分享一下涂鸦小提琴的增强功能吗?
猜你喜欢
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 2021-02-06
  • 2020-06-03
相关资源
最近更新 更多