/*! tncode 1.2 //@ https://github.com/binwind8/tncode */ if(!document.getelementbyclassname){ function hasclass(elem, cls) { cls = cls || ''; if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时,返回false var ret = new regexp(' ' + cls + ' ').test(' ' + elem.classname + ' '); return ret; } document.getelementbyclassname = function(classname,index){ var nodes=document.getelementsbytagname("*");//获取页面里所有元素,因为他会匹配全页面元素,所以性能上有缺陷,但是可以约束他的搜索范围; var arr=[];//用来保存符合的classname; for(var i=0;imax_off){ offset = max_off; } var obj = document.getelementbyclassname('slide_block'); obj.style.csstext = "transform: translate("+offset+"px, 0px)"; tncode._mark_offset = offset/max_off*(tncode._img_w-tncode._mark_w); tncode._draw_bg(); tncode._draw_mark(); }, _block_on_end:function(e){ if(!tncode._doing)return true; e.preventdefault(); var theevent = window.event || e; if(theevent.touches){ theevent = theevent.touches[0]; } console.log("_block_on_end"); tncode._is_moving = false; tncode._send_result(); }, _send_result:function(){ var haddle = {success:tncode._send_result_success,failure:tncode._send_result_failure}; tncode._result = false; var re = new _ajax(); re.request('get',tncode.checkurl +'&tn_r='+tncode._mark_offset,haddle); }, _send_result_success:function(responsetext,responsexml){ tncode._doing = false; if(responsetext=='ok'){ tncode._tncode.innerhtml = '验证成功!'; tncode._showmsg('验证成功!',1); tncode._result = true; document.getelementbyclassname('hgroup').style.display="block"; settimeout(tncode.hide,3000); if(tncode._onsuccess){ tncode._onsuccess(); } }else{ tncode._tncode.innerhtml = '验证失败?'; var obj = document.getelementbyid('tncode_div'); addclass( obj,'dd'); settimeout(function(){ removeclass( obj,'dd'); },200); tncode._result = false; tncode._showmsg('验证失败!'); tncode._err_c++; if(true || tncode._err_c>5){ settimeout(function(){ tncode.refresh(); },1000); } } }, _send_result_failure:function(xhr,status){ }, _draw_fullbg:function(){ var canvas_bg = document.getelementbyclassname('tncode_canvas_bg'); var ctx_bg = canvas_bg.getcontext('2d'); ctx_bg.drawimage(tncode._img, 0, tncode._img_h*2, tncode._img_w, tncode._img_h, 0, 0, tncode._img_w, tncode._img_h); }, _draw_bg:function(){ if(tncode._is_draw_bg){ return; } tncode._is_draw_bg = true; var canvas_bg = document.getelementbyclassname('tncode_canvas_bg'); var ctx_bg = canvas_bg.getcontext('2d'); ctx_bg.drawimage(tncode._img, 0, 0, tncode._img_w, tncode._img_h, 0, 0, tncode._img_w, tncode._img_h); }, _draw_mark:function(){ var canvas_mark = document.getelementbyclassname('tncode_canvas_mark'); var ctx_mark = canvas_mark.getcontext('2d'); //清理画布 ctx_mark.clearrect(0,0,canvas_mark.width,canvas_mark.height); ctx_mark.drawimage(tncode._img, 0, tncode._img_h, tncode._mark_w,tncode._img_h,tncode._mark_offset,0,tncode._mark_w, tncode._img_h); var imagedata = ctx_mark.getimagedata(0, 0, tncode._img_w, tncode._img_h); // 获取画布的像素信息 // 是一个一维数组,包含以 rgba 顺序的数据,数据使用 0 至 255(包含)的整数表示 // 如:图片由两个像素构成,一个像素是白色,一个像素是黑色,那么 data 为 // [255,255,255,255,0,0,0,255] // 这个一维数组可以看成是两个像素中rbga通道的数组的集合即: // [r,g,b,a].concat([r,g,b,a]) var data = imagedata.data; //alert(data.length/4); var x = tncode._img_h,y=tncode._img_w; for(var j = 0; j < x; j++) { var ii = 1,k1=-1; for(var k=0;k=0&&k>k1;){ // 得到 rgba 通道的值 var i = (j*y+k)*4; k+=ii; var r = data[i] , g = data[i+1] , b = data[i+2]; // 我们从最下面那张颜色生成器中可以看到在图片的右上角区域,有一小块在 // 肉眼的观察下基本都是白色的,所以我在这里把 rgb 值都在 245 以上的 // 的定义为白色 // 大家也可以自己定义的更精确,或者更宽泛一些 if((r+g+b)<200) data[i+3] = 0; else{ var arr_pix = [1,-5]; var arr_op = [250,0]; for (var i =1; i opacity) { v -= avg; setopacity(ele, v); } else { setopacity(ele, 0); if(status==0){ tncode._reset(); } clearinterval(timer); } }, 100); } } function fadein(ele, opacity, speed) { if (ele) { var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity; v < 1 && (v = v * 100); var count = speed / 1000; var avg = count < 2 ? (opacity / count) : (opacity / count - 1); var timer = null; timer = setinterval(function() { if (v < opacity) { v += avg; setopacity(ele, v); } else { clearinterval(timer); settimeout(function() {fadeout(obj, 0, 6000);},1000); } }, 100); } } fadein(obj, 80, 4000); }, _html:function(){ var d = document.getelementbyid('tncode_div_bg'); if(d)return; var html = '
加载中
向右滑动填充拼图
'; var bo = document.getelementsbyclassname('tncode_box'); appendhtml(bo[0],html); }, _currenturl:function(){ var list = document.getelementsbytagname('script'); for (var i in list) { var d=list[i]; if(d.src.indexof('tn_code')!==-1){//js文件名一定要带这个字符 var arr = d.src.split('tn_code'); return arr[0]; } } }, refresh:function(){ var issupportwebp = !![].map && document.createelement('canvas').todataurl('image/webp').indexof('data:image/webp') == 0; var _this = this; tncode._err_c = 0; tncode._is_draw_bg = false; tncode._result = false; tncode._img_loaded = false; var obj = document.getelementbyclassname('tncode_canvas_bg'); obj.style.display="none"; obj = document.getelementbyclassname('tncode_canvas_mark'); obj.style.display="none"; tncode._img = new image(); var img_url = tncode.tncodeurl+"&t="+math.random(); if(!issupportwebp){//浏览器不支持webp //img_url+="&nowebp=1"; } tncode._img.src = img_url; tncode._img.onload = function(){ tncode._draw_fullbg(); var canvas_mark = document.getelementbyclassname('tncode_canvas_mark'); var ctx_mark = canvas_mark.getcontext('2d'); //清理画布 ctx_mark.clearrect(0,0,canvas_mark.width,canvas_mark.height); tncode._img_loaded = true; obj = document.getelementbyclassname('tncode_canvas_bg'); obj.style.display=""; obj = document.getelementbyclassname('tncode_canvas_mark'); obj.style.display=""; }; //alert("hong kong forharvest technology and culture development co. limited".length); obj = document.getelementbyclassname('slide_block'); obj.style.csstext = "transform: translate(0px, 0px)"; obj = document.getelementbyclassname('slide_block_text'); obj.style.display="block"; }, init:function(){ var _this = this; if(!tncode._img){ tncode._html(); var obj = document.getelementbyclassname('slide_block'); tncode._bind(obj,'mousedown',_this._block_start_move); tncode._bind(document,'mousemove',_this._block_on_move); tncode._bind(document,'mouseup',_this._block_on_end); tncode._bind(obj,'touchstart',_this._block_start_move); tncode._bind(document,'touchmove',_this._block_on_move); tncode._bind(document,'touchend',_this._block_on_end); var obj = document.getelementbyclassname('tncode_close'); tncode._bind(obj,'touchstart',_this.hide); tncode._bind(obj,'mouseover',_this.hide); var obj = document.getelementbyclassname('tncode_refresh'); tncode._bind(obj,'touchstart',_this.refresh); tncode._bind(obj,'mouseover',_this.refresh); var objs = document.getelementbyclassname('tncode',-1); for (var i in objs) { var o = objs[i]; o.innerhtml = '向右滑动填充拼图'; tncode._bind(o,'touchstart',_this.show); tncode._bind(o,'mouseover',_this.show); } } }, result:function(){ return tncode._result; }, onsuccess:function(fn,opt){ tncode._onsuccess = fn; tncode.tncodeurl = opt.tncodeurl; tncode.checkurl = opt.checkurl; } }; var $tn = tncode; var _old_onload = window.onload; window.onload = function(){ if(typeof _old_onload == 'function'){ _old_onload(); } tncode.init(); };