error with arrays in javascript -
to understand note this; `when page loads gets area of image (width * height) , creates x,y positions positions in area.
this works fine.
when have area pos x,y , area (width * height) should pop positions first list can separate 2 areas.
little bug noticed little lines horizontal selected area , don't extend far that. believe reason instead of making clean square inside image every line offseted pixel or two.
here's video of behaviour https://youtu.be/v1b6demfxqw
so since there's positions list code created clone of array , removes positions.
var drop_boxes = $('.drop-box'); var area_grid = []; var image_width = $('.img-class')[0].naturalwidth; var image_height = $('.img-class')[0].naturalheight; drop_boxes.each(function() { var position = $(this).position(); var width = $(this).width(); var height = $(this).height(); var positions_clone = positions.slice(0); //console.log(positions_clone.length); var top_offset = parseint((position['top'] * image_width)/img_width); var left_offset = parseint((position['left'] * image_height)/img_height); position['top'] = top_offset; position['left'] = left_offset; var width_offset = parseint((width * image_width)/img_width); var height_offset = parseint((height * image_height)/img_height); var width_counter = 0; var height_counter = 0; var area = width_offset * height_offset; console.log(position); console.log(width_offset); console.log(height_offset); if (position['top'] < image_height-1 && position['left'] < image_width) { (counter = 0; counter < area; counter++) { var pos = [parseint(position['left']+width_counter), parseint(position['top']+height_counter)]; var index = positions.findindex(function(item) { // return result of comparing `data` `item` // simple implementation assumes `item`s arrays. return pos.length === item.length && item.every(function(n, i) { return n === pos[i] }); }); //console.log(pos); if (index > -1) { positions_clone.splice(index, 1); } //area_grid.push(pos); if (width_counter == width_offset) { width_counter = 0; height_counter += 1; } if (counter%100 == 0) { var percentage = math.round((counter/area)*100, 2); console.log("percentage: "+percentage+"%" + " "+counter); } width_counter += 1; } console.log(positions_clone.length); console.log(area_grid.length); areas[area_counter] = {'area': area_grid, 'positions': positions_clone}; parent.find('.area').text(area_counter); area_counter += 1; } any clues in fixing appreciated. i've showed how behaves after commenting out parts of code in video.
change
var index = positions.findindex(function(item) { to
var index = positions_clone.findindex(function(item) { because after each splice, indices of original positions doesn't change still using indices splice clone.
Comments
Post a Comment