Skip to content Skip to sidebar Skip to footer

How Scale All The Points Used To Make Lines/curves On A Canvas?

I am recording all the points drawn on a canvas via the mousedown and mousemove events. When the canvas is resized (for example, if it was 100w x 200h, and the user makes it 200w x

Solution 1:

I think your problem is you are applying the ratio twice to each point by modifying savedPoints[i] AND savedPoints[i+1] through the start and end objects. The next iteration, where i = i+1, savedPoints[i] will have already been modified once.

    //The new ratios
    var wRatio = canvas.width / oldWidth;
    var hRatio = canvas.height / oldHeight;

    //We need to scale and draw all our points
    for ( var i = 0, last = savedPoints.length - 1; i < last; i++ ) {
        var start = savedPoints[ i ];
        var end = savedPoints[ i + 1 ];
        var endx, endy, startx, starty;

        //We're at a new line
        if ( start === "stopline" ) continue;

        //We're finishing a previous line
        else if ( end === "stopline" ) {
            i++;
            continue;
        }

        //Resize them
        startx = start.x * wRatio;
        starty = start.y * hRatio;
        endx = end.x * wRatio;
        endy = end.y * hRatio;

        //Start recording
        context.beginPath();

        //Move the "pen" to the point
        context.moveTo( startx, starty );

        //Make the connection of the "line" with our data (I draw the lines originally this way to make more "natural")
        context.quadraticCurveTo( startx, starty, endx, endy );

        //This doesn't work either
        //context.lineTo( startx, starty, end.x, end.y );

        //Draw the line
        context.stroke();
    }

    for ( var i = 0, last = savedPoints.length - 1; i < last; i++ ) {
        if (savedPoints[i] !== 'stopline') {
            savedPoints[i].x *= wRatio;
            savedPoints[i].y *= hRatio;
        }
    }

Post a Comment for "How Scale All The Points Used To Make Lines/curves On A Canvas?"