| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- var sprite = {};
- sprite.init = function(Config) {
- var el = Config.el;
- var w = Config.width;
- var h = Config.height;
- var PL_Size = 48;
- var padding = 20;
- var MinN_X = padding + PL_Size;
- var MaxN_X = w - padding - PL_Size - PL_Size / 6;
- var MaxN_Y = padding;
- var MinN_Y = h - padding - PL_Size - PL_Size / 6;
- function RandomNum(Min, Max) {
- var Range = Max - Min;
- var Rand = Math.random();
- if(Math.round(Rand * Range) == 0) {
- return Min + 1;
- } else if(Math.round(Rand * Max) == Max) {
- return Max - 1;
- } else {
- var num = Min + Math.round(Rand * Range) - 1;
- return num;
- }
- }
- var imgSrc = Config.img();
- var X = RandomNum(MinN_X, MaxN_X);
- var Y = RandomNum(MinN_Y, MaxN_Y);
- var left_Num = -X + 10;
- 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);">';
- html += '<div style="position:relative;padding: 5px 5px;background: #fff;">';
- html += '<div style="position:relative;overflow:hidden;width:' + w + 'px;">';
- html += '<div style="position:relative;width:' + w + 'px;height:' + h + 'px;">';
- html += '<img id="scream" src="' + imgSrc + '" style="width:' + w + 'px;height:' + h + 'px;">';
- html += '<canvas id="puzzleBox" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:22;"></canvas>';
- html += '</div>';
- html += '<div class="puzzle-lost-box" style="position:absolute;width:' + w + 'px;height:' + h + 'px;top:0;left:' + left_Num + 'px;z-index:111;">';
- html += '<canvas id="puzzleShadow" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:22;"></canvas>';
- html += '<canvas id="puzzleLost" width="' + w + '" height="' + h + '" style="position:absolute;left:0;top:0;z-index:33;"></canvas>';
- html += '</div>';
- html += '<p class="ver-tips"></p>';
- html += '</div>';
- html += '</div>';
- html += '<div style="position:relative;width:' + w + 'px;margin:auto;">';
- html += '<div class="alider-prog" >';
- html += '<p id="sprite-desc">拖动左边滑块完成上方拼图</p>';
- html += '</div>';
- html += '<div class="slider-btn"></div>';
- html += '</div>';
- html += '<div class="btn-panel">';
- html += '<a title="关闭验证" class="btn-panel-common close-btn"></a><a title="刷新验证" class="btn-panel-common reset-btn"></a>';
- html += '</div>';
- html += '</div>';
- el.html(html);
- $(el).css("display", "inline-block");
- var d = PL_Size / 3;
- var c = document.getElementById("puzzleBox");
- var ctx = c.getContext("2d");
- ctx.globalCompositeOperation = "xor";
- ctx.shadowBlur = 10;
- ctx.shadowColor = "#fff";
- ctx.shadowOffsetX = 3;
- ctx.shadowOffsetY = 3;
- ctx.fillStyle = "rgba(0,0,0,0.7)";
- ctx.beginPath();
- ctx.lineWidth = "1";
- ctx.strokeStyle = "rgba(0,0,0,0)";
- ctx.moveTo(X, Y);
- ctx.lineTo(X + d, Y);
- ctx.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
- ctx.lineTo(X + 3 * d, Y);
- ctx.lineTo(X + 3 * d, Y + d);
- ctx.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
- ctx.lineTo(X + 3 * d, Y + 3 * d);
- ctx.lineTo(X, Y + 3 * d);
- ctx.closePath();
- ctx.stroke();
- ctx.fill();
- var c_l = document.getElementById("puzzleLost");
- var c_s = document.getElementById("puzzleShadow");
- var ctx_l = c_l.getContext("2d");
- var ctx_s = c_s.getContext("2d");
- var img = new Image();
- img.src = imgSrc;
- img.onload = function() {
- ctx_l.drawImage(img, 0, 0, w, h);
- }
- ctx_l.beginPath();
- ctx_l.strokeStyle = "rgba(0,0,0,0)";
- ctx_l.moveTo(X, Y);
- ctx_l.lineTo(X + d, Y);
- ctx_l.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
- ctx_l.lineTo(X + 3 * d, Y);
- ctx_l.lineTo(X + 3 * d, Y + d);
- ctx_l.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
- ctx_l.lineTo(X + 3 * d, Y + 3 * d);
- ctx_l.lineTo(X, Y + 3 * d);
- ctx_l.closePath();
- ctx_l.stroke();
- ctx_s.fill();
- ctx_l.clip();
- ctx_s.beginPath();
- ctx_s.lineWidth = "1";
- ctx_s.strokeStyle = "rgba(0,0,0,0)";
- ctx_s.moveTo(X, Y);
- ctx_s.lineTo(X + d, Y);
- ctx_s.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y);
- ctx_s.lineTo(X + 3 * d, Y);
- ctx_s.lineTo(X + 3 * d, Y + d);
- ctx_s.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d);
- ctx_s.lineTo(X + 3 * d, Y + 3 * d);
- ctx_s.lineTo(X, Y + 3 * d);
- ctx_s.closePath();
- ctx_s.stroke();
- ctx_s.shadowBlur = 10;
- ctx_s.shadowColor = "black";
- ctx_s.fill();
- var moveStart = '';
- $(".slider-btn").on('mousedown touchstart', function(e) {
- e = e || window.event;
- $(this).css({
- "background-position": "0 31.0992%"
- });
- moveStart = e.pageX || e.originalEvent.targetTouches[0].pageX;
- $("#sprite-desc").fadeOut(300);
- });
- $(window).on('mousemove', function(e) {
- e = e || window.event;
- var moveX = e.pageX;
- var d = moveX - moveStart;
- if(moveStart == '') {} else {
- if(d < 0 || d > (w - padding - PL_Size)) {} else {
- $(".slider-btn").css({
- "left": d + 'px',
- "transition": "inherit"
- });
- $("#puzzleLost").css({
- "left": d + 'px',
- "transition": "inherit"
- });
- $("#puzzleShadow").css({
- "left": d + 'px',
- "transition": "inherit"
- });
- }
- }
- })
- $(window).on('mouseup', function(e) {
- e = e || window.event;
- var moveEnd_X = e.pageX - moveStart;
- var ver_Num = X - 10;
- var deviation = 4;
- var Min_left = ver_Num - deviation;
- var Max_left = ver_Num + deviation;
- if(moveStart == '') {} else {
- if(Max_left > moveEnd_X && moveEnd_X > Min_left) {
- $(".ver-tips").css("background-color", "#5CBE71");
- $(".ver-tips").html('<span style="color:#fff">验证成功</span><span></span>');
- $(".ver-tips").addClass("slider-tips");
- $(".puzzle-lost-box").addClass("hidden");
- $("#puzzleBox").addClass("hidden");
- setTimeout(function() {
- $("#sprite-content").remove();
- $(".ver-tips").removeClass("slider-tips");
- sprite.init(Config);
- }, 2000);
- Config.success();
- } else {
- $(".ver-tips").html('<span style="color:red;"></span><span style="margin-left:4px;color:#fff">拖动滑块将悬浮图像正确拼合</span>');
- $(".ver-tips").addClass("slider-tips");
- $(".ver-tips").css("background-color", "#DD705b");
- setTimeout(function() {
- $(".ver-tips").removeClass("slider-tips");
- }, 2000);
- if(Config.error) {
- Config.error();
- }
- }
- }
- setTimeout(function() {
- $(".slider-btn").css({
- "left": '0',
- "transition": "left 0.5s"
- });
- $("#puzzleLost").css({
- "left": '0',
- "transition": "left 0.5s"
- });
- $("#puzzleShadow").css({
- "left": '0',
- "transition": "left 0.5s"
- });
- }, 1000);
- $(".slider-btn").css({
- "background-position": "0 11.79625%"
- });
- moveStart = '';
- setTimeout(function() {
- $("#sprite-desc").fadeIn(300);
- }, 1500)
- return false;
- })
- $(".reset-btn").on("click", function() {
- sprite.init(Config);
- })
- $(".close-btn").on("click", function() {
- Config.close();
- $(el).html("");
- })
- }
|