我正在通过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();
}
答案 0 :(得分:1)
我认为您的问题是,您通过savedPoints[i]
和savedPoints[i+1]
对象修改start
和end
,将比率应用于每个点两次。下一次迭代,其中i = i+1
,savedPoints[i]
将被修改一次。
//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;
}
}