I record all the points drawn on the canvas through the mousedown and mousemove . When the canvas size changes (for example, if it was 100w x 200h and the user makes it 200w x 400h), I want to redraw all these points / lines, but at a new scale. What is the right way to do this?
I am currently using the code as shown below, but it does not look right. He draws weird extra lines.
To save points, I write a point in mousedown and mousemove and mark the line as done in mouseup .
A resize event is raised:
//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 ]; //We're at a new line if ( start === "stopline" ) continue; //We're finishing a previous line else if ( end === "stopline" ) { i++; continue; } //Resize them start.x = start.x * wRatio; start.y = start.y * hRatio; end.x = end.x * wRatio; end.y = end.y * hRatio; //These lines don't make a difference //savedPoints[ i ] = start; //savedPoints[ i + 1 ] = end; //Start recording context.beginPath(); //Move the "pen" to the point context.moveTo( start.x, start.y ); //Make the connection of the "line" with our data (I draw the lines originally this way to make more "natural") context.quadraticCurveTo( start.x, start.y, end.x, end.y ); //This doesn't work either //context.lineTo( start.x, start.y, end.x, end.y ); //Draw the line context.stroke(); }
source share