I guess you are trying to take a snapshot of your three.js scene. You can use FileSaver.js to download the image to your local machine.
here is the link to FileSaver.js https://github.com/eligrey/FileSaver.js/
include the FileSaver.js in your code
All you have to do is,
1) convert the canvas to dataurl(will give you base64 encoded image)
2) convert the base64 data to blob
3) download the blob to local machine
Function to convert the base64 to blob
var base64ToBlob : function( b64Data, contentType, sliceSize ) {
return new Promise( function( resolve, reject ){
contentType = contentType || '';
sliceSize = sliceSize || 512;
try{
//converting each byte in the b64Data to a single character using the atob() method.
var byteCharacters = atob(b64Data);
var byteArrays = [];
// a set of 'sliceSize' number of characters are processed at a time.
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
//slice holds a set of 'sliceSize' number of characters from the byteCharacters array.
var slice = byteCharacters.slice(offset, offset + sliceSize);
//converting each character in 'slice' to the ASCII code.
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
//creating a typed array structure using the ASCII codes of the characters.
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
//now byteArrays holds the whole bytes converted to the ASCII character codes
//convert the typed array to the blob
var blob = new Blob( byteArrays, { type : contentType } );
resolve( blob );
}
catch( error ){
reject( error );
}
} );
}
change your download function like this,
function download() {
var dt = canvas.toDataURL('image/jpeg');
//this.href = dt; //this line is causing the page redirection
base64ToBlob( dt, 'image/png', 512 ).then( function( file ){
FileSaver.saveAs(file, "image.png");//or use just saveAs( file, "image.png" )
}, function( error ){
console.log( error );
} );
}