还是以前闲时写的东西
简单的网页绘图,划线,画直线,画框框,画圈,可以改变线的颜色宽度和填充的颜色
可以撤销、恢复、以及清空
主要使用的是canvas
效果如下(视频转的动图,后面有部分没有转出来 TAT )
写的不怎么的,还有些bug,反正没人要求,我自己写的开心写的好玩~诶 就是玩~
下附代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
canvas {
border: 1px solid #ADADAD;
}
</style>
</head>
<body>
<div>线宽度:<input type="number" min="0" id="tdlinewidth" value="2"></div>
<div>线颜色:<input type="color" value="#000000" id="tdfillcolor"></div>
<div>填充颜色:<input type="color" value="#FF00FF" id="tdcolor"></div>
<div>
<input type="radio" name="1" id="defort" value="默认" checked="" />默认
<input type="radio" name="1" id="line" value="直线" />直线
<input type="radio" name="1" id="ju" value="矩形" />矩形(按住shift画正方形)
<input type="radio" name="1" id="yuan" value="椭圆" />圆型(按住shift画正圆)
</div>
<div>
<button onclick="returnnew(true)">上一步</button>
<button onclick="huifu()">恢复</button>
<button onclick="clearcanvas()">清空</button>
</div>
<canvas id="mian" width="500" height="500"></canvas>
<script type="text/javascript">
var c = document.getElementById("mian");
var ctx = c.getContext("2d");
var point = []; //历史记录
var nextpoint = []; //恢复记录
var nowpoint = {
style: "",
point: [],
linewidth: 10,
fillcolor: ""
};
$("#mian").on('mousedown', function() {
var move = true;
nextpoint = [];
ctx.beginPath();
ctx.lineCap = "round";
ctx.lineJoin = 'round'; //转折的时候不出现尖角
ctx.strokeStyle = $("#tdfillcolor").val();
ctx.lineWidth = $("#tdlinewidth").val();
if ($("#defort:checked").length == 1) nowpoint = {
style: "defort",
point: [],
linewidth: ctx.lineWidth,
fillcolor: ctx.strokeStyle
};
if ($("#line:checked").length == 1) nowpoint = {
style: "line",
point: [],
linewidth: ctx.lineWidth,
fillcolor: ctx.strokeStyle
};
if ($("#ju:checked").length == 1)
nowpoint = {
style: "ju",
point: [],
linewidth: ctx.lineWidth,
fillcolor: ctx.strokeStyle,
color: $("#tdcolor").val()
};
if ($("#yuan:checked").length == 1) {
nowpoint = {
style: "yuan",
point: [],
linewidth: ctx.lineWidth,
fillcolor: ctx.strokeStyle,
color: $("#tdcolor").val()
};
}
var uppoint = creadepoint(event.clientX - $(this).offset().left, event.clientY - $(this).offset().top);
$("#mian").on('mousemove', function(event1) {
if (move) {
if (event1.shiftKey == 1) {
nowpoint.iszheng = true
}else{
nowpoint.iszheng = false;
}
uppoint = creadepoint(event1.clientX - $(this).offset().left, event1.clientY - $(this).offset().top);
}
})
var add = false;
$("#mian").on('mouseout', function() {
move = false;
ctx.stroke();
if (!add) {
point.push(nowpoint);
add = true;
}
})
$("#mian").on('mouseup', function() {
move = false;
ctx.stroke();
if (!add) {
point.push(nowpoint);
add = true;
}
})
})
//创建
var creadepoint = function(x, y) {
if ($("#defort:checked").length == 1) {
ctx.lineTo(x, y);
ctx.stroke();
nowpoint.point.push([x, y]);
}
if ($("#line:checked").length == 1) {
returnnew();
if (nowpoint.point.length != 0) {
ctx.moveTo(nowpoint.point[0][0], nowpoint.point[0][1]);
ctx.lineTo(x, y);
ctx.stroke();
}
nowpoint.point.push([x, y]);
}
if ($("#ju:checked").length == 1) {
returnnew();
if (nowpoint.point.length != 0) {
ctx.fillStyle = $("#tdcolor").val();
if (nowpoint.iszheng != true) {
ctx.fillRect(nowpoint.point[0][0], nowpoint.point[0][1], x - nowpoint.point[0][0], y - nowpoint.point[0][1]);
ctx.rect(nowpoint.point[0][0], nowpoint.point[0][1], x - nowpoint.point[0][0], y - nowpoint.point[0][1]);
} else {
ctx.fillRect(nowpoint.point[0][0], nowpoint.point[0][1], x - nowpoint.point[0][0], x - nowpoint.point[0][0]);
ctx.rect(nowpoint.point[0][0], nowpoint.point[0][1], x - nowpoint.point[0][0], x - nowpoint.point[0][0]);
}
nowpoint.point[1] = [x, y];
} else nowpoint.point[0] = [x, y];
}
if ($("#yuan:checked").length == 1) {
returnnew();
if (nowpoint.point.length != 0) {
ctx.fillStyle = $("#tdcolor").val();
if (nowpoint.iszheng != true) {
ctx.moveTo(nowpoint.point[0][0] + Math.abs(x - nowpoint.point[0][0]), nowpoint.point[0][1]) + Math.abs(y -
nowpoint.point[0]
[0]);
ctx.ellipse(nowpoint.point[0][0], nowpoint.point[0][1], Math.abs(x - nowpoint.point[0][0]),
Math.abs(y - nowpoint.point[0][1]), 0, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.moveTo(nowpoint.point[0][0] + Math.abs(x - nowpoint.point[0][0]), nowpoint.point[0][1]) + Math.abs(x -
nowpoint.point[0]
[0]);
ctx.ellipse(nowpoint.point[0][0], nowpoint.point[0][1], Math.abs(x - nowpoint.point[0][0]),
Math.abs(x - nowpoint.point[0][0]), 0, 0, Math.PI * 2);
ctx.fill();
}
nowpoint.point[1] = [x, y];
} else nowpoint.point[0] = [x, y];
}
return [x, y];
}
//恢复
var huifu = function() {
if (nextpoint.length == 0) return;
var e = nextpoint[nextpoint.length - 1];
var fristline = $("#tdlinewidth").val();
var fristfill = $("#tdfillcolor").val();
ctx.beginPath();
ctx.lineCap = "round";
ctx.lineJoin = 'round';
ctx.strokeStyle = $("#tdfillcolor").val(e.fillcolor).val();
ctx.lineWidth = $("#tdlinewidth").val(e.linewidth).val();
if (e.style == "defort") {
$.each(e.point, function(j, p) {
ctx.lineTo(p[0], p[1]);
ctx.stroke();
})
}
if (e.style == "line") {
ctx.moveTo(e.point[0][0], e.point[0][1]);
ctx.lineTo(e.point[e.point.length - 1][0], e.point[e.point.length - 1][1]);
ctx.stroke();
}
if (e.style == "ju") {
var fristclor = $("#tdcolor").val();
ctx.fillStyle = $("#tdcolor").val(e.color).val();
if (e.iszheng != true) {
ctx.fillRect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][1] - e.point[0][1]);
ctx.rect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][1] - e.point[0][1]);
} else {
ctx.fillRect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][0] - e.point[0][0]);
ctx.rect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][0] - e.point[0][0]);
}
ctx.stroke();
$("#tdcolor").val(fristclor);
}
if (e.style == "yuan") {
var fristclor = $("#tdcolor").val();
ctx.fillStyle = $("#tdcolor").val(e.color).val();
if (e.iszheng != true) {
ctx.ellipse(e.point[0][0], e.point[0][1], Math.abs(e.point[1][0] - e.point[0][0]),
Math.abs(e.point[1][1] - e.point[0][1]), 0, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.ellipse(e.point[0][0], e.point[0][1], Math.abs(e.point[1][0] - e.point[0][0]),
Math.abs(e.point[1][0] - e.point[0][0]), 0, 0, Math.PI * 2);
ctx.fill();
}
ctx.stroke();
$("#tdcolor").val(fristclor);
}
point.push(e);
nextpoint.length = nextpoint.length - 1;
$("#tdlinewidth").val(fristline);
$("#tdfillcolor").val(fristfill);
}
//撤销
var returnnew = function(ret) {
c.width = c.width;
var fristline = $("#tdlinewidth").val();
var fristfill = $("#tdfillcolor").val();
$.each(point, function(i, e) {
//撤销上一步 记录在恢复里面
if (ret && i == (point.length - 1)) {
nextpoint.push(e);
point.length = point.length - 1;
} else {
ctx.beginPath();
ctx.lineCap = "round";
ctx.lineJoin = 'round';
ctx.strokeStyle = $("#tdfillcolor").val(e.fillcolor).val();
ctx.lineWidth = $("#tdlinewidth").val(e.linewidth).val();
if (e.style == "defort") {
$.each(e.point, function(j, p) {
ctx.lineTo(p[0], p[1]);
ctx.stroke();
})
}
if (e.style == "line") {
ctx.moveTo(e.point[0][0], e.point[0][1]);
ctx.lineTo(e.point[e.point.length - 1][0], e.point[e.point.length - 1][1]);
ctx.stroke();
}
if (e.style == "ju") {
var fristclor = $("#tdcolor").val();
ctx.fillStyle = $("#tdcolor").val(e.color).val();
if (e.iszheng != true) {
ctx.fillRect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][1] - e.point[0][1]);
ctx.rect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][1] - e.point[0][1]);
} else {
ctx.fillRect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][0] - e.point[0][0]);
ctx.rect(e.point[0][0], e.point[0][1], e.point[1][0] - e.point[0][0], e.point[1][0] - e.point[0][0]);
}
ctx.stroke();
$("#tdcolor").val(fristclor);
}
if (e.style == "yuan") {
var fristclor = $("#tdcolor").val();
ctx.fillStyle = $("#tdcolor").val(e.color).val();
if (e.iszheng != true) {
ctx.ellipse(e.point[0][0], e.point[0][1], Math.abs(e.point[1][0] - e.point[0][0]),
Math.abs(e.point[1][1] - e.point[0][1]), 0, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.ellipse(e.point[0][0], e.point[0][1], Math.abs(e.point[1][0] - e.point[0][0]),
Math.abs(e.point[1][0] - e.point[0][0]), 0, 0, Math.PI * 2);
ctx.fill();
}
ctx.stroke();
$("#tdcolor").val(fristclor);
}
}
})
$("#tdlinewidth").val(fristline);
$("#tdfillcolor").val(fristfill);
}
var clearcanvas = function() {
c.width = c.width;
point = []; //上一步记录
nextpoint = []; //恢复
}
</script>
</body>
</html>