Sprite.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var sprite = {};
  2. sprite.init = function(Config) {
  3. var el = Config.el;
  4. var w = Config.width;
  5. var h = Config.height;
  6. var PL_Size = 48;
  7. var padding = 20;
  8. var MinN_X = padding + PL_Size;
  9. var MaxN_X = w - padding - PL_Size - PL_Size / 6;
  10. var MaxN_Y = padding;
  11. var MinN_Y = h - padding - PL_Size - PL_Size / 6;
  12. function RandomNum(Min, Max) {
  13. var Range = Max - Min;
  14. var Rand = Math.random();
  15. if(Math.round(Rand * Range) == 0) {
  16. return Min + 1;
  17. } else if(Math.round(Rand * Max) == Max) {
  18. return Max - 1;
  19. } else {
  20. var num = Min + Math.round(Rand * Range) - 1;
  21. return num;
  22. }
  23. }
  24. var imgSrc = Config.img();
  25. var X = RandomNum(MinN_X, MaxN_X);
  26. var Y = RandomNum(MinN_Y, MaxN_Y);
  27. var left_Num = -X + 10;
  28. var html = '<div id="sprite-content" style="background-color:#fff;position:relative;border: 1px solid #d0d0d0;box-shadow: 0 1px 8px rgba(128,128,128,0.3);">';
  29. html += '<div style="position:relative;padding: 5px 5px;background: #fff;">';
  30. html += '<div style="position:relative;overflow:hidden;width:' + w + 'px;">';
  31. html += '<div style="position:relative;width:' + w + 'px;height:' + h + 'px;">';
  32. html += '<img id="scream" src="' + imgSrc + '" style="width:' + w + 'px;height:' + h + 'px;">';
  33. html += '<canvas id="puzzleBox" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:22;"></canvas>';
  34. html += '</div>';
  35. html += '<div class="puzzle-lost-box" style="position:absolute;width:' + w + 'px;height:' + h + 'px;top:0;left:' + left_Num + 'px;z-index:111;">';
  36. html += '<canvas id="puzzleShadow" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:22;"></canvas>';
  37. html += '<canvas id="puzzleLost" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:33;"></canvas>';
  38. html += '</div>';
  39. html += '<p class="ver-tips"></p>';
  40. html += '</div>';
  41. html += '</div>';
  42. html += '<div style="position:relative;width:' + w + 'px;margin:auto;">';
  43. html += '<div class="alider-prog" >';
  44. html += '<p id="sprite-desc">拖动左边滑块完成上方拼图</p>';
  45. html += '</div>';
  46. html += '<div class="slider-btn"></div>';
  47. html += '</div>';
  48. html += '<div class="btn-panel">';
  49. html += '<a title="关闭验证" class="btn-panel-common close-btn"></a><a title="刷新验证" class="btn-panel-common reset-btn"></a>';
  50. html += '</div>';
  51. html += '</div>';
  52. el.html(html);
  53. $(el).css("display", "inline-block");
  54. var d = PL_Size / 3;
  55. var c = document.getElementById("puzzleBox");
  56. var ctx = c.getContext("2d");
  57. ctx.globalCompositeOperation = "xor";
  58. ctx.shadowBlur = 10;
  59. ctx.shadowColor = "#fff";
  60. ctx.shadowOffsetX = 3;
  61. ctx.shadowOffsetY = 3;
  62. ctx.fillStyle = "rgba(0,0,0,0.7)";
  63. ctx.beginPath();
  64. ctx.lineWidth = "1";
  65. ctx.strokeStyle = "rgba(0,0,0,0)";
  66. ctx.moveTo(X, Y);
  67. ctx.lineTo(X + d, Y);
  68. ctx.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
  69. ctx.lineTo(X + 3 * d, Y);
  70. ctx.lineTo(X + 3 * d, Y + d);
  71. ctx.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
  72. ctx.lineTo(X + 3 * d, Y + 3 * d);
  73. ctx.lineTo(X, Y + 3 * d);
  74. ctx.closePath();
  75. ctx.stroke();
  76. ctx.fill();
  77. var c_l = document.getElementById("puzzleLost");
  78. var c_s = document.getElementById("puzzleShadow");
  79. var ctx_l = c_l.getContext("2d");
  80. var ctx_s = c_s.getContext("2d");
  81. var img = new Image();
  82. img.src = imgSrc;
  83. img.onload = function() {
  84. ctx_l.drawImage(img, 0, 0, w, h);
  85. }
  86. ctx_l.beginPath();
  87. ctx_l.strokeStyle = "rgba(0,0,0,0)";
  88. ctx_l.moveTo(X, Y);
  89. ctx_l.lineTo(X + d, Y);
  90. ctx_l.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
  91. ctx_l.lineTo(X + 3 * d, Y);
  92. ctx_l.lineTo(X + 3 * d, Y + d);
  93. ctx_l.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
  94. ctx_l.lineTo(X + 3 * d, Y + 3 * d);
  95. ctx_l.lineTo(X, Y + 3 * d);
  96. ctx_l.closePath();
  97. ctx_l.stroke();
  98. ctx_s.fill();
  99. ctx_l.clip();
  100. ctx_s.beginPath();
  101. ctx_s.lineWidth = "1";
  102. ctx_s.strokeStyle = "rgba(0,0,0,0)";
  103. ctx_s.moveTo(X, Y);
  104. ctx_s.lineTo(X + d, Y);
  105. ctx_s.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
  106. ctx_s.lineTo(X + 3 * d, Y);
  107. ctx_s.lineTo(X + 3 * d, Y + d);
  108. ctx_s.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
  109. ctx_s.lineTo(X + 3 * d, Y + 3 * d);
  110. ctx_s.lineTo(X, Y + 3 * d);
  111. ctx_s.closePath();
  112. ctx_s.stroke();
  113. ctx_s.shadowBlur = 10;
  114. ctx_s.shadowColor = "black";
  115. ctx_s.fill();
  116. var moveStart = '';
  117. $(".slider-btn").on('mousedown touchstart', function(e) {
  118. e = e || window.event;
  119. $(this).css({
  120. "background-position": "0 31.0992%"
  121. });
  122. moveStart = e.pageX || e.originalEvent.targetTouches[0].pageX;
  123. $("#sprite-desc").fadeOut(300);
  124. });
  125. $(window).on('mousemove', function(e) {
  126. e = e || window.event;
  127. var moveX = e.pageX;
  128. var d = moveX - moveStart;
  129. if(moveStart == '') {} else {
  130. if(d < 0 || d > (w - padding - PL_Size)) {} else {
  131. $(".slider-btn").css({
  132. "left": d + 'px',
  133. "transition": "inherit"
  134. });
  135. $("#puzzleLost").css({
  136. "left": d + 'px',
  137. "transition": "inherit"
  138. });
  139. $("#puzzleShadow").css({
  140. "left": d + 'px',
  141. "transition": "inherit"
  142. });
  143. }
  144. }
  145. })
  146. $(window).on('mouseup', function(e) {
  147. e = e || window.event;
  148. var moveEnd_X = e.pageX - moveStart;
  149. var ver_Num = X - 10;
  150. var deviation = 4;
  151. var Min_left = ver_Num - deviation;
  152. var Max_left = ver_Num + deviation;
  153. if(moveStart == '') {} else {
  154. if(Max_left > moveEnd_X && moveEnd_X > Min_left) {
  155. $(".ver-tips").css("background-color", "#5CBE71");
  156. $(".ver-tips").html('<span style="color:#fff">验证成功</span><span></span>');
  157. $(".ver-tips").addClass("slider-tips");
  158. $(".puzzle-lost-box").addClass("hidden");
  159. $("#puzzleBox").addClass("hidden");
  160. setTimeout(function() {
  161. $("#sprite-content").remove();
  162. $(".ver-tips").removeClass("slider-tips");
  163. sprite.init(Config);
  164. }, 2000);
  165. Config.success();
  166. } else {
  167. $(".ver-tips").html('<span style="color:red;"></span><span style="margin-left:4px;color:#fff">拖动滑块将悬浮图像正确拼合</span>');
  168. $(".ver-tips").addClass("slider-tips");
  169. $(".ver-tips").css("background-color", "#DD705b");
  170. setTimeout(function() {
  171. $(".ver-tips").removeClass("slider-tips");
  172. }, 2000);
  173. if(Config.error) {
  174. Config.error();
  175. }
  176. }
  177. }
  178. setTimeout(function() {
  179. $(".slider-btn").css({
  180. "left": '0',
  181. "transition": "left 0.5s"
  182. });
  183. $("#puzzleLost").css({
  184. "left": '0',
  185. "transition": "left 0.5s"
  186. });
  187. $("#puzzleShadow").css({
  188. "left": '0',
  189. "transition": "left 0.5s"
  190. });
  191. }, 1000);
  192. $(".slider-btn").css({
  193. "background-position": "0 11.79625%"
  194. });
  195. moveStart = '';
  196. setTimeout(function() {
  197. $("#sprite-desc").fadeIn(300);
  198. }, 1500)
  199. return false;
  200. })
  201. $(".reset-btn").on("click", function() {
  202. sprite.init(Config);
  203. })
  204. $(".close-btn").on("click", function() {
  205. Config.close();
  206. $(el).html("");
  207. })
  208. }