Erasure.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var s =
  5. (this && this.__decorate) ||
  6. function(t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", { value: !0 });
  17. const { ccclass: o, property: a, menu: n, executeInEditMode: r } = cc._decorator;
  18. e = class extends cc.Component {
  19. constructor() {
  20. super(...arguments),
  21. (this.mask = null),
  22. (this.invokeEvent = []),
  23. (this.scrapteRadius = 35),
  24. (this.scrapteArea = 0.5),
  25. (this.achieveNum = 0),
  26. (this.pixelNum = 0);
  27. }
  28. onLoad() {
  29. this.maskGraphics = this.mask._graphics;
  30. }
  31. onEnable() {
  32. this.getInitNum(),
  33. (this.pixelNum = 0),
  34. (this.achieveNum = 0),
  35. (this.achieveNum = this.scrapteArea * this.initPixel()),
  36. this.node.on(cc.Node.EventType.TOUCH_START, this.onEventStart, this),
  37. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onEventMove, this),
  38. this.node.on(cc.Node.EventType.TOUCH_END, this.onEventEnd, this);
  39. }
  40. onDisable() {
  41. this.node.off(cc.Node.EventType.TOUCH_START, this.onEventStart, this),
  42. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onEventMove, this),
  43. this.node.off(cc.Node.EventType.TOUCH_END, this.onEventEnd, this);
  44. }
  45. onEventStart(t) {
  46. this.comFun(this.node.convertToNodeSpaceAR(t.touch.getLocation())), this.checkScrape();
  47. }
  48. onEventMove(t) {
  49. this.comFun(this.node.convertToNodeSpaceAR(t.touch.getLocation())), this.checkScrape();
  50. }
  51. onEventEnd(t) {
  52. this.comFun(this.node.convertToNodeSpaceAR(t.touch.getLocation())), this.checkScrape();
  53. }
  54. addCircle(t) {
  55. var e = this.maskGraphics,
  56. i = cc.color(0, 0, 0, 255);
  57. e.ellipse(t.x, t.y, this.scrapteRadius + 10, this.scrapteRadius + 10),
  58. (e.lineWidth = 2),
  59. (e.fillColor = i),
  60. e.fill();
  61. }
  62. checkScrape() {
  63. this.achieveNum < this.pixelNum &&
  64. (cc.Component.EventHandler.emitEvents(this.invokeEvent, this),
  65. cc.log("已经刮完图层"),
  66. (this.mask.enabled = !1));
  67. }
  68. comFun(t) {
  69. this.checkPixelPiont(t), this.addCircle(t);
  70. }
  71. getInitNum() {
  72. (this.pixelNum = 0), (this.achieveNum = this.scrapteArea * this.initPixel());
  73. }
  74. initPixel() {
  75. var t = this.node.width,
  76. e = this.node.height;
  77. let i = (this.node.width = t) / 2,
  78. s = (this.node.height = e) / 2,
  79. o = -s,
  80. a = o;
  81. var n = [],
  82. r = 2 * this.scrapteRadius,
  83. l = 2 * this.scrapteRadius;
  84. for (let t = -i; t <= i; t += r)
  85. for (o = a; o <= s; o += l) {
  86. var h = [t, o];
  87. (h.isTouch = !0), n.push(h);
  88. }
  89. return (this.pixelPiont = n).length;
  90. }
  91. checkPixelPiont(t) {
  92. var e,
  93. i,
  94. s,
  95. o = this.pixelPiont;
  96. for (s in o)
  97. if (
  98. ((e = Math.abs(t.x - o[s][0])),
  99. (i = Math.abs(t.y - o[s][1])),
  100. e <= this.scrapteRadius && i <= this.scrapteRadius && o[s].isTouch)
  101. )
  102. return (o[s].isTouch = !1), void this.pixelNum++;
  103. }
  104. };
  105. s([a(cc.Mask)], e.prototype, "mask", void 0),
  106. s([a({ type: cc.Component.EventHandler })], e.prototype, "invokeEvent", void 0),
  107. s([a({})], e.prototype, "scrapteRadius", void 0),
  108. s([a({})], e.prototype, "scrapteArea", void 0),
  109. (e = s([o, r], e)),
  110. (i.default = e);