jquery imgareaselect jsとプログラムを組み合わせてピクチャカットを実現
5060 ワード
/*
, ff3 , jquery width() height() , ,
load
*/
sanshi_imgareaselect = function(pic_id,view_div_id){
this.pic_obj = jQuery("#"+pic_id);
this.pic_width;
this.pic_height;
this.view_div_id = view_div_id;
this.view_width = 100;
this.view_height = 100;
this.view_img_id = view_div_id+"_sanshi_img";
this.ias;
}
//
sanshi_imgareaselect.prototype.make_view_pic =function(){
var img_obj = jQuery(document.createElement("IMG"));
img_obj.attr("src",this.pic_obj.attr("src"));
img_obj.attr("id",this.view_img_id);
img_obj.attr("width",this.view_width);
img_obj.attr("height",this.view_height);
return img_obj;
}
//
sanshi_imgareaselect.prototype.init=function(){
this.pic_width = this.pic_obj.attr("width");
this.pic_height = this.pic_obj.attr("height");
//alert(this.pic_width+":"+this.pic_height);
//
jQuery("#"+this.view_div_id).append(this.make_view_pic());
//
jQuery("#"+this.view_div_id).css({'width':this.view_width,'height':this.view_height,'overflow':'hidden'});
//
var fun_str="if ( selection.width && selection.height){ var scaleX = "+this.view_width+" / selection.width;var scaleY = "+this.view_height+" / selection.height;jQuery('#"+this.view_img_id+"').css({width: Math.round(scaleX * "+this.pic_width+"),height: Math.round(scaleY * "+this.pic_height+"),marginLeft: -Math.round(scaleX * selection.x1),marginTop: -Math.round(scaleY * selection.y1)});}";
//alert(fun_str);
// imgAreaSelect
var ias = this.pic_obj.imgAreaSelect({
//
//aspectRatio:"1:1",
//
fadeSpeed:200,
//
autoHide:false,
//
show:true,
// api
instance: true,
//
onInit:function(img, selection){ias.setSelection(100, 50, 250, 150, true);ias.update();},
// ,
onSelectEnd:function(img, selection){eval(fun_str);}
});
//
this.ias = ias;
}
// get
sanshi_imgareaselect.prototype.save_pic=function(post_page,post_param){
var opt = this.ias.getSelection(true);
var post_arr = new Array();
jQuery.each(post_param,function(i,n){
var temp_str =i+"=";
temp_str += opt[n] ? opt[n] : n;
post_arr.push(temp_str);
});
// ,
post_page += post_page.lastIndexOf("?")<0 ? "?" : "&";
// get url
post_url = post_page+post_arr.join("&");
alert(post_url);
}
これはカプセル化されたjsコードです.次はこのコードがどのように使われているかを見てみましょう.
$(document).ready(function () {
//
var sanshi_img = new sanshi_imgareaselect("mypic","preview");
// , bug,
$("#mypic").load(function(){sanshi_img.init();});
//
$("#save_pic").click(function(){
sanshi_img.save_pic('1.php?n=6',{"id":5,"px1":"x1","py1":"y1",'px2':"x2","py2":"y2",'pwidth':"width",'pheight':"height"});
});
})
htmlコードを見てみましょう
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-animated.css" href="css/imgareaselect-animated.css" />
<script type="text/javascript" src="jquery-1.3.2.min.js" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js" src="scripts/jquery.imgareaselect.pack.js"></script>
</head>
<body>
<div id="s">
<img id="mypic" name="mypic" dt="sanshi" src="scott-h-biram.jpg" src="scott-h-biram.jpg" title="mypic"/>
</div>
<div id="preview"></div>
<div><input type="button" id="save_pic" value=" "></div>
</body>
</html>
この中のhtmlの頭式はなくすことができなくて、もしなくすならば、ie 7の下で問題があって、使用に影響しないで、しかし美観度に影響します
from:http://www.codesky.net/article/doc/201004/20100417042644.htm