Say we have a Base64 image:

Let these 4 images go!

- Create a canvas element in memory and prepare its context :
var canvas = document.createElement('canvas'); var ctx = canvas.getContext("2d");
- Then let them prepare an empty array for further storage of our base lines:
var parts = [];
- Now we need to create a new image , assign the onload function and set it to src:
var img = new Image(); img.onload = split_4; img.src = "data:image/png;base64,iVBORw0KGgoAAAA.......................etc"
- Split_4 function :
If the image we need to cut is 260 × 80, it means that we need to set our canvas element to 1/4 of this size:
var w2 = img.width / 2,
- Draw our canvas 4 times , each time moving our image to new XY positions :
// 0 0 1.iteration // -130 0 2.iteration // 0 -40 3.iteration // -130 -40 4.iteration
and at each iteration of the loop, we simply push the extracted Canvas data into our parts Array:
for(var i=0; i<4; i++){ var x = (-w2*i) % (w2*2), // New X position y = (h2*i)<=h2? 0 : -h2 ; // New Y position ctx.drawImage(this, x, y, w2*2, h2*2); // imgObject, X, Y, width, height parts.push( canvas.toDataURL() ); // ("image/jpeg") for jpeg }
Now always inside the onload function you can get all parts of the image from the array:
console.log( parts ); // ["data:image/png;base64,iV...z9d/oBHAAAAAElFTkSuQmCC", // "data:image/png;base64,iV...yVhNNW1AAAAAElFTkSuQmCC", // "data:image/png;base64,iV...Q2FoAAAAABJRU5ErkJggg==", // "data:image/png;base64,iV...RQXgXQAAAAASUVORK5CYII="]
To get only (ie) the first use of the image: parts[0];
Example:
var canvas = document.createElement('canvas'),
img { margin: 10px; }
<div id="test"></div>