HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章

Wesley13
• 阅读 475

电子签章图片采集

印章图片的采集两种互补方式

方式1:在线生成印章图片方式,但是这种方式有个弊端,对印章中公司名称字数有限制,字数越多可能就完蛋了。

方式2:上传印章扫描件,系统来对扫描图片进行处理,提取扫描件中的印章图片。

本文介绍方式1,方式2待后续发布,方式1本来想用java实现印章图片生成,虽然网上有很多现成代码,但需要调整印章图片大小达到规范,印章大小:圆形印章尺寸43mm*43mm(误差允许范围2-3mm),椭圆印章43mm*26mm(误差允许范围2-3mm)比较接近真实印章。****想到java调试起来比较费劲,所以改用html5实现

html5实现圆章,椭圆章思路:

HTML5 标签用于绘制图像(通过脚本,通常是 JavaScript),canvas进行印章绘画,然后通过canvas生成印章图片然后转成base64图片。

 难点:

   椭圆章的曲线文字比较难搞,虽然网上有JQ的js可以直接生成曲线字符排列,但是却无法转换成canvas。

解决:

先把文字圆形排列,然后文字圆进行缩放(均匀压缩)。

先上效果图:

             圆形中英文圆形印章        中文圆形印章          椭圆中英文印章             椭圆中文印章

        HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章              HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章           HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章     HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章

硬货

代码:

圆形

  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动态配置印章设置

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>

原文出处:https://www.cnblogs.com/yk-1992/p/10964665.html

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这