Adrian Buzea
Adrian Buzea

Reputation: 836

Drawing bezier curve on html canvas

I need to draw a bezier curve defined by N control points which are stored in an array. I have a canvas which is 500px x 500px. Here's the JSFiddle link : jsfiddle.net/HswXy Entire JS code:

<script>
    window.onload=function(){
        var old_n=0,n=0;
        var nrSelect = document.getElementById("mySelect");
        var submit = document.getElementById("submit");

        nrSelect.addEventListener("change",function(){
            old_n=n;
            n = nrSelect.selectedIndex;

            var inputx,inputy,br;

            if(document.getElementById("pointsdiv"))
            {
                for(i=1;i<=n;i++){
                    inputx = document.createElement('input');
                    inputy = document.createElement('input');
                    br = document.createElement('br');

                    inputx.type = "text";
                    inputy.type = "text";
                    inputx.size = 3;
                    inputy.size = 3;
                    inputx.id = "x_" + i;
                    inputy.id = "y_" + i;
                    inputx.value = "x_" + i;
                    inputy.value = "y_" + i;

                    inputx.addEventListener("focus",function(){if(this.value==this.id) this.value="";});
                    inputy.addEventListener("focus",function(){if(this.value==this.id) this.value="";});

                    document.getElementById("pointsdiv").appendChild(inputx);
                    document.getElementById("pointsdiv").appendChild(inputy);
                    document.getElementById("pointsdiv").appendChild(br);
                }

                document.getElementById("pointsdiv").id="pointsdiv_after";
            }
            else
            {
                if( old_n < n )
                {
                    for(i=old_n+1;i<=n;i++){
                        inputx = document.createElement('input');
                        inputy = document.createElement('input');
                        br = document.createElement('br');

                        inputx.type = "text";
                        inputy.type = "text";
                        inputx.size = 3;
                        inputy.size = 3;
                        inputx.id = "x_" + i;
                        inputy.id = "y_" + i;
                        inputx.value = "x_" + i;
                        inputy.value = "y_" + i;

                        inputx.addEventListener("focus",function(){if(this.value==this.id) this.value="";});
                        inputy.addEventListener("focus",function(){if(this.value==this.id) this.value="";});

                        document.getElementById("pointsdiv_after").appendChild(inputx);
                        document.getElementById("pointsdiv_after").appendChild(inputy);
                        document.getElementById("pointsdiv_after").appendChild(br);
                    }
                }
                else
                {
                    var parent;

                    for(i=n+1;i<=old_n;i++){
                        parent = document.getElementById("pointsdiv_after");

                        parent.removeChild(parent.lastChild);
                        parent.removeChild(parent.lastChild);
                        parent.removeChild(parent.lastChild);
                    }
                }
            }
        });



        //BEZIER CURVE
        function factorial(n){
            var result=1;
            for(i=2;i<=n;i++){
                result = result*i;
            }
            return result;
        }
        function Point(x,y){
            this.x=x;
            this.y=y;
        }
        var points = new Array();
        function getPoint(t){
            var i;
            var x=points[0].x;
            var y=points[0].y;
            var factn = factorial(n);
            for(i=0;i<n;i++){
                var b = factn / (factorial(i)*factorial(n-i));
                var k = Math.pow(1-t,n-i)*Math.pow(t,i);
                // console.debug( i+": ",points[i] );
                x += b*k*points[i].x;
                y += b*k*points[i].y;
            }
            return new Point(x, y);
        }
        //--BEZIER CURVE
        submit.addEventListener("click",function(){
            if(n){
                for(i=1;i<=n;i++){
                    var px = document.getElementById("x_"+i);
                    var py = document.getElementById("y_"+i);
                    points.push(new Point(parseInt(px.value,10),parseInt(py.value,10)));
                    // console.debug( points[i-1] );
                }
                var canvas = document.getElementById('myCanvas');
                var context = canvas.getContext('2d');
                context.beginPath();
                console.debug( points[0].x, points[0].y );
                context.moveTo(points[0].x, points[0].y);
                var t=0.01;
                while (t<=1)
                {
                    //get coordinates at position
                    var p=getPoint(t);
                    // console.debug( p.x, p.y );
                    //draw line to coordinates
                    context.lineTo(p.x, p.y);
                    //increment position
                    t += 0.01;
                }
                context.stroke();
            }
        });
    }
</script>

The problem is that it doesn't seem to work correctly. I'm testing it for 2 points now and it always starts in the upper left corner o the canvas (even if my first point is at let's say (250,250) and the length of the line is not the number of pixels it should be.. it's longer.

Upvotes: 1

Views: 1176

Answers (1)

bfavaretto
bfavaretto

Reputation: 71908

I'm no expert on canvas or geometry, but here is what I think: you shouldn't moveTo points[0] before drawing your faux line, since your getPoint function seems to lead you there. What I could see while debugging your code is that you first moved to (20,20), then drew a straight line to (roughly) (40,40), then started drawing gradually back to (20,20), generating that closed shape that wasn't what you were looking for.

A small (quick and dirty) change to your code made it start drawing a curve:

// DO NOT MOVE TO HERE
//context.moveTo(points[0].x, points[0].y);
var t = 0.01;

// MOVE TO THE FIRST POINT RETURNED BY getPoint
var p0 = getPoint(t);
context.moveTo(p0.x, p0.y);
t+=0.01;

// now loop from 0.02 to 1...

I'm sure you can refactor that into something better, as I said my changes were quick and dirty.

http://jsfiddle.net/HswXy/3/

Upvotes: 1

Related Questions