全屏js头像上传插件源码高清版

本文实例为大家分享了全屏js头像上传插件源码,供大家参考,具体内容如下

index.html

<!DOCTYPE html> 

<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title>ccp</title>
  <link href="Content/ccp.css" rel="stylesheet" />
  <script src="Scripts/cxc.js"></script>
  <script src="Scripts/ccp.js"></script>
  <script src="Scripts/fun.js"></script>
</head>
<body>
  <div id="SelectPicture">选 择 图 片</div> 

  <div id="pre">
    <div id="prea">
      <div id="ctna">
        <img id="imga"/>
      </div>
    </div>
    <div id="preb">
      <div id="ctnb">
        <img id="imgb"/>
      </div>
    </div>
    <div id="prec">
      <div id="ctnc">
        <img id="imgc"/>
      </div>
    </div>
  </div> 

  <div id="ccp">
    <div id="ctn">
      <img id="fixed_img" />
      <div id="varied_div">
        <img id="varied_div_img" />
        <i id="TopLeft"></i>
        <i id="TopRight"></i>
        <i id="BottomRight"></i>
        <i id="BottomLeft"></i>
      </div>
    </div>
    <div id="black_cover"></div>
  </div> 

  <div id="bt">
    <div id="Y_OUT">
      <div id="Y">Y</div>
    </div>
    <div id="N_OUT">
      <div id="N">N</div>
    </div>
  </div>
</body>
</html> 

cxc.js

/* cxc.js 频繁操作公共接口 */
var $ = function (id) {
  return document.getElementById(id);
};  //通过id获取dom对象
var A = function (msg) {
  alert(msg);
};  //alert的简写
var EmptyFun = function () {
};  // 空方法
var setL = function (dom, L) {
  dom.style.left = L + "px";
};  // 设置 dom 的 left
var setT = function (dom, T) {
  dom.style.top = T + "px";
};  // 设置 dom 的 top
var setLT = function (dom, L, T) {
  dom.style.left = L + "px";
  dom.style.top = T + "px";
};  //同时设置 dom 的 left top
var getLT = function (dom) {
  return [parseInt(dom.style.left), parseInt(dom.style.top)];
};  //  返回dom的left和top值,类型为整型数组[int,int]
var setW = function (W) {
  dom.style.width = W + "px";
};   // 设置 dom 的 width
var setH = function (H) {
  dom.style.height = H + "px";
};   // 设置 dom 的 height
var setWH = function (dom, W, H) {
  dom.style.width = W + "px";
  dom.style.height = H + "px";
};   //同时设置 dom 的 width height
var getWH = function (dom) {
  return [parseInt(dom.style.width), parseInt(dom.style.height)];
}; //  返回dom的 width 和 height 值,类型为整型数组[int,int]
var setLTWH = function (dom, L, T, W, H) {
  dom.style.left = L + "px";
  dom.style.top = T + "px";
  dom.style.width = W + "px";
  dom.style.height = H + "px";
};  //同时设置 dom 的 left top width height
var setRTWH = function (dom, R, T, W, H) {
  dom.style.right = R + "px";
  dom.style.top = T + "px";
  dom.style.width = W + "px";
  dom.style.height = H + "px";
};  //同时设置 dom 的 left top width height
var getLTWH = function (dom) {
  return [parseInt(dom.style.left), parseInt(dom.style.top), parseInt(dom.style.width), parseInt(dom.style.height)]
};   // 返回dom的 left top width height 值,类型为整型数组[int,int,int,int]
var setcursor = function (dom,shape) {
  dom.style.cursor = shape;
}; //设置鼠标经过dom的指针形状
var EventsType = {//事件类型
  click:"click",
  mousedown:"mousedown",
  mouseup:"mouseup",
  mouseover:"mouseover",
  mouseleave:"mouseleave",
  mousemove:"mousemove",
  DOMContentLoaded:"DOMContentLoaded",
};
var AddEvent = function (dom, type, fun) {
  dom.addEventListener(type, fun, false);
};   //添加dom对象的事件监听方法
var AddEvent2 = function (dom, type1, fun1, type2, fun2) {
  dom.addEventListener(type1, fun1, false);
  dom.addEventListener(type2, fun2, false);
};   //一次添加dom的两个事件监听方法
var AddEvent3 = function (dom, type1, fun1, type2, fun2, type3, fun3) {
  dom.addEventListener(type1, fun1, false);
  dom.addEventListener(type2, fun2, false);
  dom.addEventListener(type3, fun3, false);
}; //一次添加dom的三个事件监听方法
var DelEvent = function (dom, type, fun) {
  dom.removeEventListener(type, fun, false);
}; // 删除dom对象的事件监听方法
var DelEvent2 = function (dom, type1, fun1, type2, fun2) {
  dom.removeEventListener(type1, fun1, false);
  dom.removeEventListener(type2, fun2, false);
}; //一次删除dom对象的两个事件监听方法
var DelEvent3 = function (dom, type1, fun1, type2, fun2, type3, fun3) {
  dom.removeEventListener(type1, fun1, false);
  dom.removeEventListener(type2, fun2, false);
  dom.removeEventListener(type3, fun3, false);
}; //一次删除dom对象的三个事件监听方法
var inArray = function (str, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (str == arr[i]) {
      return true;
    }
  }
  return false;
}; // 判断字符串str是否存在于数组arr
var cannotselect = function () {
  window.getSelection().removeAllRanges();
};  //页面元素(文字、图片等)不能被选中
var setStyle = function (dom, styleName, styleValue) {
  dom.setAttribute("style", styleName + ":" + styleValue + ";");
}; //设置dom的 一个style 属性值
var setStyle2 = function (dom, styleName1, styleValue1, styleName2, styleValue2) {
  dom.setAttribute("style", styleName1 + ":" + styleValue1 + ";" + styleName2 + ":" + styleValue2 + ";");
};//一次设置dom的 两个style 属性值
var delStyle = function (dom, styleName) {
  dom.removeAttribute("style", styleName);
};//删除dom的 一个style 属性值
var delStyle2 = function (dom, styleName1, styleName2) {
  dom.removeAttribute("style", styleName1);
  dom.removeAttribute("style", styleName2);
};//一次删除dom的 两个style 属性值
var setAttr = function (dom, attrName, attrValue) {
  dom.setAttribute(attrName, attrValue);
};// 设置dom的 一个属性值
var setAttr2 = function (dom, attrName1, attrValue1, attrName2, attrValue2) {
  dom.setAttribute(attrName1, attrValue1);
  dom.setAttribute(attrName2, attrValue2);
};//一次设置dom的 两个属性值
var delAttr = function (dom, attrName) {
  dom.removeAttribute(attrName);
};//删除dom的 一个属性值
var delAttr2 = function (dom, attrName1, attrName2) {
  dom.removeAttribute(attrName1);
  dom.removeAttribute(attrName2);
};//删除dom 的两个属性值
var Hide = function (dom) {
  dom.style.display = "none";
};//  隐藏dom
var Hide3 = function (dom1,dom2,dom3) {
  dom1.style.display = "none";
  dom2.style.display = "none";
  dom3.style.display = "none";
};//  隐藏3个dom
var Show = function (dom) {
  dom.style.display = "inline";
}; // 显示dom
var Show3 = function (dom1, dom2, dom3) {
  dom1.style.display = "inline";
  dom2.style.display = "inline";
  dom3.style.display = "inline";
};//  显示3个dom
/* cxc.js 频繁操作公共接口 */ 

/* AJAX 接口 */
var url, method, msg;
var xmlReq = new XMLHttpRequest();
var AJAX = function (url, method, msg, callback) {
  xmlReq.open(method, url, true);
  xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlReq.onreadystatechange = function () {
    if (xmlReq.readyState == 4) {
      if (xmlReq.status == 200) {
        callback();
      }
      else {
        A("ajax status is " + xmlReq.status);
      }
    }
  };
  xmlReq.send(msg);
};
/* AJAX 接口 */ 

/* 入口 */
var start = function (fun) {
  var begin = function () {
    DelEvent(document, EventsType.DOMContentLoaded, begin);
    fun();
  };
  AddEvent(document, EventsType.DOMContentLoaded, begin);
};
/* 入口 */ 

/* 环境 */
var screenW = window.innerWidth;
var screenH = window.innerHeight;
/* 环境 */ 

ccp.js

var cfg = {
  imgtype: ["image/jpeg", "image/png", "image/gif", "image/bmp"],
  imgsize: 5 * 1024 * 1024,
  varied_divMIN: 50,
  varied_divDEFAULT: 100,
  needWH:0,
};
var dom = {
  body: null,
  SelectPicture: null,
  upfile: null,
  pre: null,
  ccp: null,
  bt: null,
  prea: null,
  preb: null,
  prec: null,
  ctna: null,
  ctnb: null,
  ctnc: null,
  imga: null,
  imgb: null,
  imgc: null,
  Y_OUT: null,
  N_OUT: null,
  Y: null,
  N: null,
  ctn: null,
  black_cover: null,
  fixed_img: null,
  varied_div: null,
  varied_div_img: null,
  TopLeft: null,
  TopRight: null,
  BottomRight: null,
  BottomLeft: null,
};
var ccp = {
  SelectPictureW: null,
  SelectPictureH: null,
  SelectPictureP1: 9,
  SelectPictureP2: 4,
  SelectPictureL: null,
  SelectPictureT: null,
  SelectPictureFontSize: null,
  ///////////////////////////
  file: null,
  imgtype: null,
  imgsize: null,
  ///////////////////////////
  imgW: null,
  imgH: null,
  imgN: null,
  imgURL: null,
  //////////////////////////
  preW: null,
  preH: null,
  ccpW: null,
  ccpH: null,
  btW: null,
  btH: null,
  /////////////////////////
  pre4: null,
  preaL: null,
  preaT: null,
  preaWH: null,
  prebL: null,
  prebT: null,
  prebWH: null,
  precL: null,
  precT: null,
  precWH: null,
  ctnLT: 3,
  ctnaWH: null,
  ctnbWH: null,
  ctncWH: null,
  //////////////////////////
  YN3: null,
  YN_OUTWH: null,
  YNWH: null,
  YN_OUTR: null,
  Y_OUTT: null,
  N_OUTT: null,
  YNLT1: 25,
  YNLT2: 20,
  //////////////////////////
  ctnL: null,
  ctnT: null,
  black_coverL: null,
  black_coverT: null,
  /////////////////////////
  varied_divL: null,
  varied_divT: null,
  varied_divWH: null,
  varied_divMaxL: null,
  varied_divMaxT: null,
  varied_div_imgWH: null,
  varied_div_imgL: null,
  varied_div_imgT: null,
  /////////////////////////
  imgaW: null,
  imgaH: null,
  imgaL: null,
  imgaT: null,
  imgbW: null,
  imgbH: null,
  imgbL: null,
  imgbT: null,
  imgcW: null,
  imgcH: null,
  imgcL: null,
  imgcT: null,
  /////////////////////////
};
var GET_DOM = function () {
  dom.body = document.body;
  dom.pre = $("pre");
  dom.ccp = $("ccp");
  dom.bt = $("bt");
  dom.SelectPicture = $("SelectPicture");
  dom.prea = $("prea");
  dom.preb = $("preb");
  dom.prec = $("prec");
  dom.ctna = $("ctna");
  dom.ctnb = $("ctnb");
  dom.ctnc = $("ctnc");
  dom.imga = $("imga");
  dom.imgb = $("imgb");
  dom.imgc = $("imgc");
  dom.Y_OUT = $("Y_OUT");
  dom.N_OUT = $("N_OUT");
  dom.Y = $("Y");
  dom.N = $("N");
  dom.ctn = $("ctn");
  dom.black_cover = $("black_cover");
  dom.fixed_img = $("fixed_img");
  dom.varied_div = $("varied_div");
  dom.varied_div_img = $("varied_div_img");
  dom.TopLeft = $("TopLeft");
  dom.TopRight = $("TopRight");
  dom.BottomRight = $("BottomRight");
  dom.BottomLeft = $("BottomLeft");
};
var INIT_DOM = function () {
  setStyle2(dom.body, "width", screenW + "px", "height", screenH + "px");
  dom.body.style.backgroundImage = get_random_bgimg(7);/////////////////////
  set_SelectPictureW(Math.floor(screenW / ccp.SelectPictureP1));
  AddEvent2(dom.SelectPicture, EventsType.mouseover, SelectPicture_mouseover, EventsType.mouseleave, SelectPicture_mouseleave);
  AddEvent(dom.SelectPicture, EventsType.click, SelectPicture_click);/////////////////////
  ccp.preH = ccp.ccpH = ccp.btH = screenH - 2;
  ccp.ccpW = screenH + 100 - 2;
  ccp.preW = Math.ceil((screenW - (ccp.ccpW + 2)) / 2) - 2;
  ccp.btW = screenW - (ccp.ccpW + 2) - (ccp.preW + 2) - 2;
  setStyle2(dom.pre, "width", ccp.preW + "px", "height", ccp.preH + "px");
  setStyle2(dom.ccp, "width", ccp.ccpW + "px", "height", ccp.ccpH + "px");
  setStyle2(dom.bt, "width", ccp.btW + "px", "height", ccp.btH + "px");
  Hide3(dom.pre, dom.ccp, dom.bt);/////////////////////
};
var EVENTS = function () {
  AddEvent(dom.varied_div, EventsType.mousedown, varied_div_mousedown);//varied_div
  AddEvent(dom.TopLeft, EventsType.mousedown, TopLeft_mousedown);//TopLeft
  AddEvent(dom.TopRight, EventsType.mousedown, TopRight_mousedown);//TopRight
  AddEvent(dom.BottomRight, EventsType.mousedown, BottomRight_mousedown);//BottomRight
  AddEvent(dom.BottomLeft, EventsType.mousedown, BottomLeft_mousedown);//BottomLeft
  AddEvent(dom.Y, EventsType.click, Y_click);//Y
  AddEvent(dom.N, EventsType.click, N_click);//N
};
var END = function () {
  AddEvent(document, EventsType.mousemove, cannotselect);
};
start(function () {
  GET_DOM();
  INIT_DOM();
  EVENTS();
  END();
});

fun.js

var get_random_bgimg = function (n) {
  var m = Math.floor(Math.random() * n);
  var r = "url(Images/bg" + 6 + ".png)";
  return r;
};
var set_SelectPictureW = function (W) {
  ccp.SelectPictureW = W;
  ccp.SelectPictureH = Math.floor(ccp.SelectPictureW / ccp.SelectPictureP2);
  ccp.SelectPictureL = Math.floor((screenW - ccp.SelectPictureW) / 2);
  ccp.SelectPictureT = Math.floor((screenH - ccp.SelectPictureH) / 2);
  ccp.SelectPictureFontSize = Math.floor(ccp.SelectPictureH / 1.5);
  setStyle(dom.SelectPicture, "font-size", ccp.SelectPictureFontSize + "px");
  setLTWH(dom.SelectPicture, ccp.SelectPictureL, ccp.SelectPictureT, ccp.SelectPictureW, ccp.SelectPictureH);
};
var SelectPicture_mouseover = function () {
  set_SelectPictureW(ccp.SelectPictureW + 15);
};
var SelectPicture_mouseleave = function () {
  set_SelectPictureW(ccp.SelectPictureW - 15);
};
///////////////////////////////////////////////////
var downX, downY, oldL, oldT, tempWH, tempL, tempT, dxMax, tempMaxL, tempMaxT;
var varied_divLimit = function () {
  if (ccp.varied_divL < 0)
    ccp.varied_divL = 0;
  else if (ccp.varied_divL > ccp.varied_divMaxL)
    ccp.varied_divL = ccp.varied_divMaxL;
  if ((ccp.varied_divT < 0))
    ccp.varied_divT = 0;
  else if (ccp.varied_divT > ccp.varied_divMaxT)
    ccp.varied_divT = ccp.varied_divMaxT;
};
var varied_div_mousedown = function (e) {
  if (e.button > 0) {
    varied_div_mouseup();
    return false;
  }
  e.preventDefault && e.preventDefault();
  downX = e.clientX;
  downY = e.clientY;
  oldL = ccp.varied_divL;
  oldT = ccp.varied_divT;
  AddEvent2(document, EventsType.mouseup, varied_div_mouseup, EventsType.mousemove, doc_varied_div_mousemove);
};
var doc_varied_div_mousemove = function (e) {
  ccp.varied_divL = oldL + e.clientX - downX;
  ccp.varied_divT = oldT + e.clientY - downY;
  varied_divLimit();
  setLT(dom.varied_div, ccp.varied_divL, ccp.varied_divT);
  setvaried_div_img();
  setpreimg();
};
var varied_div_mouseup = function () {
  DelEvent2(document, EventsType.mouseup, varied_div_mouseup, EventsType.mousemove, doc_varied_div_mousemove);
};
////////////////////////////////////////////////////
var TopLeft_mousedown = function (e) {
  if (e.button > 0) {
    TopLeft_mouseup();
    return false;
  }
  e.preventDefault && e.preventDefault();
  downX = e.clientX;
  downY = e.clientY;
  oldL = ccp.varied_divL;
  oldT = ccp.varied_divT;
  tempWH = ccp.varied_divWH;
  tempL = ccp.varied_divL;
  tempT = ccp.varied_divT;
  tempMaxL = ccp.varied_divMaxL;
  tempMaxT = ccp.varied_divMaxT;
  dxMax = tempL >= tempT ? tempT : tempL;
  AddEvent2(document, EventsType.mouseup, TopLeft_mouseup, EventsType.mousemove, doc_TopLeft_mousemove);
};
var doc_TopLeft_mousemove = function (e) {
  varied_div_mouseup();//移动事件屏蔽,非常重要
  var dx = e.clientY - downY;
  if (dx < 0 && Math.abs(dx) > dxMax) {
    dx = -dxMax;
  }
  else if (dx > 0 && dx > tempWH - cfg.varied_divMIN) {
    dx = tempWH - cfg.varied_divMIN;
  }
  ccp.varied_divMaxL = tempMaxL + dx;
  ccp.varied_divMaxT = tempMaxT + dx;
  ccp.varied_divL = oldL + dx;
  ccp.varied_divT = oldT + dx;
  ccp.varied_divWH = tempWH - dx;
  setLTWH(dom.varied_div, ccp.varied_divL, ccp.varied_divT, ccp.varied_divWH, ccp.varied_divWH);
  setvaried_div_img();
  setpreimg();
};
var TopLeft_mouseup = function () {
  DelEvent2(document, EventsType.mouseup, TopLeft_mouseup, EventsType.mousemove, doc_TopLeft_mousemove);
};
////////////////////////////////////////////////////
var TopRight_mousedown = function (e) {
  if (e.button > 0) {
    TopRight_mouseup();
    return false;
  }
  e.preventDefault && e.preventDefault();
  downX = e.clientX;
  downY = e.clientY;
  oldL = ccp.varied_divL;
  oldT = ccp.varied_divT;
  tempWH = ccp.varied_divWH;
  tempL = ccp.imgW - ccp.varied_divL - ccp.varied_divWH;
  tempT = ccp.varied_divT;
  tempMaxL = ccp.varied_divMaxL;
  tempMaxT = ccp.varied_divMaxT;
  dxMax = tempL >= tempT ? tempT : tempL;
  AddEvent2(document, EventsType.mouseup, TopRight_mouseup, EventsType.mousemove, doc_TopRight_mousemove);
};
var doc_TopRight_mousemove = function (e) {
  varied_div_mouseup();//移动事件屏蔽,非常重要
  var dx = e.clientY - downY;
  if (dx < 0 && Math.abs(dx) > dxMax) {
    dx = -dxMax;
  }
  else if (dx > 0 && dx > tempWH - cfg.varied_divMIN) {
    dx = tempWH - cfg.varied_divMIN;
  }
  ccp.varied_divMaxL = tempMaxL + dx;
  ccp.varied_divMaxT = tempMaxT + dx;
  ccp.varied_divL = oldL;
  ccp.varied_divT = oldT + dx;
  ccp.varied_divWH = tempWH - dx;
  setLTWH(dom.varied_div, ccp.varied_divL, ccp.varied_divT, ccp.varied_divWH, ccp.varied_divWH);
  setvaried_div_img();
  setpreimg();
};
var TopRight_mouseup = function () {
  DelEvent2(document, EventsType.mouseup, TopRight_mouseup, EventsType.mousemove, doc_TopRight_mousemove);
};
///////////////////////////////////////////////////
var BottomRight_mousedown = function (e) {
  if (e.button > 0) {
    BottomRight_mouseup();
    return false;
  }
  e.preventDefault && e.preventDefault();
  downX = e.clientX;
  downY = e.clientY;
  oldL = ccp.varied_divL;
  oldT = ccp.varied_divT;
  tempWH = ccp.varied_divWH;
  tempL = ccp.imgW - ccp.varied_divL - ccp.varied_divWH;
  tempT = ccp.imgH - ccp.varied_divT - ccp.varied_divWH;
  tempMaxL = ccp.varied_divMaxL;
  tempMaxT = ccp.varied_divMaxT;
  dxMax = tempL >= tempT ? tempT : tempL;
  AddEvent2(document, EventsType.mouseup, BottomRight_mouseup, EventsType.mousemove, doc_BottomRight_mousemove);
};
var doc_BottomRight_mousemove = function (e) {
  varied_div_mouseup();//移动事件屏蔽,非常重要
  var dx = e.clientY - downY;
  if (dx > 0 && dx > dxMax) {
    dx = dxMax;
  }
  else if (dx < 0 && Math.abs(dx) > tempWH - cfg.varied_divMIN) {
    dx = -(tempWH - cfg.varied_divMIN);
  }
  ccp.varied_divMaxL = tempMaxL - dx;
  ccp.varied_divMaxT = tempMaxT - dx;
  ccp.varied_divL = oldL;
  ccp.varied_divT = oldT;
  ccp.varied_divWH = tempWH + dx;
  setLTWH(dom.varied_div, ccp.varied_divL, ccp.varied_divT, ccp.varied_divWH, ccp.varied_divWH);
  setvaried_div_img();
  setpreimg();
};
var BottomRight_mouseup = function () {
  DelEvent2(document, EventsType.mouseup, BottomRight_mouseup, EventsType.mousemove, doc_BottomRight_mousemove);
};
///////////////////////////////////////////////////
var BottomLeft_mousedown = function (e) {
  if (e.button > 0) {
    BottomLeft_mouseup();
    return false;
  }
  e.preventDefault && e.preventDefault();
  downX = e.clientX;
  downY = e.clientY;
  oldL = ccp.varied_divL;
  oldT = ccp.varied_divT;
  tempWH = ccp.varied_divWH;
  tempL = ccp.varied_divL;
  tempT = ccp.imgH - ccp.varied_divT - ccp.varied_divWH;
  tempMaxL = ccp.varied_divMaxL;
  tempMaxT = ccp.varied_divMaxT;
  dxMax = tempL >= tempT ? tempT : tempL;
  AddEvent2(document, EventsType.mouseup, BottomLeft_mouseup, EventsType.mousemove, doc_BottomLeft_mousemove);
};
var doc_BottomLeft_mousemove = function (e) {
  varied_div_mouseup();//移动事件屏蔽,非常重要
  var dx = e.clientY - downY;
  if (dx > 0 && dx > dxMax) {
    dx = dxMax;
  }
  else if (dx < 0 && Math.abs(dx) > tempWH - cfg.varied_divMIN) {
    dx = -(tempWH - cfg.varied_divMIN);
  }
  ccp.varied_divMaxL = tempMaxL - dx;
  ccp.varied_divMaxT = tempMaxT - dx;
  ccp.varied_divL = oldL - dx;
  ccp.varied_divT = oldT;
  ccp.varied_divWH = tempWH + dx;
  setLTWH(dom.varied_div, ccp.varied_divL, ccp.varied_divT, ccp.varied_divWH, ccp.varied_divWH);
  setvaried_div_img();
  setpreimg();
};
var BottomLeft_mouseup = function () {
  DelEvent2(document, EventsType.mouseup, BottomLeft_mouseup, EventsType.mousemove, doc_BottomLeft_mousemove);
};
///////////////////////////////////////////////////
var getDATA = function () {
  var parameter = location.search; //获取url中"?"符后的字串
  if (parameter.length == 0) {
    return 666;
  } else {
    var ss = parameter.split("&");
    url = ss[0].split("=")[1];
    cfg.needWH = ss[1].split("=")[1];
  }
  if (url.length == 0) {
    return 777;
  } else if (cfg.needWH == 0) {
    return 888;
  } else if (cfg.needWH > 1000) {
    return 999;
  } 

  var canvas = document.createElement("canvas");
  canvas.style.display = "none";
  var ctx = canvas.getContext("2d");
  ctx.fillStyle = "#FFFFFF";
  canvas.width = canvas.height = cfg.needWH;
  ctx.fillRect(0, 0, cfg.needWH, cfg.needWH);
  var a = Math.ceil(ccp.varied_divL * ccp.imgN);
  var b = Math.ceil(ccp.varied_divT * ccp.imgN);
  var c = Math.ceil(ccp.varied_divWH * ccp.imgN);
  ctx.drawImage(dom.fixed_img, a, b, c, c, 0, 0, cfg.needWH, cfg.needWH);
  return canvas.toDataURL(ccp.imgtype, 1);
};
var callback = function () {
  var txt = xmlReq.responseText;
  alert(txt);
  window.opener = null;
  window.open('', '_self');
  window.close();
};
var Y_click = function () {
  var DATA = getDATA();
  DATA == 666 && alert("没有参数");
  DATA == 777 && alert("没有返回地址");
  DATA == 888 && alert("未给出返回图片大小");
  DATA == 999 && alert("图片大小不能超过 1000 X 1000");
  if (DATA == 666 || DATA == 777 || DATA == 888 || DATA == 999) {
    window.opener = null;
    window.open('', '_self');
    window.close();
  }//没有参数或参数错误关闭页面
  method = "post";
  msg = "";
  AJAX(url, method, "DATA=" + DATA, callback);
};
var N_click = function () {
  Hide3(dom.pre, dom.ccp, dom.bt);
};
///////////////////////////////////////////////////
var check_imgtype = function () {
  if (!inArray(ccp.imgtype, cfg.imgtype)) {
    alert("请选择正确的图片类型");
    return false;
  } else { return true; }
};
var check_imgsize = function () {
  if (ccp.imgsize > cfg.imgsize) {
    alert("图片不能超过"+(cfg.imgsize/1024)/1024+"M");
    return false;
  } else { return true; }
};
var set_pre = function () {
  ccp.preaWH = Math.round(ccp.preW * 0.6);
  ccp.prebWH = Math.round(ccp.preW * 0.5);
  ccp.precWH = Math.round(ccp.preW * 0.4);
  ccp.preaL = Math.round((ccp.preW - ccp.preaWH) / 2);
  ccp.prebL = Math.round((ccp.preW - ccp.prebWH) / 2);
  ccp.precL = Math.round((ccp.preW - ccp.precWH) / 2);
  ccp.pre4 = Math.round((ccp.preH - ccp.preaWH - ccp.prebWH - ccp.precWH) / 4);
  ccp.preaT = ccp.pre4;
  ccp.prebT = ccp.pre4 * 2 + ccp.preaWH;
  ccp.precT = ccp.pre4 * 3 + ccp.preaWH + ccp.prebWH;
  setLTWH(dom.prea, ccp.preaL, ccp.preaT, ccp.preaWH, ccp.preaWH);
  setLTWH(dom.preb, ccp.prebL, ccp.prebT, ccp.prebWH, ccp.prebWH);
  setLTWH(dom.prec, ccp.precL, ccp.precT, ccp.precWH, ccp.precWH);
  ////////////////////////////////////////////////////////////////
  ccp.ctnaWH = ccp.preaWH - ccp.ctnLT * 2;
  ccp.ctnbWH = ccp.prebWH - ccp.ctnLT * 2;
  ccp.ctncWH = ccp.precWH - ccp.ctnLT * 2;
  setLTWH(dom.ctna, ccp.ctnLT, ccp.ctnLT, ccp.ctnaWH, ccp.ctnaWH);
  setLTWH(dom.ctnb, ccp.ctnLT, ccp.ctnLT, ccp.ctnbWH, ccp.ctnbWH);
  setLTWH(dom.ctnc, ccp.ctnLT, ccp.ctnLT, ccp.ctncWH, ccp.ctncWH);
  dom.imga.src = dom.imgb.src = dom.imgc.src = ccp.imgURL;
};
var setYN = function (dom, n) {
  ccp.YNWH = ccp.YN_OUTWH - n * 2;
  setStyle(dom, "font-size", Math.floor(ccp.YNWH * 0.9) + "px");
  setLTWH(dom, n, n, ccp.YNWH, ccp.YNWH);
};
var Y_mouseover = function () {
  setYN(dom.Y, ccp.YNLT2);
};
var Y_mouseleave = function () {
  setYN(dom.Y, ccp.YNLT1);
};
var N_mouseover = function () {
  setYN(dom.N, ccp.YNLT2);
};
var N_mouseleave = function () {
  setYN(dom.N, ccp.YNLT1);
};
var set_bt = function () {
  ccp.YN_OUTWH = Math.round(ccp.btW * 0.6);
  ccp.YN_OUTR = Math.round((ccp.btW - ccp.YN_OUTWH) / 2);
  ccp.YN3 = Math.round((ccp.btH - ccp.YN_OUTWH * 2) / 3);
  ccp.Y_OUTT = ccp.YN3;
  ccp.N_OUTT = ccp.YN3 * 2 + ccp.YN_OUTWH;
  setRTWH(dom.Y_OUT, ccp.YN_OUTR, ccp.Y_OUTT, ccp.YN_OUTWH, ccp.YN_OUTWH);
  setRTWH(dom.N_OUT, ccp.YN_OUTR, ccp.N_OUTT, ccp.YN_OUTWH, ccp.YN_OUTWH);
  setYN(dom.Y, ccp.YNLT1);
  setYN(dom.N, ccp.YNLT1);
  AddEvent2(dom.Y, EventsType.mouseover, Y_mouseover, EventsType.mouseleave, Y_mouseleave);
  AddEvent2(dom.N, EventsType.mouseover, N_mouseover, EventsType.mouseleave, N_mouseleave);
};
var setvaried_div = function () {
  if (ccp.imgW > ccp.imgH) {
    ccp.varied_divWH = ccp.imgH < cfg.varied_divDEFAULT ? ccp.imgH : cfg.varied_divDEFAULT;
  }
  else {
    ccp.varied_divWH = ccp.imgW < cfg.varied_divDEFAULT ? ccp.imgW : cfg.varied_divDEFAULT;
  }
  ccp.varied_divL = Math.round((ccp.imgW - ccp.varied_divWH) / 2);
  ccp.varied_divT = Math.round((ccp.imgH - ccp.varied_divWH) / 2);
  ccp.varied_divMaxL = ccp.imgW - ccp.varied_divWH;
  ccp.varied_divMaxT = ccp.imgH - ccp.varied_divWH;
  setLTWH(dom.varied_div, ccp.varied_divL, ccp.varied_divT, ccp.varied_divWH, ccp.varied_divWH);
};
var setvaried_div_img = function () {
  ccp.varied_div_imgL = -ccp.varied_divL;
  ccp.varied_div_imgT = -ccp.varied_divT;
  setLT(dom.varied_div_img, ccp.varied_div_imgL, ccp.varied_div_imgT);
};
var setpreimg = function () {
  var p1, p2, p3;
  p1 = ccp.ctnaWH / ccp.varied_divWH;
  p2 = ccp.ctnbWH / ccp.varied_divWH;
  p3 = ccp.ctncWH / ccp.varied_divWH;
  ccp.imgaW = Math.round(p1 * ccp.imgW);
  ccp.imgaH = Math.round(p1 * ccp.imgH);
  ccp.imgaL = Math.round(p1 * ccp.varied_div_imgL);
  ccp.imgaT = Math.round(p1 * ccp.varied_div_imgT);
  ccp.imgbW = Math.round(p2 * ccp.imgW);
  ccp.imgbH = Math.round(p2 * ccp.imgH);
  ccp.imgbL = Math.round(p2 * ccp.varied_div_imgL);
  ccp.imgbT = Math.round(p2 * ccp.varied_div_imgT);
  ccp.imgcW = Math.round(p3 * ccp.imgW);
  ccp.imgcH = Math.round(p3 * ccp.imgH);
  ccp.imgcL = Math.round(p3 * ccp.varied_div_imgL);
  ccp.imgcT = Math.round(p3 * ccp.varied_div_imgT);
  setLTWH(dom.imga, ccp.imgaL, ccp.imgaT, ccp.imgaW, ccp.imgaH);
  setLTWH(dom.imgb, ccp.imgbL, ccp.imgbT, ccp.imgbW, ccp.imgbH);
  setLTWH(dom.imgc, ccp.imgcL, ccp.imgcT, ccp.imgcW, ccp.imgcH);
};
var set_ccp = function () {
  ccp.ctnL = ccp.preW + 3 + Math.ceil((ccp.ccpW - ccp.imgW) / 2);
  ccp.ctnT = 1 + Math.ceil((ccp.ccpH - ccp.imgH) / 2);
  setLTWH(dom.ctn, ccp.ctnL, ccp.ctnT, ccp.imgW, ccp.imgH);
  ccp.black_coverL = ccp.preW + 3;
  ccp.black_coverT = 1;
  setLTWH(dom.black_cover, ccp.black_coverL, ccp.black_coverT, ccp.ccpW, ccp.ccpH);
  setLTWH(dom.fixed_img, 0, 0, ccp.imgW, ccp.imgH);
  dom.fixed_img.src = ccp.imgURL;
  setvaried_div();
  setWH(dom.varied_div_img, ccp.imgW, ccp.imgH);
  dom.varied_div_img.src = ccp.imgURL;
  setvaried_div_img();
  setpreimg();
};
var setStart = function () {
  set_pre();
  set_bt();
  set_ccp();
  Show3(dom.pre, dom.ccp, dom.bt);
};
var setImgWH = function (src, type) {
  var image = new Image();
  image.onload = function () {
    var width = this.width, height = this.height;//图片的宽高
    var p = width / height;
    if (p > 1) {
      if (p > ccp.ccpW / 50) {
        alert("图片宽高比不能超过" + p);
        return;
      }
      else {
        if (height < 50) {
          ccp.imgN = height / 50;
          ccp.imgH = 50;
          ccp.imgW = Math.round(ccp.imgH * p);
        }
        else if (width > ccp.ccpW) {
          ccp.imgN = width / ccp.ccpW;
          ccp.imgW = ccp.ccpW;
          ccp.imgH = Math.round(ccp.imgW / p);
        }
        else {
          ccp.imgN = 1;
          ccp.imgW = width;
          ccp.imgH = height;
        }
      }
    }
    else {
      if (p < 50 / ccp.ccpH) {
        alert("图片宽高比不能小于" + p);
        return;
      }
      else {
        if (width < 50) {
          ccp.imgN = width / 50;
          ccp.imgW = 50;
          ccp.imgH = Math.round(ccp.imgW / p);
        }
        else if (height > ccp.ccpH) {
          ccp.imgN = height / ccp.ccpH;
          ccp.imgH = ccp.ccpH;
          ccp.imgW = Math.round(ccp.imgH * p);
        }
        else {
          ccp.imgN = 1;
          ccp.imgW = width;
          ccp.imgH = height;
        }
      }
    }
    ccp.imgURL = this.src;
    delete image;
    setStart();
  };
  image.onerror = function () {
    alert("图片已损坏,请上传正确图片");
  };
  image.src = src;
};
var SelectPicture_click = function () {
  dom.upfile = document.createElement("input");
  setAttr2(dom.upfile, "type", "file", "id", "upfile");
  dom.upfile.click();
  dom.upfile.onchange = function () {
    ccp.file = this.files[0];
    ccp.imgtype = ccp.file.type;
    if (!check_imgtype()) {
      return;
    } //检查文件类型
    ccp.imgsize = ccp.file.size;
    if (!check_imgsize()) {
      return;
    }; //检查图片大小
    var reader = new FileReader();
    reader.onload = function () {
      setImgWH(this.result, ccp.imgtype);
    };
    reader.readAsDataURL(ccp.file);
  };
};

ccp.css

*{
  margin:0px;
  padding:0px;
}
#SelectPicture{
  position:absolute;
  border:3px solid #ff6a00;
  border-radius:8px;
  color:#ff6a00;
  text-align:center;
  font-family:'Microsoft YaHei';
  cursor:pointer;
}
#upfile{
  display:none;
}
#pre,#ccp,#bt{
  float:left;
  z-index:1;
  border:1px solid #ffffff;
}
#ccp{
  border:1px dashed #808080;
  border-left:1px dashed #808080;
  border-right:1px dashed #808080;
}
#prea,#preb,#prec{
  position:absolute;
  background-color:#ff6a00;
  border-radius:7px;
}
#ctna,#ctnb,#ctnc{
  position:absolute;
  background-color:#ffffff;
  overflow:hidden;
}
#imga,#imgb,#imgc{
  position:absolute;
  left:0px;
  top:0px;
}
#Y_OUT,#N_OUT{
  position:absolute;
}
#Y,#N{
  /* background-color:#ff6a00;*/
  position:absolute;
  text-align:center;
  border:3px solid #ff6a00;
  border-radius:50%;
  color:#ff6a00;
  font-family:Arial;
  cursor:pointer;
}
#ctn{
  position:absolute;
  background-color:blueviolet;
  overflow:hidden;
}
#black_cover{
  position:absolute;
  background-color:black;
  opacity:0.6;
  z-index:3;
}
#fixed_img{
  position:absolute;
}
#varied_div{
  position:absolute;
  z-index:4;
  overflow: hidden;
  cursor:move;
}
#BottomRight,#TopRight,#TopLeft,#BottomLeft {
  background:#D6FB66;
  display:block;
  width:6px;
  height:6px;
  position:absolute;
  z-index:5;
  bottom:0;
  right:0;
  cursor:nw-resize;
}
#BottomLeft {
  bottom:0;
  left:0;
  cursor:ne-resize;
}
#TopRight {
  top:0;
  right:0;
  cursor:ne-resize;
}
#TopLeft {
  top:0;
  left:0;
  cursor:nw-resize;
}
#varied_div_img{
  position:absolute;
  z-index:5;
} 

以上就是本文的全部内容,希望对大家学习javascript程序设计有所帮助。

(0)

相关推荐

  • 针对多用户实现头像上传功能PHP代码 适用于登陆页面制作

    一个网站,其实说白了就是某几个特定功能的组合,而更换用户头像就在这些功能之中.今天就来做个测试,针对不同的用户,实现头像上传功能. -------------------------------------------------------------------------------- 成品图 思路  •针对不同的用户上传头像,我们要为每一个已登录的用户创建一个文件夹,文件夹的名称以当前用户的用户名为准. •用户上传成功后,跳转到用户登录成功后的页面,并刷新用户头像. 登陆页面 表单制作

  • JS仿flash上传头像效果实现代码

    先看效果图(在FF下): 下面是我整个的思路: 一.用ajaxFileUpload.js异步上传图片.这个不用多说了,挺简单的东西. 二.div拖拽用jquery-ui.这个也不多说了. 三.上传过图片的处理. 编辑容器是300*300,如果图片不在300*300之内,这里会生成一个maxHeight=300,maxWidth=300的缩略图, 用于设置编辑容器的背景,如果原文件为123.jpg,这里生成的文件为123.jpg.view.jpg,当然,文件123.jpg.view.jpg可能不会

  • 关于js拖拽上传 [一个拖拽上传修改头像的流程]

    如今现代的浏览器已经有很多支持拖拽文件读取操作,其优点不再复述.前端时间利用拖拽改进了一下网站的头像上传流程,对其中的要点和实践体会做一点总结. 先看一下总体视图:1. 文件拖拽接受区域要有明显的标示,并且要尽可能的大(由于版面的原因,这个界面的拖放盒子并不大).可以用虚线框盒子等样式吸引用户拖拽文件.最好有明显的文字提示和图标配合. 2. 在交互体验上当文件拖入浏览器窗口时,可以用拖放区变换背景颜色等向用户发起放置操作邀请. 实现代码: 复制代码 代码如下: doc.bind({ 'drage

  • PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁

    昨天花了点时间整合了一下头像插件 东拼西凑的成果 先来看下效果 1.先使用ajaxfileupload插件做异步上传.这个地方我本来想做个上传进度的效果,但技术有限失败了.上传按钮我还做了一个文件大小的限制,但是由于浏览器兼容性的问题,不完美在IE6--IE9之间还有很多问题需要解决 getFileSize函数是用于判断文件大小的函数 复制代码 代码如下: function getFileSize(fileName) {                var byteSize = 0;     

  • JavaScript头像上传插件源码分享

    本文实例为大家分享了JavaScript头像上传插件源码,供大家参考,具体内容如下 效果图: 源码: cxc.js /* cxc.js 频繁操作公共接口 */ var $ = function (id) { return document.getElementById(id); }; //通过id获取dom对象 var A = function (msg) { alert(msg); }; //alert的简写 var EmptyFun = function () { }; // 空方法 var

  • web前端开发upload上传头像js示例代码

    这次分享一个简易的上传头像示例,其大致流程为: 一.将选择的图片转为base64字符串 function preview(file) {//预览图片得到图片base64 var prevDiv = document.getElementById('preview'); if (file.files && file.files[0]) { var reader = new FileReader(); reader.onload = function(evt){ prevDiv.innerHT

  • 实现Asp.net mvc上传头像加剪裁功能

    在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐? 本文主要介绍了Asp.net mvc实现上传头像加剪裁功能,分享给大家供大家参考.具体如下: 运行效果截图如下: 具体代码如下: 前台代码 <link href="~/Content/fineuploader.css" rel="stylesheet" /> <link href="~/Content/jquery.Jcrop

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • php视频拍照上传头像功能实现代码分享

    如果要在php中实现视频拍照我们需要借助于flash插件了,由flash拍出的确照片我们再通过php的$GLOBALS ['HTTP_RAW_POST_DATA']接受数据,然后保存成图片就可以了,下面我来介绍一下. 使用的是$GLOBALS ['HTTP_RAW_POST_DATA'],这个系统函数跟post很相似,但是$GLOBALS ['HTTP_RAW_POST_DATA']支持的数据格式更丰富些. 设计流程: $GLOBALS ['HTTP_RAW_POST_DATA']传递照片数据流

  • php+js iframe实现上传头像界面无跳转

    上传头像,界面无跳转的方式很多,我用的是加个iframe那种.下面直接上代码. html: 复制代码 代码如下: //route 为后端接口 //upload/avatar 为上传的头像的保存地址 //imgurl=/upload/avatar/<?=$uid?> 这里最后的<?=$uid?>是为了后面用js实现即时显示最新的更换后的头像用的,参照下面的js部分的代码 //头像保存名称为uid.type,如1.jpg,2.png等 //$user['avatar'] 用户如果上传过

随机推荐