手写毛笔字效果-手机版

原创 2016年03月18日 23:19:47
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta name="viewport" content="width=400">
	<title> canvas手写毛笔字效果 </title>
<style type="text/css">
body {
	margin: 0;
	padding: 0;
	background-color: #999999;
}

h1 {
	text-align: center;
}

#canvasId {
	background-color: #FFFFcc;
}

.button {
	width: 100px;
	height: 40px;
}
</style>
</head>
<body>
<h1>手写毛笔字效果-手机版</h1>
<canvas id="canvasId" width="400" height="400"></canvas><br />
<input type="button" value="全部清除" class="button" onclick="hw.clear();" />
<input type="button" value="清除最后一笔" class="button" onclick="hw.historyBack();" />
<script type="text/javascript">
function Handwriting(id) {
	this.canvas = document.getElementById(id);
	this.ctx = this.canvas.getContext("2d");
	this.ctx.fillStyle = "rgba(0,0,0,0.25)";
	this.canvas.addEventListener("touchstart", this.downEvent.bind(this), false);
	this.canvas.addEventListener("touchmove", this.moveEvent.bind(this), false);
	this.canvas.addEventListener("touchend", this.upEvent.bind(this), false);
	this.canvas.addEventListener("contextmenu", function(e){ e.preventDefault() }, false);
	//this.canvas.onmousedown = this.downEvent.bind(this);
	//this.canvas.onmousemove = this.moveEvent.bind(this);
	//this.canvas.onmouseup = this.upEvent.bind(this);
	//this.canvas.onmouseout = this.upEvent.bind(this);
	this.moveFlag = false;
	this.upof = {};
	this.radius = 0;
	this.has = [];
	this.startOf = null;
	this.lineMax = 30;
	this.lineMin = 3;
	this.linePressure = 1;
	this.smoothness = 80;
	this.history = [];
}

Handwriting.prototype.clear = function () {
	this.history = [];
	this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
}

Handwriting.prototype.historyBack = function () {
	this.history.pop();
	this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
	for (var i = 0; i < this.history.length; i++) {
		var h = this.history[i];
		for (var j = 0; j < h.length; j+=3) {
			this.ctx.beginPath();
			this.ctx.arc(h[j],h[j+1],h[j+2],0,2*Math.PI,true);
			this.ctx.fill();
		}
	}
}

Handwriting.prototype.downEvent = function (e) {
	this.moveFlag = true;
	this.has = [];
	this.history.push([]);
	this.upof = this.getXY(e);
	this.startOf = this.upof;
}

Handwriting.prototype.moveEvent = function (e) {
	if (!this.moveFlag)
		return;
	e.preventDefault();
	var of = this.getXY(e);
	var up = this.upof;
	var ur = this.radius;
	this.has.unshift({time:new Date().getTime() ,dis:this.distance(up,of)});
	var dis = 0;
	var time = 0;
	for (var n = 0; n < this.has.length-1; n++) {
		dis += this.has[n].dis;
		time += this.has[n].time-this.has[n+1].time;
		if (dis>this.smoothness)
			break;
	}
	var or = Math.min(time/dis*this.linePressure+this.lineMin , this.lineMax) / 2;
	this.radius = or;
	this.upof = of;
	if (dis<7)
		return;
	if (this.startOf) {
		up = this.startOf;
		ur = or;
		this.startOf = null;
	}
	var len = Math.ceil(this.distance(up,of)/2);
	for (var i = 0; i < len; i++) {
		var x = up.x + (of.x-up.x)/len*i;
		var y = up.y + (of.y-up.y)/len*i;
		var r = ur + (or-ur)/len*i;
		this.ctx.beginPath();
		this.ctx.arc(x,y,r,0,2*Math.PI,true);
		this.ctx.fill();
		this.history[this.history.length-1].push(x,y,r);
	}
}

Handwriting.prototype.upEvent = function (e) {
	this.moveFlag = false;
}

Handwriting.prototype.getXY = function (e) {
	var x = e.touches[0].clientX;
	var y = e.touches[0].clientY;
	//var x = e.clientX;
	//var y = e.clientY;
	return {
		x : x - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
		y : y - this.canvas.offsetTop  + (document.body.scrollTop || document.documentElement.scrollTop)
	}
}

Handwriting.prototype.distance = function (a,b) {
	var x = b.x-a.x , y = b.y-a.y;
	return Math.sqrt(x*x+y*y);
}

var hw = new Handwriting("canvasId");
hw.lineMax = 40;
hw.lineMin = 5;
hw.linePressure = 3.1;
hw.smoothness = 100;
</script>
</body>
</html>


在线演示:https://js-game.github.io/Handwriting/handwriting.html

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

canvas 手写毛笔字效果

canvas 手写毛笔字效果 #canvasId { background-color: #FFFFcc; } function Handwriting(id) { this....
  • jslang
  • jslang
  • 2016年03月03日 18:47
  • 4276

wpf inkcanvas customink 毛笔效果

用wpf来做毛笔书写效果,需要自定义inkcanvas,基本效果可以,就是连续画很长的笔触时会有卡顿现象。解决方法,做一个判断,当笔触超过一定长度则停止。 核心代码: using System;...

使用WPF的自定义InkCanvas实现毛笔效果

首先找到了一图类似于毛笔笔触

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

使用贝塞尔曲线算法实现毛笔签名效果

最近项目中有个需要签名的地方,要用到手写签名,开始只是简单的实现手写签名,如图: 后来领导说,能不能实现像毛笔那样签名的效果,那好吧,领导说怎样就怎样吧,而且我也觉得这里用毛笔效果会更好些。那就只好...

自定义view—Canvas实现手写板和涂鸦功能

学习导航第一节:http://blog.csdn.net/bobo8945510/article/details/53197727 —自定义View—自定义属性及引用第二节:http://blog.c...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

android中实现毛笔效果(View 中绘图)

最近有一个项目设计一个APP实现通过触摸屏实现毛笔写字效果。传统的绘画板程序直接通过Path的moveTo和LineTo便可实现简单的线条绘画程序。然而要达到毛笔的笔锋效果则需要更为详细点的设计。我的...

JS判断客户端是否已安装ActiveX控件

if(document.all.player.object == null) { alert("请先安装播放器控件!") }
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:手写毛笔字效果-手机版
举报原因:
原因补充:

(最多只允许输入30个字)