I have a problem with .toDataURL()
for large canvas. I want to enconde in base64
and decode on php file but if I have a large canvas the strD
Have updated the code to split the canvas into smaller canvas objects. Works pretty good and have added a tracker also:
This Allows for tracking of the upload process and overall I think is better for the user. I use PHP to rejoin at a later stage.
It avoids the issues of size of canvas / browser etc.
My first post so hope it helps!
// pass in type for the file name
function sliceCanvas(type, canvasId){
var largeCanvas = document.getElementById(canvasId).getContext('2d');
var slice = document.createElement('canvas').getContext('2d');
var baseSize = 500;
fileH = largeCanvas.canvas.height / baseSize;
fileW = largeCanvas.canvas.width / baseSize;
slice.canvas.width = baseSize;
slice.canvas.height = baseSize;
count = 1;
numFiles = Math.ceil(fileH) * Math.ceil(fileW);
for (var y=0; y < largeCanvas.canvas.height; y+=baseSize){
for (var x=0; x < largeCanvas.canvas.width; x+=baseSize){
slice.clearRect(0, 0, slice.canvas.width, slice.canvas.height);
slice.drawImage(largeCanvas.canvas, x, y, baseSize, baseSize, 0, 0, baseSize, baseSize);
var imagePiece = slice.canvas.toDataURL();
typeFinal = type + count;
exportSlice(typeFinal, imagePiece, numFiles);
count++;
}
}
}
Ajax to upload:
function exportSlice(type, dataURL, fileNum){
percent = 0;
percentComplete = 0;
$.ajax({
type: "POST",
url: YourServerSideFiletoSave,
data: {image: dataURL, type: type}
})
.done(function( response ) {
console.log(response);
percent++;
percentComplete = Math.ceil(Number(percent/fileNum*100));
return true;
})
.fail(function(response) {
console.log("Image FAILED");
console.log(response);
return false;
})
.always(function(response) {
console.log( "Always");
});
}