87,993
社区成员
发帖
与我相关
我的任务
分享
<!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">
<style type="text/css">
body {
margin: 0;
padding: 0;
text-align: center;
width: 100%;
/*background-color: #936;*/
}
#canvasId {
background-color: #FFF;
}
.button {
width: 100px;
height: 40px;
font-size: 15px;
}
/*#imgDiv img{border:2px dashed #238E1B;}*/
</style>
</head>
<body>
<!--<h1>写字版</h1>-->
<canvas id="canvasId" width="400" height="500" style="border: 1px solid #7462CF"></canvas><br />
<input type="button" value="保存" class="button" onclick="hw.save();" />
<input type="button" value="清除写字板" class="button" onclick="hw.clear();" />
<input type="button" value="全部清除" class="button" onclick="hw.clearAll();" />
<input type="button" value="全部保存" class="button" onclick="hw.saveAll();" />
<div></div>
<canvas id="saveImgs" width="400" height="100" style="border:1px green solid"></canvas>
<script src="../JS/jquery-1.11.1.js"></script>
<script type="text/javascript">
function Handwriting(id) {
this.canvas = document.getElementById(id);
this.ctx = this.canvas.getContext("2d");
var on = ("ontouchstart" in document) ? {
start: "touchstart", move: "touchmove", end: "touchend"
} : {
start: "mousedown", move: "mousemove", end: "mouseup"
};
this.canvas.addEventListener(on.start, this.downEvent.bind(this), false);
this.canvas.addEventListener(on.move, this.moveEvent.bind(this), false);
this.canvas.addEventListener(on.end, this.upEvent.bind(this), false);
this.canvas.addEventListener("contextmenu", function (e) { e.preventDefault() }, false);
this.moveFlag = false;
this.upof = {};
this.radius = 0;
this.has = [];
this.startOf = null;
this.lineMax = 30;
this.lineMin = 10;
this.linePressure = 1;
this.smoothness = 80;
this.history = [];
this.setColor("rgba(0,0,0,0.25)");
this.SavedImageCount = 0;
this.saveCanvas = document.getElementById('saveImgs');
}
Handwriting.prototype.save = function () {
var imgData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height).data;
var lOffset = this.canvas.width, rOffset = 0, tOffset = this.canvas.height, bOffset = 0;
var hasData = false;
for (var i = 0; i < this.canvas.width; i++) {
for (var j = 0; j < this.canvas.height; j++) {
var pos = (i + this.canvas.width * j) * 4
if (imgData[pos] > 0 || imgData[pos + 1] > 0 || imgData[pos + 2] || imgData[pos + 3] > 0) {
bOffset = Math.max(j, bOffset); // 找到有色彩的最下端
rOffset = Math.max(i, rOffset); // 找到有色彩的最右端
tOffset = Math.min(j, tOffset); // 找到有色彩的最上端
lOffset = Math.min(i, lOffset); // 找到有色彩的最左端
hasData = true;
}
}
}
lOffset++;
rOffset++;
tOffset++;
bOffset++;
var c = this.saveCanvas;
var imgWidth = rOffset - lOffset, imgHeight = bOffset - tOffset;
//转为100*100大小
var scale = Math.max(imgWidth, imgHeight) / 100;
//目标大小
var oWidth = Math.floor(imgWidth / scale), oHeight = Math.floor(imgHeight / scale);
//仅当大小大于10时才保存
if (hasData && imgWidth > 10 && imgHeight > 10) {
//目标位置
var oX = Math.floor((this.SavedImageCount % 4) * 100);
var oY = Math.floor(Math.floor(this.SavedImageCount / 4) * 100);
var ctxs = c.getContext("2d");
//根据需要增加高度
if (oY + 100 > Number($(c).attr('height'))) {
var imageData = ctxs.getImageData(0, 0, c.width, c.height);// 保存当前图像
$(c).attr('height', Number($(c).attr('height')) + 100);
ctxs.putImageData(imageData, 0, 0); //重绘大小改变前保存的图像
}
//修正位置,绘制在中间
var oX = Math.floor(oX + (100 - oWidth) / 2);
var oY = Math.floor(oY + (100 - oHeight) / 2);
ctxs.drawImage(this.canvas, lOffset, tOffset, imgWidth, imgHeight, oX, oY, oWidth, oHeight);
this.SavedImageCount++;
}
this.history = [];
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
Handwriting.prototype.clear = function () {
this.history = [];
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
Handwriting.prototype.clearAll = function () {
this.history = [];
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.saveCanvas.height = 100;
this.SavedImageCount = 0;
}
Handwriting.prototype.saveAll = function () {
}
//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.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;
this.history.push([]);
}
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 et = e.touches ? e.touches[0] : e;
var x = et.clientX;
var y = et.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);
}
Handwriting.prototype.setColor = function (c) {
this.ctx.fillStyle = c;
}
var hw = new Handwriting("canvasId");
hw.setColor("black");//设置画笔颜色
hw.lineMax = 30;//设置画笔最大线宽
hw.lineMin = 10;//设置画笔最小线宽
hw.linePressure = 1.2;//设置画笔笔触压力
hw.smoothness = 30;//设置画笔笔触大小变化的平滑度。
</script>
</body>
</html>
Handwriting.prototype.save = function () {
var imgData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height).data;
var lOffset = this.canvas.width, rOffset = 0, tOffset = this.canvas.height, bOffset = 0;
var hasData = false;
for (var i = 0; i < this.canvas.width; i++) {
for (var j = 0; j < this.canvas.height; j++) {
var pos = (i + this.canvas.width * j) * 4
if (imgData[pos] > 0 || imgData[pos + 1] > 0 || imgData[pos + 2] || imgData[pos + 3] > 0) {
bOffset = Math.max(j, bOffset); // 找到有色彩的最下端
rOffset = Math.max(i, rOffset); // 找到有色彩的最右端
tOffset = Math.min(j, tOffset); // 找到有色彩的最上端
lOffset = Math.min(i, lOffset); // 找到有色彩的最左端
hasData = true;
}
}
}
lOffset++;
rOffset++;
tOffset++;
bOffset++;
var c = document.getElementById("saveImgs");
var imgWidth = rOffset - lOffset, imgHeight = bOffset - tOffset;
//转为100*100大小
var scale = Math.max(imgWidth, imgHeight) / 100;
//目标大小
var oWidth = Math.floor(imgWidth / scale), oHeight = Math.floor(imgHeight / scale);
//仅当大小大于10时才保存
if (hasData && oWidth > 10 && oHeight > 10) {
//目标位置
var oX = Math.floor((this.SavedImageCount % 4) * 100);
var oY = Math.floor(Math.floor(this.SavedImageCount / 4) * 100);
var ctxs = c.getContext("2d");
//根据需要增加高度
if (oY + 100 > Number($(c).attr('height'))) {
var imageData = ctxs.getImageData(0, 0, c.width, c.height);// 保存当前图像
$(c).attr('height', Number($(c).attr('height')) + 100);
ctxs.putImageData(imageData, 0, 0); //重绘大小改变前保存的图像
}
//修正位置,绘制在中间
var oX = Math.floor(oX + (100 - oWidth) / 2);
var oY = Math.floor(oY + (100 - oHeight) / 2);
ctxs.drawImage(this.canvas, lOffset, tOffset, imgWidth, imgHeight, oX, oY, oWidth, oHeight);
this.SavedImageCount++;
}
this.history = [];
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}