When resizing a canvas element (using a style change), I also want to scale the inverse of the canvas. I can't just change the height / width as this causes the canvas to clear itself, so I do:
- Creating a temporary canvas item
- Draw a picture of the current canvas on this temporary canvas
- Resize current canvas
- Draw the temp canvas image back to the current canvas, but scale to a new size.
This leads to some blurring - very noticeable after many changes (for example: when dragging to resize). How can I do this without any blur?
EDIT: Disabling image anti-aliasing ( context.webkitImageSmoothingEnabled = false; ) does not fix the problem, it just makes it redraw it more and more, until the image looks like the original after several resizing.
A resize event is raised:
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 ); */ }
Is there a way to get the strokes and “scale” all these points and redraw?
source share