1 var YImgSample=function(img){ 2 var scope=this; 3 this.image = img; 4 this.name = img.name; 5 this.canvas = null; 6 this.datas = null; 7 this.size = null; 8 this.build=function(){ 9 if(typeof(this.image)=="string"){ 10 var url = this.image; 11 this.image=new Image(); 12 this.image.onload=function(){ 13 scope.image=this; 14 scope.build(); 15 }; 16 this.image.src=url; 17 }else if(this.image instanceof Image){ 18 if(typeof(this.name)!="string"){ 19 var name = this.image.src.split('/'); 20 this.name = name[name.length-1]; 21 } 22 // this.canvas = this.getCanvas(); 23 this.canvas = this.getCanvas(true); 24 this.datas = this.getDatas(true);//alert("build image "+this.datas); 25 this.size = [this.image.width,this.image.height]; 26 }else if(this.image&&(this.image.tagName+"").toLowerCase()=='canvas'){ 27 if(typeof(this.name)!="string"){ 28 this.name='canvas'; 29 } 30 this.canvas = this.getCanvas(true); 31 this.datas = this.getDatas(true); 32 this.image = this.getImage(true); 33 this.size = [this.image.width,this.image.height]; 34 }else{ 35 //error 36 this.image = null; 37 } 38 }; 39 this.getCopy=function(){ 40 return new YImgSample(this.image); 41 }; 42 this.getCanvas = function(reference){ 43 if(reference&&this.canvas)return this.canvas; 44 var canvas = YDom.create('canvas',{width:this.image.width,height:this.image.height}); 45 var context = canvas.getContext('2d'); 46 context.drawImage(this.image, 0,0); 47 return canvas; 48 }; 49 this.getBase64=function(reference){ 50 var canvas = this.getCanvas(reference); 51 var dataURL = canvas.toDataURL("image/png"); 52 return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); 53 }; 54 this.getImage = function(reference){ 55 if(reference&&this.image)return this.image; 56 var image = new Image(); 57 image.name = this.name; 58 image.src = this.getBase64(true); 59 return image; 60 }; 61 this.getDatas = function(reference){ 62 if(reference&&this.datas)return this.datas; 63 var canvas = this.getCanvas(true); 64 var context = canvas.getContext('2d'); 65 return context.getImageData(0, 0, this.image.width, this.image.height); 66 }; 67 this.getStamp = function(size){ 68 var imgsize = [this.image.width, this.image.height]; 69 var canvas = YDom.create('canvas',{width:size[0],height:size[1]}); 70 var context = canvas.getContext('2d'); 71 var bounds=[0,0,0,0],f; 72 if(size[0]/imgsize[0]>size[1]/imgsize[1]){ 73 f = size[1]/imgsize[1]; 74 }else{ 75 f = size[0]/imgsize[0]; 76 } 77 bounds[2]=Math.floor(imgsize[0]*f); 78 bounds[3]=Math.floor(imgsize[1]*f); 79 bounds[0]=Math.round((size[0]-bounds[2])*0.5); 80 bounds[1]=Math.round((size[1]-bounds[3])*0.5); 81 context.drawImage(this.image, 0,0,this.image.width, this.image.height,bounds[0],bounds[1],bounds[2],bounds[3]); 82 return canvas; 83 }; 84 this.getPixel=function(vxy){ 85 vxy[0]=Math.max(0,Math.min(this.size[0]-1,Math.round(vxy[0]))); 86 vxy[1]=Math.max(0,Math.min(this.size[1]-1,Math.round(vxy[1]))); 87 var i = 4*(vxy[1]*this.size[0]+vxy[0]); 88 return [this.datas.data[i+0],this.datas.data[i+1],this.datas.data[i+2],this.datas.data[i+3]]; 89 }; 90 this.setPixel=function(vxy,rgba){ 91 vxy[0]=Math.max(0,Math.min(this.size[0]-1,Math.round(vxy[0]))); 92 vxy[1]=Math.max(0,Math.min(this.size[1]-1,Math.round(vxy[1]))); 93 var i = 4*(vxy[1]*this.size[0]+vxy[0]); 94 this.datas.data[i+0]=rgba[0]; 95 this.datas.data[i+1]=rgba[1]; 96 this.datas.data[i+2]=rgba[2]; 97 this.datas.data[i+3]=rgba[3]; 98 99 }; 100 this.build(); 101 }; 102