用 canvas 原生JS创建红色公章

javascript 复制代码
function officialseal(opt) {
	opt = opt || {
		canvas: $$('#canvas'),
		companyName: canvas.getAttribute('data-companyName'),
		sealTypeName: canvas.getAttribute('data-sealTypeName')
	};
	
	// official seal
	var ctx = opt['canvas'].getContext('2d'), color = '#f00', width = opt['canvas'].width/2, height = opt['canvas'].height/2;
	ctx.lineWidth = 6;
	ctx.strokeStyle = color;
	ctx.beginPath();
	ctx.arc(width, height, 110, 0, Math.PI*2);
	ctx.stroke();

	// five pointed star
	var radius = 30, rotato = 0;
	ctx.save();
	ctx.fillStyle = color;
	ctx.translate(width, height);
	ctx.rotate(Math.PI+rotato);
	ctx.beginPath();
	var x, y, dig = Math.PI/5*4;
	for (var i = 0;i< 5;i++) {
		 x = Math.sin(i*dig);
		 y = Math.cos(i*dig);
		 ctx.lineTo(x*radius, y*radius);
	}
	ctx.closePath();
	ctx.stroke();
	ctx.fill();
	ctx.restore();
	
	// sealTypeName
	ctx.font = '20px Microsoft YaHei';
	ctx.textBaseline = 'middle';
	ctx.textAlign = 'center';
	ctx.lineWidth = 1;
	ctx.fillStyle = color;
	ctx.fillText(opt['sealTypeName'], width, height+70);

	// companyName
	ctx.translate(width, height);
	ctx.font = '26px Microsoft YaHei';
	var  chars = opt['companyName'].split('')
		, count = chars.length
		, angle = 4*Math.PI / (3*(count - 1))
	;
	for (var i = 0; i < chars.length; i++){ // var character = chars[i];
		if (i == 0) {
			ctx.rotate(5*Math.PI/6);
		} else {
			ctx.rotate(angle);
		}
		ctx.save(); 
		ctx.translate(90, 0);
		ctx.rotate(Math.PI/2);
		ctx.fillText(chars[i], 0, 5);
		ctx.restore(); 
	}
}
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width,user-scalable=yes" />
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<title>official_seal</title>
<script src="/js/u.js?v=1"></script>
</head>
<body onclick="clickHandler(event);" onchange="changeHandler(event);">
<canvas id="canvas" width="250" height="250" data-companyName="郑州快马科技有限公司" data-sealTypeName="测试专用章"></canvas>
<script src="official_seal.js?v=1"></script>
</body>
</html>
相关推荐
zhoxier4 小时前
elementui el-select 获取value和label 以及 对象的方法
javascript·vue.js·elementui
是小狐狸呀4 小时前
elementUI-表单-下拉框数据选中后,视图不更新
前端·javascript·elementui
IT码农-爱吃辣条8 小时前
Three.js 初级教程大全
开发语言·javascript·three.js
烛阴8 小时前
告别繁琐的类型注解:TypeScript 类型推断完全指南
前端·javascript·typescript
gnip9 小时前
工程项目中.env 文件原理
前端·javascript
JohnYan9 小时前
工作笔记 - CentOS7环境运行Bun应用
javascript·后端·容器
芜青10 小时前
HTML+CSS:浮动详解
前端·css·html
东风西巷11 小时前
Rubick:基于Electron的开源桌面效率工具箱
前端·javascript·electron·软件需求
Miracle_G12 小时前
每日一个知识点:JavaScript 箭头函数与普通函数比较
javascript
unfetteredman12 小时前
Error: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
前端·javascript·vite