75bae7f0b5e7927401f23f959a5bf7ac39cfd9fa.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file 主要功能
  3. * @author clmtulip(车丽美, clmtulip@gmail.com) liyong(liyong1239@163.com)
  4. */
  5. define(function (require) {
  6. function ZeroArray(option) {
  7. this.defaultOption = {type: 'RECT'};
  8. this._init(option);
  9. }
  10. ZeroArray.prototype = {
  11. RECT: '_calculateRect',
  12. _init: function (option) {
  13. this._initOption(option);
  14. this._initCanvas();
  15. },
  16. _initOption: function (option) {
  17. for (k in option) {
  18. this.defaultOption[k] = option[k];
  19. }
  20. },
  21. _initCanvas: function () {
  22. var canvas = document.createElement("canvas");
  23. canvas.width = 1;
  24. canvas.height = 1;
  25. var ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2);
  26. canvas.width = this.defaultOption.width;
  27. canvas.height = this.defaultOption.height;
  28. if (canvas.getContext) {
  29. var ctx = canvas.getContext('2d');
  30. }
  31. this.canvas = canvas;
  32. this.ctx = ctx;
  33. this.ratio = ratio;
  34. },
  35. /**执行计算, 并返回
  36. *
  37. * @param callback
  38. * 返回 {initarr, area, maxHit, maxWit} 给callback
  39. */
  40. calculate: function (callback, callbackObj) {
  41. var calType = this.defaultOption.type,
  42. calmethod = this[calType];
  43. this[calmethod].call(this, callback, callbackObj);
  44. },
  45. /**
  46. * callback 函数的 正确执行
  47. * @param result 计算后的结果,{initarr, area, maxHit, maxWit}
  48. * @param callback 计算成功之后的回调函数
  49. * @param callbackObj 回调函数的执行作用域
  50. * @private
  51. */
  52. _calculateReturn: function (result, callback, callbackObj) {
  53. callback.call(callbackObj, result);
  54. },
  55. _calculateRect: function (callback, callbackObj) {
  56. var result = {},
  57. width = this.defaultOption.width >> 5 << 5,
  58. height = this.defaultOption.height;
  59. // 初始化数组
  60. result.initarr = this._rectZeroArray(width * height);
  61. // 总面积
  62. result.area = width * height;
  63. // 最大高度
  64. result.maxHit = height;
  65. // 最大宽度
  66. result.maxWit = width;
  67. // 边界
  68. result.imgboard = this._rectBoard(width, height);
  69. this._calculateReturn(result, callback, callbackObj);
  70. },
  71. _rectBoard: function (width, height) {
  72. var row = [];
  73. for (var i = 0; i < height; i++) {
  74. row.push({
  75. y: i,
  76. start: 0,
  77. end: width
  78. })
  79. }
  80. var cloumn = [];
  81. for (var i = 0; i < width; i++) {
  82. cloumn.push({
  83. x: i,
  84. start: 0,
  85. end: height
  86. })
  87. }
  88. return {row: row, cloumn: cloumn};
  89. },
  90. _rectZeroArray: function (num) {
  91. var a = [],
  92. n = num,
  93. i = -1;
  94. while (++i < n) a[i] = 0;
  95. return a;
  96. }
  97. };
  98. return ZeroArray;
  99. });