電子簽章圖片採集javascript
印章圖片的採集兩種互補方式:html
方式1:在線生成印章圖片方式,可是這種方式有個弊端,對印章中公司名稱字數有限制,字數越多可能就完蛋了。html5
方式2:上傳印章掃描件,系統來對掃描圖片進行處理,提取掃描件中的印章圖片。java
本文介紹方式1,方式2待後續發佈,方式1原本想用java實現印章圖片生成,雖然網上有不少現成代碼,但須要調整印章圖片大小達到規範,印章大小:圓形印章尺寸43mm*43mm(偏差容許範圍2-3mm),橢圓印章43mm*26mm(偏差容許範圍2-3mm)比較接近真實印章。想到java調試起來比較費勁,因此改用html5實現。jquery
html5實現圓章,橢圓章思路:canvas
HTML5 <canvas> 標籤用於繪製圖像(經過腳本,一般是 JavaScript),canvas進行印章繪畫,而後經過canvas生成印章圖片而後轉成base64圖片。測試
難點:字體
橢圓章的曲線文字比較難搞,雖然網上有JQ的js能夠直接生成曲線字符排列,可是卻沒法轉換成canvas。ui
解決:this
先把文字圓形排列,而後文字圓進行縮放(均勻壓縮)。
先上效果圖:
圓形中英文圓形印章 中文圓形印章 橢圓中英文印章 橢圓中文印章
硬貨
代碼:
圓形
1 <!DOCTYPE HTML> 2 <HEAD> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 4 <title>圓形公章</title> 5 </HEAD> 6 <body> 7 <BR> 8 輸入單位(14位,多了本身調):<input id="dw" width="50" type="text" value='某某某某某某某某某單位人事部'/> 9 <br> 10 輸入單位(英文):<input id="edw" width="50" type="text" value='WTYRBCHFBBDHCBFVBDDD'/> 11 <br> 12 章下面文字:<input id="mdtext" width="50" type="text" value='專用章'/> 13 <div id="sealdiv" > 14 <canvas id="canvas" width="250" height="250"></canvas> 15 </div> 16 <input type="button" onclick="javascript:createSealEx();" value="生成中英公章"/> 17 <input type="button" onclick="javascript:createSealEx2();" value="生成中公章"/> 18 </body> 19 20 <SCRIPT LANGUAGE="javascript"> 21 22 function createSealEx(){ 23 var dw = document.getElementById("dw"); 24 var edw = document.getElementById("edw"); 25 var mdtext = document.getElementById("mdtext"); 26 var tuzhangdiv = document.getElementById("tuzhangdiv"); 27 tuzhangdiv.innerHTML ="<canvas id='canvas' width='160' height='160'></canvas>"; 28 createSeal('canvas',dw.value,edw.value,mdtext.value); 29 30 31 } 32 function createSealEx2(){ 33 var dw = document.getElementById("dw"); 34 var edw = document.getElementById("edw"); 35 var mdtext = document.getElementById("mdtext"); 36 var tuzhangdiv = document.getElementById("tuzhangdiv"); 37 tuzhangdiv.innerHTML ="<canvas id='canvas' width='160' height='160'></canvas>"; 38 createSeal11('canvas',dw.value,mdtext.value); 39 40 } 41 42 function createSeal11(id,company,name){ 43 44 var canvas = document.getElementById(id); 45 var context = canvas.getContext('2d'); 46 47 // 繪製印章邊框 48 var width=canvas.width/2; 49 var height=canvas.height/2; 50 context.lineWidth=2; 51 context.strokeStyle="#f00"; 52 context.beginPath(); 53 context.arc(width,height,78,0,Math.PI*2); 54 context.stroke(); 55 context.save(); 56 57 context.lineWidth=1; 58 context.strokeStyle="#f00"; 59 context.beginPath(); 60 context.arc(width,height,75,0,Math.PI*2); 61 context.stroke(); 62 context.save(); 63 64 //畫五角星 65 create5star(context,width,height,25,"#f00",0); 66 67 // 繪製印章名稱 68 context.font = '18px Helvetica'; 69 context.textBaseline = 'middle';//設置文本的垂直對齊方式 70 context.textAlign = 'center'; //設置文本的水平對對齊方式 71 context.lineWidth=1; 72 context.fillStyle = '#f00'; 73 context.fillText(name,width,height+53); 74 75 // 繪製印章單位 76 context.translate(width,height);// 平移到此位置, 77 context.font = '20px Helvetica' 78 var count = company.length;// 字數 79 var angle = 4*Math.PI/(3*(count - 1));// 字間角度 80 var chars = company.split(""); 81 var c; 82 for (var i = 0; i < count; i++){ 83 c = chars[i];// 須要繪製的字符 84 if(i==0) 85 context.rotate(5*Math.PI/6); 86 else 87 context.rotate(angle); 88 context.save(); 89 context.translate(64, 0);// 平移到此位置,此時字和x軸垂直 90 context.rotate(Math.PI/2);// 旋轉90度,讓字平行於x軸 91 context.fillText(c,0, 5);// 此點爲字的中心點 92 context.restore(); 93 } 94 95 96 } 97 102 103 function createSeal(id,company,ecompany,name){ 104 var canvas = document.getElementById(id); 105 var context = canvas.getContext('2d'); 106 context.translate(0,0);//移動座標原點 107 // 繪製印章邊框 108 var width=canvas.width/2; 109 var height=canvas.height/2; 110 //邊框1 111 context.lineWidth=2; 112 context.strokeStyle="#f00"; 113 context.beginPath(); 114 context.arc(width,height,78,0,Math.PI*2); 115 context.stroke(); 116 context.save(); 117 118 //邊框2 119 context.lineWidth=1; 120 context.strokeStyle="#f00"; 121 context.beginPath(); 122 context.arc(width,height,63,0,Math.PI*2); 123 context.stroke(); 124 context.save(); 125 126 127 //畫五角星 128 create5star(context,width,height,20,"#f00",0); 129 130 // 繪製印章類型 131 context.font = 'bolder 15px SimSun'; 132 context.textBaseline = 'middle';//設置文本的垂直對齊方式 133 context.textAlign = 'center'; //設置文本的水平對對齊方式 134 context.lineWidth=1; 135 context.fillStyle = '#f00'; 136 context.fillText(name,width,height+50); 137 138 139 // 繪製印章中文單位 140 context.translate(width,height);// 平移到此位置, 141 context.font = 'bolder 18px SimSun' 142 var count = company.length;// 字數 143 var angle = 4*Math.PI/(3*(count-1));// 字間角度 144 var chars = company.split(""); 145 var c; 146 for (var i = 0; i < count; i++){ 147 c = chars[i];// 須要繪製的字符 148 if(i==0) 149 context.rotate(5*Math.PI/6); 150 else 151 context.rotate(angle); 152 context.save(); 153 // 平移到此位置,此時字和x軸垂直,第一個參數是與圓外邊的距離,越大距離越近 154 context.translate(52, 0); 155 context.rotate(Math.PI/2);// 旋轉90度,讓字平行於x軸 156 context.fillText(c,0, 5);// 此點爲字的中心點 157 context.restore(); 158 } 159 //繪製印章英文單位 160 context.translate(width-80,height-80);// 平移到此位置, 161 context.font = 'bolder 10px SimSun'; 162 var ecount = ecompany.length;// 字數 163 var eangle = (5*Math.PI)/(3*(ecount));// 字間角度 164 var echars = ecompany.split(""); 165 var ec; 166 for (var j = 0; j < ecount; j++){ 167 ec = echars[j];// 須要繪製的字符 168 if(j==0) 169 context.rotate(6*Math.PI/7-1); 170 else 171 context.rotate(eangle); 172 context.save(); 173 // 平移到此位置,此時字和x軸垂直,第一個參數是與圓外邊的距離,越大距離越近 174 context.translate(74, 0); 175 context.rotate(Math.PI/2);// 旋轉90度,讓字平行於x軸 176 context.fillText(ec,0, 4.8);// 此點爲字的中心點 177 context.restore(); 178 } 179 185 186 } 187 188 //繪製五角星 189 function create5star(context,sx,sy,radius,color,rotato){ 190 context.save(); 191 context.fillStyle=color; 192 context.translate(sx,sy);//移動座標原點 193 context.rotate(Math.PI+rotato);//旋轉 194 context.beginPath();//建立路徑 195 var x = Math.sin(0); 196 var y= Math.cos(0); 197 var dig = Math.PI/5 *4; 198 for(var i = 0;i< 5;i++){//畫五角星的五條邊 199 var x = Math.sin(i*dig); 200 var y = Math.cos(i*dig); 201 context.lineTo(x*radius,y*radius); 202 } 203 context.closePath(); 204 context.stroke(); 205 context.fill(); 206 context.restore(); 207 } 208 227 228 </html>
橢圓
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>橢圓</title> </head> <body> 輸入單位(支持最多14位,多了本身調):<input id="dw" width="50" type="text" value='測試印章橢圓科技有限公司公司'/> <br> 輸入單位(英文):<input id="edw" width="50" type="text" value='EASTPORTCOMPANY'/> <br> 章下面文字:<input id="mdtext" width="50" type="text" value='公司章'/> <div id="sealdiv" > <canvas id="canvas" width="165" height="165"></canvas> </div> </div> <input type="button" onclick="javascript:createSealEx();" value="生成中文公章"/> <input type="button" onclick="javascript:createSealEx2();" value="生成中英公章"/> <script> function createSealEx(){ var dw = document.getElementById("dw"); var edw = document.getElementById("edw"); var mdtext = document.getElementById("mdtext"); var sealdiv = document.getElementById("sealdiv"); sealdiv.innerHTML ="<canvas id='canvas' width='165' height='165'></canvas>"; createSeal2('canvas',dw.value,mdtext.value); } function createSealEx2(){ var dw = document.getElementById("dw"); var edw = document.getElementById("edw"); var mdtext = document.getElementById("mdtext"); var sealdiv = document.getElementById("sealdiv"); sealdiv.innerHTML ="<canvas id='canvas' width='165' height='165'></canvas>"; createSeal1('canvas',dw.value,edw.value,mdtext.value); } function createSeal1(id,company,ecompany,name){ var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.strokeStyle="red";//設置邊框顏色 context.textBaseline = 'middle';//設置文本的垂直對齊方式 context.textAlign = 'center'; //設置文本的水平對對齊方式 context.lineWidth = 2;//橢圓1寬度 //3個參數: 左邊距 上邊據 寬度 橢圓扁度 BezierEllipse4(context, 85, 79, 79, 55); //橢圓1 context.lineWidth = 1; BezierEllipse4(context, 85, 79, 76, 52); //橢圓2 context.lineWidth = 2; BezierEllipse4(context, 85, 79, 63, 39); //橢圓3 // 繪製印章類型 context.font = 'bolder 10px SimSun';//設置字體大小 樣式 context.fillStyle = 'red';//設置字體顏色 context.fillText(name,canvas.width/2+3,canvas.height/2+25); context.save(); //保存上述操做 //繪製英文 var circle={ x:canvas.width/2, y:canvas.height/2, radius:58 }; var startAngle=220;//控制字符起始位置度數 var endAngle =-40;//首位字符相隔度數 var radius=circle.radius //圓的半徑 var angleDecrement=(startAngle-endAngle)/(ecompany.length-1)//每一個字母佔的弧度 context.font="bolder 10px SimSun" context.lineWidth=1;//設置字體胖瘦 var ratioX = 70 / circle.radius; //橫軸縮放比率 var ratioY = 45 / circle.radius; //縱軸縮放比率 //進行縮放(均勻壓縮) 重點 context.scale(ratioX, ratioY); var index=0; for(var index=0;index<ecompany.length;index++){ //保存以前的設置開始繪畫 context.save(); context.beginPath(); context.translate(circle.x+Math.cos((Math.PI/180)*startAngle)*radius-12,circle.y-Math.sin((Math.PI/180)*startAngle)*radius+19)//繪製點 +-微調 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle) ; //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 context.fillText(ecompany.charAt(index),0,0); context.strokeText(ecompany.charAt(index),0,0); startAngle-=angleDecrement; context.restore(); } // 繪製印章類型 context.font = 'bolder 14px SimSun'; context.lineWidth=1; context.fillStyle = '#f00'; context.fillText(company.substring(0,6),canvas.width/2-11,canvas.height/2+6); context.save(); context.font = 'bolder 14px SimSun'; context.lineWidth=1; context.fillStyle = '#f00'; context.fillText(company.substring(6,12),canvas.width/2-12,canvas.height/2+25); context.save(); context.font = 'bolder 14px SimSun'; context.lineWidth=1; context.fillStyle = '#f00'; context.fillText(company.substring(12,company.length),canvas.width/2-12,canvas.height/2+40); context.save(); } function createSeal2(id,company,name){ var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.strokeStyle="red";//設置文本顏色 context.textBaseline = 'middle';//設置文本的垂直對齊方式 context.textAlign = 'center'; //設置文本的水平對對齊方式 context.lineWidth = 2;//橢圓1寬度 //3個參數: 左邊距 上邊據 寬度 橢圓扁度 BezierEllipse4(context, 85, 79, 79, 55); //橢圓1 context.lineWidth = 1; BezierEllipse4(context, 85, 79, 76, 52); //橢圓2 // 繪製印章類型 context.font = 'bolder 15px SimSun'; context.lineWidth=1; context.fillStyle = '#f00'; context.fillText(name,canvas.width/2+3,canvas.height/2+10); context.save(); //繪製中文 var ccircle={ x:canvas.width/2, y:canvas.height/2, radius:59 }; var cstartAngle=170;//控制字符起始位置度數 var cendAngle =15;//首位字符相隔度數 var cradius=ccircle.radius //圓的半徑 var cangleDecrement=(cstartAngle-cendAngle)/(company.length-1)//每一個字母佔的弧度 context.font="12px SimSun" var cratioX = 66 / ccircle.radius; //橫軸縮放比率 var cratioY = 57 / ccircle.radius; //縱軸縮放比率 //進行縮放(均勻壓縮) context.scale(cratioX, cratioY); var cindex=0; for(var cindex=0;cindex<company.length;cindex++){ context.save() context.beginPath() //繪製點 context.translate(ccircle.x+Math.cos((Math.PI/180)*cstartAngle)*cradius-6,ccircle.y-Math.sin((Math.PI/180)*cstartAngle)*cradius+14) context.rotate((Math.PI/2)-(Math.PI/180)*cstartAngle) //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 context.fillText(company.charAt(cindex),0,0) context.strokeText(company.charAt(cindex),0,0) cstartAngle-=cangleDecrement context.restore() } } function BezierEllipse4(ctx, x, y, a, b){ var k = .5522848, ox = a * k, // 水平控制點偏移量 oy = b * k; // 垂直控制點偏移量</p> <p> ctx.beginPath(); //從橢圓的左端點開始順時針繪製四條三次貝塞爾曲線 ctx.moveTo(x - a, y); ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b); ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y); ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b); ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y); ctx.closePath(); ctx.stroke(); }; </script> </body> </html>
整理了一下
html5動態配置印章設置
1 <!DOCTYPE HTML> 2 <html> 3 4 <head> 5 6 7 </head> 8 <body> 9 長(毫米):<input type="text" id="sealHiegth" value="20"/><br> 10 寬(毫米):<input type="text" id="sealWidth" value="43"/><br> 11 印章類型:<input type="text" id="sealType" value="公司章"/><br> 12 公司名稱:<input type="text" id="sealCompany" value="東方神祕力量有限公司"/><br> 13 公司名稱(英):<input type="text" id="sealECompany" value="EFEFFEEFFDDFEFEFE"/><br> 14 15 <input type="radio" value="red" name="sealColor" checked="checked"/>紅色 16 <input type="radio" value="blue" name="sealColor"/>藍色<br> 17 18 <input type="radio" value="1" name="sealEnglish" checked="checked"/>中文 19 <input type="radio" value="2" name="sealEnglish"/>中英文<br> 20 21 <input type="radio" value="1" name="sealShape"/>圓形 22 <input type="radio" value="2" name="sealShape" />橢圓 23 <input type="radio" value="3" name="sealShape" checked="checked"/>長方形<br> 24 25 內外間距(毫米):<input type="text" id="sealSpacing" value="5"/><br> 26 <button onclick="createSeal();" >SHOW</button><button onclick="toImage();" >GO</button><br> 27 展現圖:<br> 28 <div id="sealPicMake"> 29 30 </div> 31 <br> 32 效果圖片:<br> 33 <img id="sealPic"> 34 35 <script type="text/javascript" src="jquery.min.js"></script> 36 <script type="text/javascript"> 37 $(function(){ 38 39 //createSeal(); 40 $("input[name='sealShape']").on('click',function(){ 41 var value = $(this).val(); 42 alert(value) 43 }); 44 45 46 }); 47 48 function toImage(){ 49 var img = document.getElementById('sealPic'); 50 var data = createSeal().toDataURL( 'image/png', 1 ); //1表示質量(無損壓縮) 51 img.src = data; 52 } 53 54 55 function createSeal(){ 56 var sealHiegth = $("#sealHiegth").val(); 57 var sealWidth = $("#sealWidth").val(); 58 var sealType = $("#sealType").val(); 59 var sealCompany = $("#sealCompany").val(); 60 var sealECompany = $("#sealECompany").val(); 61 var sealColor = $("input[name='sealColor']:checked").val(); 62 var sealShape = $("input[name='sealShape']:checked").val(); 63 var sealSpacing = $("#sealSpacing").val(); 64 var sealEnglish = $("input[name='sealEnglish']:checked").val(); 65 //長寬 毫米轉px 66 var height = parseFloat(sealHiegth)*96/25.4; 67 var width = parseFloat(sealWidth)*96/25.4; 68 var spacing = parseFloat(sealSpacing)*96/25.4; 69 $("#sealPicMake").html("<canvas id='canvas' width='"+width+"px' height='"+height+"px'></canvas>"); 70 //印章數據 71 var sealData={ 72 sealHiegth:height, 73 sealWidth:width, 74 sealColor:sealColor, 75 sealType:sealType, 76 sealCompany:sealCompany, 77 sealECompany:sealECompany, 78 sealShape:sealShape, 79 sealSpacing:spacing, 80 sealEnglish:sealEnglish 81 82 }; 83 //html5對象 84 var canvas = document.getElementById("canvas");; 85 var context = canvas.getContext('2d'); 86 if(sealData.sealShape=="1"){ 87 //圓形印章 88 createCircularSeal(context,sealData); 89 }else if(sealData.sealShape=="2"){ 90 //橢圓印章 91 createEllipseSeal(context,sealData); 92 }else if(sealData.sealShape=="3"){ 93 //方形印章 94 createRectangleSeal(context,sealData); 95 }else{ 96 alert('刷新頁面重試'); 97 } 98 return canvas; 99 100 101 }; 102 //長方形章 103 function createRectangleSeal(context,sealData){ 104 //設置文本的垂直對齊方式 105 context.textBaseline = 'middle'; 106 //設置文本的水平對對齊方式 107 context.textAlign = 'center'; 108 //設置文本顏色 109 context.lineWidth = 2; 110 context.font = '15px Arial'; 111 context.strokeStyle=sealData.sealColor; 112 context.strokeRect(1,1,canvas.width-5,canvas.height-5); 113 context.lineWidth = 1; 114 context.strokeRect(4,4,canvas.width-11,canvas.height-11); 115 116 //中文 117 context.save(); 118 context.lineWidth=2; 119 context.fillStyle = sealData.sealColor; 120 context.fillText(sealData.sealCompany,canvas.width/2,canvas.height/2-sealData.sealSpacing); 121 context.restore(); 122 //英文 123 context.save(); 124 context.font = '12px Arial'; 125 context.lineWidth=2; 126 context.fillStyle = sealData.sealColor; 127 context.fillText(sealData.sealECompany,canvas.width/2,canvas.height/2); 128 context.restore() 129 // 繪製印章類型 130 context.save(); 131 context.lineWidth=2; 132 context.fillStyle = sealData.sealColor; 133 context.fillText(sealData.sealType,canvas.width/2,canvas.height/2+sealData.sealSpacing); 134 context.restore(); 135 136 } 137 138 139 140 //橢圓印章 141 function createEllipseSeal(context,sealData){ 142 var width=canvas.width/2; 143 var height=canvas.height/2; 144 //設置文本顏色 145 context.strokeStyle=sealData.sealColor; 146 //設置文本的垂直對齊方式 147 context.textBaseline = 'middle'; 148 //設置文本的水平對對齊方式 149 context.textAlign = 'center'; 150 151 //3個參數: 左邊距 上邊據 寬度 橢圓扁度 152 //橢圓1 153 context.lineWidth = 2; 154 BezierEllipse4(context, width+2, height-1, width-4, height-24); 155 //橢圓2 156 context.lineWidth = 1; 157 BezierEllipse4(context, width+2, height-1, width-6, height-26); 158 159 if(sealData.sealEnglish=="1"){ 160 161 //繪製英文 162 var circle={ 163 x:width, 164 y:height, 165 radius:width-21 166 }; 167 //控制字符起始位置度數 168 var startAngle=190; 169 //首位字符相隔度數 170 var endAngle =-10; 171 //圓的半徑 172 var radius=circle.radius-sealData.sealSpacing 173 //每一個字母佔的弧度 174 var angleDecrement=(startAngle-endAngle)/(sealData.sealCompany.length-1) 175 context.font="18px SimSun" 176 //橫軸縮放比率 177 var ratioX = (width-17) / circle.radius; 178 //縱軸縮放比率 179 var ratioY = (height-34) / circle.radius; 180 //進行縮放(均勻壓縮) 181 context.scale(ratioX, ratioY); 182 var index=0; 183 context.lineWidth=1; 184 for(var index=0;index<sealData.sealCompany.length;index++){ 185 context.save() 186 context.beginPath() 187 //繪製點 188 context.translate(circle.x+Math.cos((Math.PI/180)*startAngle)*radius-3,circle.y-Math.sin((Math.PI/180)*startAngle)*radius+24) 189 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle) //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 190 context.fillText(sealData.sealCompany.charAt(index),0,0) 191 context.strokeText(sealData.sealCompany.charAt(index),0,0) 192 startAngle-=angleDecrement 193 context.restore() 194 } 195 196 // 繪製印章類型 197 context.font = 'border 18px SimSun'; 198 context.lineWidth=2; 199 context.fillStyle = sealData.sealColor; 200 context.fillText(sealData.sealType,width-6,height+45); 201 context.save(); 202 203 204 205 }else if(sealData.sealEnglish=="2"){ 206 //橢圓3 207 context.lineWidth = 2; 208 BezierEllipse4(context, width+2, height-1, width-20-sealData.sealSpacing, height-40-sealData.sealSpacing); 209 210 //繪製英文 211 var circle={ 212 x:width, 213 y:height, 214 radius:width-22 215 }; 216 //控制字符起始位置度數 217 var startAngle=230; 218 //首位字符相隔度數 219 var endAngle =-40; 220 //圓的半徑 221 var radius=circle.radius 222 //每一個字母佔的弧度 223 var angleDecrement=(startAngle-endAngle)/(sealData.sealECompany.length-1) 224 context.font="10px SimSun" 225 //橫軸縮放比率 226 var ratioX = (width-12.5) / circle.radius; 227 //縱軸縮放比率 228 var ratioY = (height-34.5) / circle.radius; 229 //進行縮放(均勻壓縮) 230 context.scale(ratioX, ratioY); 231 var index=0; 232 context.lineWidth=1; 233 for(var index=0;index<sealData.sealECompany.length;index++){ 234 context.save() 235 context.beginPath() 236 //繪製點 237 context.translate(circle.x+Math.cos((Math.PI/180)*startAngle)*radius-10,circle.y-Math.sin((Math.PI/180)*startAngle)*radius+19) 238 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle) //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 239 context.fillText(sealData.sealECompany.charAt(index),0,0) 240 context.strokeText(sealData.sealECompany.charAt(index),0,0) 241 startAngle-=angleDecrement 242 context.restore() 243 } 244 // 繪製印章公司 245 context.font = '14px SimSun'; 246 context.lineWidth=1; 247 context.fillStyle = sealData.sealColor; 248 context.fillText(sealData.sealCompany.substring(0,6),width-11,height+6); 249 context.save(); 250 context.fillText(sealData.sealCompany.substring(6,12),width-12,height+25); 251 context.save(); 252 context.fillText(sealData.sealCompany.substring(12,sealData.sealCompany.length),width-12,height+40); 253 context.save(); 254 255 // 繪製印章類型 256 context.font = '10px SimSun'; 257 context.lineWidth=1; 258 context.fillStyle = sealData.sealColor; 259 context.fillText(sealData.sealType,width-10,height+55); 260 context.save(); 261 262 } 263 264 265 266 267 } 268 269 270 271 //圓形印章 272 function createCircularSeal(context,sealData){ 273 //繪製印章邊框1 274 var width=canvas.width/2; 275 var height=canvas.height/2; 276 //-------------------最外圈兩個圓 開始------------------------------------------------------ 277 context.lineWidth=2; 278 context.strokeStyle=sealData.sealColor; 279 context.beginPath(); 280 //arc(x, y, radius, startRad, endRad, anticlockwise) 281 //在canvas畫布上繪製以座標點(x,y)爲圓心、 282 //半徑爲radius的圓上的一段弧線。 283 //起始弧度是startRad. 284 //結束弧度是endRad。 285 //anticlockwise表示是以逆時針方向仍是順時針方向開始繪製,若是爲true則表示逆時針,若是爲false則表示順時針。anticlockwise參數是可選的,默認爲false,即順時針。 286 //以方形中心爲圓心 畫半徑爲邊長一半減2px的園 287 context.arc(width,height,width-2,0,Math.PI*2, false); 288 context.stroke(); 289 context.save(); 290 //繪製印章邊框2 291 context.lineWidth=1; 292 context.strokeStyle=sealData.sealColor; 293 context.beginPath(); 294 //圓的半徑在邊長一半基礎上減去5px 295 context.arc(width,height,width-5,0,Math.PI*2, false); 296 context.stroke(); 297 context.save(); 298 //-------------------最外圈兩個圓 結束------------------------------------------------------- 299 300 301 if(sealData.sealEnglish=='1'){ 302 //只含有中文 303 //-------------------------畫中文環繞 開始--------------------------------------------------- 304 //控制字符起始位置度數 305 var startAngle=240; 306 //首位字符相隔度數 307 var endAngle =-40; 308 //圓的半徑在邊長一半基礎上減去23px 309 var radius=width-25-sealData.sealSpacing ; 310 311 //每一個字母佔的弧度 312 var angleDecrement=(startAngle-endAngle)/(sealData.sealCompany.length-1); 313 context.font="18px SimSun"; 314 var index=0; 315 for(var index=0;index<sealData.sealECompany.length;index++){ 316 context.save(); 317 context.beginPath(); 318 //繪製點 319 context.translate(width+Math.cos((Math.PI/180)*startAngle)*radius, 320 height-Math.sin((Math.PI/180)*startAngle)*radius); 321 //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 322 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle); 323 context.fillText(sealData.sealCompany.charAt(index),0,0); 324 context.strokeText(sealData.sealCompany.charAt(index),0,0); 325 startAngle-=angleDecrement; 326 context.restore(); 327 } 328 //-------------------------畫中文環繞 結束---------------------------------------------------- 329 330 }else if(sealData.sealEnglish=='2'){ 331 //含中英文 332 //-------------------------畫英文環繞 開始--------------------------------------------------- 333 //控制字符起始位置度數 334 var startAngle=240; 335 //首位字符相隔度數 336 var endAngle =-40; 337 //圓的半徑在邊長一半基礎上減去16px 338 var radius=width-16 ; 339 //每一個字母佔的弧度 340 var angleDecrement=(startAngle-endAngle)/(sealData.sealECompany.length-1); 341 context.font="11px SimSun"; 342 var index=0; 343 for(var index=0;index<sealData.sealECompany.length;index++){ 344 context.save(); 345 context.beginPath(); 346 //繪製點 347 context.translate(width+Math.cos((Math.PI/180)*startAngle)*radius, 348 height-Math.sin((Math.PI/180)*startAngle)*radius); 349 //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 350 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle); 351 context.fillText(sealData.sealECompany.charAt(index),0,0); 352 context.strokeText(sealData.sealECompany.charAt(index),0,0); 353 startAngle-=angleDecrement; 354 context.restore(); 355 } 356 //-------------------------畫英文環繞 結束---------------------------------------------------- 357 358 //-------------------------畫內圓 開始-------------------------------------------------------- 359 //繪製印章邊框3 360 context.lineWidth=1; 361 context.strokeStyle=sealData.sealColor; 362 context.beginPath(); 363 //圓的半徑在邊長一半基礎上減去20px再減去用戶設置的內間距 364 context.arc(width,height,width-20-sealData.sealSpacing,0,Math.PI*2, false); 365 context.stroke(); 366 context.save(); 367 //-------------------------畫內圓 結束--------------------------------------------------------- 368 369 //-------------------------畫中文環繞 開始--------------------------------------------------- 370 startAngle=240; 371 //圓的半徑在邊長一半基礎上減去37px 再減去用戶設置的間隔 372 radius=width-37-sealData.sealSpacing; 373 //每一個字母佔的弧度 374 var angleDecrement=(startAngle-endAngle)/(sealData.sealCompany.length-1); 375 context.font="15px SimSun"; 376 var index=0; 377 for(var index=0;index<sealData.sealECompany.length;index++){ 378 context.save(); 379 context.beginPath(); 380 //繪製點 381 context.translate(width+Math.cos((Math.PI/180)*startAngle)*radius, 382 height-Math.sin((Math.PI/180)*startAngle)*radius); 383 //Math.PI/2爲旋轉90度 Math.PI/180*X爲旋轉多少度 384 context.rotate((Math.PI/2)-(Math.PI/180)*startAngle); 385 context.fillText(sealData.sealCompany.charAt(index),0,0); 386 context.strokeText(sealData.sealCompany.charAt(index),0,0); 387 startAngle-=angleDecrement; 388 context.restore(); 389 } 390 //-------------------------畫中文環繞 結束---------------------------------------------------- 391 392 } 393 394 395 396 //-----------------畫五角星 開始---------------------------------------------------------------- 397 context.save(); 398 context.fillStyle=sealData.sealColor; 399 //移動座標圓心原點 400 context.translate(width,height); 401 //旋轉 402 context.rotate(Math.PI); 403 //建立路徑 404 context.beginPath(); 405 //畫五角星的五條邊 406 for(var i = 0;i< 5;i++){ 407 context.lineTo(Math.sin(i*(Math.PI/5 *4))*20,Math.cos(i*(Math.PI/5 *4))*20); 408 } 409 context.closePath(); 410 context.stroke(); 411 context.fill(); 412 context.restore(); 413 //-----------------畫五角星 結束------------------------------------------------------------------- 414 415 //-----------------繪製印章類型 開始---------------------------------------------------------------- 416 context.font = '12px SimSun'; 417 context.textBaseline = 'middle';//設置文本的垂直對齊方式 418 context.textAlign = 'center'; //設置文本的水平對對齊方式 419 context.lineWidth=1; 420 context.fillStyle = sealData.sealColor; 421 context.fillText(sealData.sealType,width,height+30); 422 //-----------------繪製印章類型 結束---------------------------------------------------------------- 423 424 425 426 427 }; 428 429 function BezierEllipse4(ctx, x, y, a, b){ 430 var k = .5522848, 431 ox = a * k, // 水平控制點偏移量 432 oy = b * k; // 垂直控制點偏移量</p> <p> 433 ctx.beginPath(); 434 //從橢圓的左端點開始順時針繪製四條三次貝塞爾曲線 435 ctx.moveTo(x - a, y); 436 ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b); 437 ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y); 438 ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b); 439 ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y); 440 ctx.closePath(); 441 ctx.stroke(); 442 }; 443 444 </script> 445 446 </body> 447 </html>