我正在通过mousedown
和mousemove
事件记录画布上绘制的所有点。当画布调整大小时(例如,如果它是 100w x 200h,而用户将其设为 200w x 400h),我想重绘所有这些点/线,但要以新的比例。这样做的正确方法是什么?
我目前正在使用如下代码,但无法正确绘制。它画出奇怪的额外线条。
为了保存这些点,我将点记录在mousedown
和中,mousemove
并将一条线标记为在 中完成mouseup
。
调用调整大小事件:
//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();
}