javascript - image slicing, only saves (via Filesaver.js) last slice in sequence -


hi i'm writing function break large image individual smaller tiles, , save tiles locally via filesaver.js. however, reason, for-loop keeps writing last tile in. example, if have image , break 4 blocks, , decide write out 2 blocks in first row, both saved image tiles display 2nd image tile.

i write out block number image name (testimage_0.jpg & testimage_1.jpg), both tile image names testimage_1.jpg. have no clue why!, tried context.clearrect, no success. can please me out here silly problem? here function:

var canvas = document.createelement('canvas');  var tilewidthpx = parseint(document.getelementbyid("horizppt").value); var tileheightpx = parseint(document.getelementbyid("vertppt").value); canvas.setattribute('width', tilewidthpx); canvas.setattribute('height', tileheightpx);  for(var = 0; < 2; i++) {        var context = canvas.getcontext('2d');     context.clearrect(0,0,canvas.width,canvas.height);     context.drawimage(coverageimg,i*tilewidthpx,0,tilewidthpx,tileheightpx,0,0,tilewidthpx,tileheightpx);       canvas.toblob(function(blob){saveas(blob,'testimage_'+i+'.jpg');}, "image/jpg");      alert('done: '+i); } 

even though alert displays correct 'i' value of loop... please, anybody, javascript async-problem i'm missing (also converted algorithm promise, didn't solve anything)

so javascript's async functionality made weird. got tile generating algorithm correct creating repetitive function , adding delays (important) in function, here. had pass row , column values correct template position generated. here's resulting code:

    var canvas = document.createelement('canvas');      var tilewidthpx = parseint(document.getelementbyid("horizppt").value);     var tileheightpx = parseint(document.getelementbyid("vertppt").value);     canvas.setattribute('width', tilewidthpx);     canvas.setattribute('height', tileheightpx);      var context = canvas.getcontext('2d');            var col = 0; //variable columns     var row = 0;     var colcount = 2;     var rowcount = 2;     loop(col,row,colcount,rowcount);    //call delayed loop first time     function loop (col,row,colcount,rowcount)   //repetitive function     {             settimeout(function ()          {                 context.beginpath();             context.drawimage(coverageimg,col*tilewidthpx,row*tileheightpx,tilewidthpx,tileheightpx,0,0,tilewidthpx,tileheightpx);              canvas.toblob(function(blob){saveas(blob,'testimage_c'+col+'_r'+row+'.png');}, "image/png");             context.closepath();             context.clearrect(0,0,canvas.width,canvas.height);                col++;                if(col == colcount)             {                 col = 0;                 row++;             }             if(row < rowcount)                                   loop(col,row,colcount,rowcount);  //repeat function while below col threshold              }.bind(this), 2000) //function delayed 2 seconds*/     } 

hope helps whomever gets stuck same annoying problem...


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -