用户:Salt lovely/沙盒

Salt lovely留言 | 贡献2022年10月5日 (三) 21:34的版本 (编辑“用户:Salt_lovely/沙盒”的“测试功能”章节 // 维基盐编辑器)

测试功能

JS代码存档

// MCBBS头像上传时三个大小头像切片上传代码 - 修改 - 启用抗锯齿
// 其实就加了两行
(0, eval)(`function saveAvatar() {
    var img = $('avatarimage');
    var sd = getSelectorDimention();
    var ad = getAvatarDimension();
    var rl = (sd.left-ad.left)/ad.width;
    var rt = (sd.top-ad.top)/ad.height;
    var rw = sd.width/ad.width;
    var rh = sd.height/ad.height;
    var iw = jQuery('#avatarimage').width();
    var ih = jQuery('#avatarimage').height();
    var sl = rl*iw;
    var st = rt*ih;
    var sw = rw*iw;
    var sh = rh*ih;
    var tw = sw;
    var th = sh;
    if (sw>200 || sh>250) {
        var r = Math.max(sw/200, sh/250);
        tw = Math.floor(sw/r);
        th = Math.floor(sh/r);
    }          
    var canvas = document.createElement('canvas');
    canvas.width = tw;
    canvas.height = th;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, tw, th);
    ctx.drawImage(img, sl, st, sw, sh, 0, 0, tw, th);
    ctx.imageSmoothingEnabled = true;   // 启用抗锯齿
    ctx.imageSmoothingQuality = "high"; // 高品质抗锯齿
    var dataURL = canvas.toDataURL("image/jpeg");
    jQuery('#avatar1').val(dataURL.substr(dataURL.indexOf(",") + 1));
    var tw = sw;
    var th = sh;
    if (sw>120 || sh>120) {
        var r = Math.max(sw/120, sh/120);
        tw = Math.floor(sw/r);
        th = Math.floor(sh/r);
    }     
    var canvas = document.createElement('canvas');
    canvas.width = tw;
    canvas.height = th;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, tw, th);
    ctx.drawImage(img, sl, st, sw, sh, 0, 0, tw, th);
    var dataURL = canvas.toDataURL("image/jpeg");
    jQuery('#avatar2').val(dataURL.substr(dataURL.indexOf(",") + 1));
    var mwh = Math.min(sw, sh);
    if (sw>mwh) {
        sl += Math.floor((sw-mwh)/2);
        sw = mwh;
    }
    if (sh>mwh) {
        st += Math.floor((sh-mwh)/2);
        sh = mwh;
    }
    var tw = 48;
    var th = 48;
    var canvas = document.createElement('canvas');
    canvas.width = tw;
    canvas.height = th;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, tw, th);
    ctx.drawImage(img, sl, st, sw, sh, 0, 0, tw, th);
    var dataURL = canvas.toDataURL("image/jpeg");
    jQuery('#avatar3').val(dataURL.substr(dataURL.indexOf(",") + 1));
    var src = $('avatarform').action;
    $('avatarform').action = data[data.indexOf('src')+1].replace('images/camera.swf?inajax=1', 'index.php?m=user&a=rectavatar&base64=yes');
    $('avatarform').target='rectframe'; 
}`)