简体   繁体   English

画布形状中的控制点

[英]Control Points in Canvas shape

I have made canvas Shapes(squares) in kinetic js having 4 control points on each of their vertices.A user can click and drag these control points and distort the shape any way he/she pleases. 我用动力学js制作了画布Shapes(squares),在每个顶点上都有4个控制点,用户可以单击并拖动这些控制点并按照自己的喜好变形形状。 I also tried adding control points in the mid-point of each line by adding additional anchors and plugging them into the Bezier Curve..The js fiddle is http://jsfiddle.net/Lucy1/opsy1pn9/4/ 我还尝试通过添加其他锚点并将其插入Bezier曲线来在每条线的中点添加控制点。js小提琴是http://jsfiddle.net/Lucy1/opsy1pn9/4/

The corresponding JS code is 相应的JS代码是

   var room = new Kinetic.Shape({
    x: 0,
    y: 0,
    width: w,
    height: h,
    stroke: "blue",
    fill: 'red',
    drawFunc: function (context) {
        var x = this.x();
        var y = this.y();
        var w = this.width();
        var h = this.height();
        var tlX = this.anchorTL.x();
        var tlY = this.anchorTL.y();
        context.beginPath();
        context.moveTo(tlX, tlY);
        // top
        context.bezierCurveTo(x + w / 3, y, this.anchorTM.x(), this.anchorTM.y(), this.anchorTR.x(), this.anchorTR.y());
        // right
        context.bezierCurveTo(x + w, y + h / 3, this.anchorRM.x(), this.anchorRM.y(), this.anchorBR.x(), this.anchorBR.y());
        // bottom
        context.bezierCurveTo(x + w * 2 / 3, y + h, this.anchorBM.x(), this.anchorBM.y(), this.anchorBL.x(), this.anchorBL.y());
        // left
        context.bezierCurveTo(x, y + h * 2 / 3, this.anchorLM.x(), this.anchorLM.y(), tlX, tlY);

        context.closePath();
        context.fillStrokeShape(this);
    }
});

g.add(room);

room.anchorTR = makeAnchor(w, 0, g);
room.anchorBR = makeAnchor(w, h, g);
room.anchorBL = makeAnchor(0, h, g);
room.anchorTL = makeAnchor(0, 0, g);

room.anchorTM = makeAnchor(w/2,0,g);
room.anchorRM = makeAnchor(w,h/2,g);
room.anchorLM = makeAnchor(0,h/2,g);
room.anchorBM = makeAnchor(w/2,h,g);

layer.draw();
}

The problem i am facing is that the mid-point control points are not moving with the line like the control points situated at the vertex..Please Help. 我面临的问题是中点控制点没有像位于顶点的控制点那样随线移动。请帮助。

In looking at the history of your posts, you have previously been using Cubic Bezier Curves. 在查看帖子的历史记录时,您以前一直在使用三次贝塞尔曲线。

Each Bezier curve has 4 control points so you need 4 anchors--not 3 as you show. 每个贝塞尔曲线都有4个控制点,因此您需要4个锚点,而不是您显示的3个。 The control points are: (1) starting point (a corner) (2) mid point influencing the starting point (3) mid point influencing the ending point (4) ending point (a corner). 控制点是:(1)起点(角)(2)影响起点的中点(3)影响终点的中点(4)终点(角)。

But your fiddle uses just one control point on your curve between the corners. 但是您的小提琴只在拐角之间的曲线上使用了一个控制点。 This indicates a Quadratic Curve instead of a Cubic Bezier Curve. 这表明是二次曲线,而不是三次贝塞尔曲线。

Each Quadratic curve has 3 control points so you need 3 anchors as in your fiddle. 每条二次曲线都有3个控制点,因此您需要3个锚点,就像小提琴一样。 The control points are: (1) starting point (a corner) (2) middle control point influencing the curve (3) ending point (a corner). 控制点是:(1)起点(拐角)(2)影响曲线的中间控制点(3)终点(拐角)。

So if instead you want the user to drag on a quadratic curve to manipulate that curve, you can approximate the resulting middle quadratic control point using this math: 因此,如果相反,您希望用户在二次曲线上拖动以操纵该曲线,则可以使用以下数学公式来近似得到中间的二次控制点:

var controlPointX = 2*mouseX -startpointX/2 -endpoinX/2;
var controlPointY = 2*mouseY -startpointY/2 -endpointY/2;

Here's a Demo having the user drag to adjust a Quadratic curve: 这是一个让用户拖动以调整二次曲线的演示:

http://jsfiddle.net/m1erickson/f4ag0myj/ http://jsfiddle.net/m1erickson/f4ag0myj/

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

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