当画布元素调整大小(通过样式更改)时,我还想缩放画布的绘制图像。我不能只更改高度/宽度,因为这会导致画布自行清除,所以我这样做:
- 创建一个临时画布元素
- 将当前画布的图像绘制到该临时画布上
- 调整当前画布的大小
- 将临时画布的图像绘制回当前画布,但缩放到新大小
这会导致一些模糊 - 在多次调整大小后非常明显(例如:拖动以调整大小时)。我将如何在没有任何模糊的情况下做到这一点?
编辑:关闭context.webkitImageSmoothingEnabled = false;
图像平滑(
调用调整大小事件:
var tmpCanvas = null;
//Make a temporary canvas
tmpCanvas = document.createElement( "canvas" );
//Set its size to be equal
tmpCanvas.height = originalCanvas.height;
tmpCanvas.width = originalCanvas.width;
//Draw our current canvas onto it
tmpCanvas.getContext( "2d" ).drawImage( originalCanvas, 0, 0 );
//Set new dimensions
originalCanvas.width = originalCanvas.offsetWidth;
originalCanvas.height = originalCanvas.offsetHeight;
var originalContext = originalCanvas.getContext( "2d" );
//Set background and colors
originalContext.fillStyle = "#ffffff";
originalContext.strokeStyle = "#000000";
//Set paintbrush
originalContext.lineWidth = 4;
originalContext.lineCap = "round";
//Fill background as white
originalContext.fillRect( 0, 0, originalCanvas.width, originalCanvas.height );
//We have a saved signature
if ( SignatureCanvas.hasSignature === true )
{
//Draw it back but scaled (results in blurred image)
originalContext.drawImage( tmpCanvas, 0, 0, tmpCanvas.width, tmpCanvas.height, 0, 0, originalCanvas.width, originalCanvas.height );
/**
* This results in a blurred image as well
//Draw it back but scaled
originalContext.scale( originalCanvas.width / tmpCanvas.width, originalCanvas.height / tmpCanvas.height );
originalContext.drawImage( tmpCanvas, 0, 0, tmpCanvas.width, tmpCanvas.height, 0, 0, tmpCanvas.width, tmpCanvas.height );
*/
}
有没有办法获得笔触并“缩放”所有这些点并重绘?