如何提升Javascript Canvas桶填充

时间:2013-10-04 23:55:44

标签: javascript image performance canvas html5-canvas

有没有人知道如何优化我的Javascript桶填充的性能?我首先尝试使用递归函数,但它遇到了堆栈溢出。目前的代码在Chrome中运行良好,但Firefox将停止执行。 : - (

var canvas = {
  height: 300,
  width: 300
};

var color;
var imgArray = [];
var imgdata;

$(document).ready(function() {
  canvas.obj = document.createElement('canvas');

  $('body').append(canvas.obj);

  $(canvas.obj).attr('height', canvas.height);
  $(canvas.obj).attr('width', canvas.width);

  var context = canvas.obj.getContext('2d');
  var imageObj = new Image();

  imageObj.onload = function() {
    context.drawImage(imageObj, 0, 0, canvas.height, canvas.width);
    imgdata = context.getImageData(0, 0, canvas.width, canvas.height).data;
  };

  imageObj.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4Xu2dC5wcVZX/Tz8mIQ8e0UAIIkGBSNA/CIg8REURBOQvrq4PIPpXQdd11+cqKrIfV5f1gS9cd9cPj8RVCAgrPvD/BxIQQkBAQoJBMCGQkGAgLyAJeUwyM939P+fce6ure2bS3VW3qru6f4Vxembq1r31u1XfOffcc8/NVfggHFAACoyogLwcuajahAu7z/q1zP+XJ4p18aiN6sByrEOJNSlw0yqsi+hdoTJ/ZY3qjhyA1YEdiCa1V4EAKvLm2JdmRGpZ8Ehr6+AjdkAuZwtVSgypAvH/61Hgl1FeTDkiw7C9CnmrXTQRPfQo51junNGE9awwxOqhBWB5kx4XyqICoxk5btiR05dp+F/60e5VBywMqgBEgWU1xD8Xq4r/yS/LJf5Y6HlgqY5WowBeqhFDnb865ju9AawsvmVoc0IK2L/0DKiqheR+ZqtkK0DfojrTqMaiqmtdiaEnw52ff2wi5UtsRXDZvFgTOXhjKF+hmVftUE2cZaUMs/rWW6AAVkKPPi6bEQXqh3INhmniWxGLq8L/iR3VcEin/hke9vDXay+YwJ8LVMixtSWWWMV4bXr5qDD8Z87axqoy1J31GQwJhw+ZAaxeflpw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AllVAMDKas+h3VCgBxUAsHqw03HLUCCrCgBYWe05tBsK9KAC/x/N+TpdkoaNyAAAAABJRU5ErkJggg==';


  $('canvas').click(function(e) {
    color = getRGBA(getPos(e.pageX, e.pageY));
    addToImageArray(e.pageX, e.pageY, true);
    checkImage();

    $('#currentcolor').css('background-color', 'rgba('+color.r+','+color.g+','+color.b+','+color.a+')');

    for (var y = 0; y < canvas.height; y++) {
      for (var x = 0; x < canvas.width; x++) {
        var posarray = searchArray(x, y);

        if (posarray.inarray && posarray.inrange) {
          var poscolor = getRGBA(getPos(x, y));

          $('#imgholder').append('<div style="height: 1px; width: 1px; background-color: rgba('+poscolor.r+','+poscolor.g+','+poscolor.b+','+poscolor.a+'); display:inline-block; position:absolute; top: '+y+'px; left: '+x+'px"></div>');
        }
      }
    }

  });
});

function checkImage() {
  var changed = true;
  var i = 0;

  console.log('start');

  while (changed) {
    changed = false;

    for (var y = 0; y < canvas.height; y++) {
      for (var x = 0; x < canvas.width; x++) {
        var posarray = searchArray(x, y);

        if (posarray.inarray && posarray.inrange) {
          var pos = {
            top: {
              x: x,
              y: y-1
            },
            bottom: {
              x: x,
              y: y+1
            },
            left: {
              x: x-1,
              y: y
            },
            right: {
              x: x+1,
              y: y
            }
          };

          i++;

          //top
          var topposarray = searchArray(pos.top.x, pos.top.y);

          if (pos.top.y >= 0 && !topposarray.inarray) {
            if (inrange(color, getRGBA(getPos(pos.top.x, pos.top.y)))) {
              addToImageArray(pos.top.x, pos.top.y, true);
              changed = true;
            }
            else {
              addToImageArray(pos.top.x, pos.top.y, false);
            }
          }

          //bottom
          var bottomposarray = searchArray(pos.bottom.x, pos.bottom.y);

          if (pos.top.y <= canvas.height && !bottomposarray.inarray) {
            if (inrange(color, getRGBA(getPos(pos.bottom.x, pos.bottom.y)))) {
              addToImageArray(pos.bottom.x, pos.bottom.y, true);
              changed = true;
            }
            else {
              addToImageArray(pos.bottom.x, pos.bottom.y, false);
            }
          }

          //left
          var leftposarray = searchArray(pos.left.x, pos.left.y);

          if (pos.top.x >= 0 && !leftposarray.inarray) {
            if (inrange(color, getRGBA(getPos(pos.left.x, pos.left.y)))) {
              addToImageArray(pos.left.x, pos.left.y, true);
              changed = true;
            }
            else {
              addToImageArray(pos.left.x, pos.left.y, false);
            }
          }

          //right
          var rightposarray = searchArray(pos.right.x, pos.right.y);

          if (pos.top.x <= canvas.width && !rightposarray.inarray) {
            if (inrange(color, getRGBA(getPos(pos.right.x, pos.right.y)))) {
              addToImageArray(pos.right.x, pos.right.y, true);
              changed = true;
            }
            else {
              addToImageArray(pos.right.x, pos.right.y, false);
            }
          }
        }
      }
    }
  }

  console.log('done');
  $('#currentcolor').html('loops: '+ i);
}

function crange(color1, color2) {
  return (color1 > color2-10 && color1 < color2+10);
}

function inrange(color1, color2) {
  if (crange(color1.r,color2.r) && crange(color1.g,color2.g) && crange(color1.b,color2.b)) {
    return true;
  }
  else {
    return false;
  }
}

function searchArray(x, y) {
  if (imgArray[y] !== undefined && imgArray[y][x] !== undefined) {
    return {
      inarray: true,
      inrange: imgArray[y][x]
    };
  }
  else {
    return {
      inarray: false
    };
  }
}

function addToImageArray(x, y, inrange) {
  if (imgArray[y] === undefined) {
    imgArray[y] = [];
  }

  imgArray[y][x] = inrange;
}

function getRGBA(pos) {
  var color = {
    r: imgdata[pos],
    g: imgdata[pos+1],
    b: imgdata[pos+2],
    a: imgdata[pos+3]
  };

  return color;
}

function getPos(x, y) {
  var pos = (y * 4)* canvas.width;
  pos += (x*4);

  return pos;
}

Test code http://jsfiddle.net/XzMxx/

提前致谢!

0 个答案:

没有答案