Canvas简单画板
实现功能:canvas画板。 可拖动,可缩放
用JQuery实现 事件, Jquery实现的 缩放和拖放
,所以需要引入相关的JS文件。
下面是代码部分:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-1.8.0.js"></script>
<script src="jquery-ui-1.9.0.custom.js"></script>
<link href="jquery-ui-1.9.0.custom.css" rel="stylesheet" />
<style type="text/css">
.common:hover {
background: rgba(205,214,169,0.4);
}
.common {
background-color: rgba(0,0,0,0);
}
canvas {
}
</style>
<script type="text/javascript">
function Point(x, y) { this.x = x; this.y = y; }
var main = {
points: [],
statue: false
}
$(function () {
var canvasOffset = $("#canvas").offset();
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "#2c45a0";
ctx.lineWidth = 2;
$("#canvas").mousedown(function (e) {
main.statue = true;
main.points.length = 0;
var bpx = (e.pageX - canvasOffset.left) || 0;
var bpy = (e.pageY - canvasOffset.top) || 0;
main.points.push(new Point(bpx, bpy));
ctx.beginPath();
ctx.moveTo(bpx, bpy); ctx.stroke();
})
$("#canvas").mousemove(function (e) {
if (main.statue) {
var cpx = (e.pageX - canvasOffset.left) || 0;
var cpy = (e.pageY - canvasOffset.top) || 0;
main.points.push(new Point(cpx, cpy));
ctx.lineTo(cpx, cpy);
ctx.stroke();
}
})
$('#canvas').mouseup(function (e) {
main.statue = false;
var xc = new Array(), yc = new Array();
for (var i = 0; i < main.points.length; i++) {
xc.push(main.points[i].x);
yc.push(main.points[i].y);
}
var maxx = Math.max.apply(null, xc),
maxy = Math.max.apply(null, yc),
minx = Math.min.apply(null, xc),
miny = Math.min.apply(null, yc),
leftTopPoint = new Point(minx, miny),
rightBottomPoint = new Point(maxx, maxy);
console.log("(" + leftTopPoint.x + ":" + leftTopPoint.y + ")" + "-->(" + rightBottomPoint.x + ":" + rightBottomPoint.y + ")");
var height = parseInt(rightBottomPoint.y - leftTopPoint.y),
width = parseInt(rightBottomPoint.x - leftTopPoint.x),
top = parseInt(leftTopPoint.y + canvasOffset.top) ,
left = parseInt(leftTopPoint.x + canvasOffset.left);
//创建div
var div = document.createElement("div");
div.className = "common"
var s = div.style;
s.height = height+"px";
s.width = width+"px";
// s.border = "1px solid red";
s.position = "absolute";
s.top = top+"px";
s.left = left+"px";
s.zIndex = 100 + $("canvas").length;
//创建canvas副本
var can = document.createElement("canvas");
can.height = height;
can.width = width;
//加入dom中
div.appendChild(can);
document.getElementsByTagName("body")[0].appendChild(div);
var zindex;
// $(can).draggable();
$(div).draggable({
start: function (a, b) {
zindex = $(this).css("zIndex");
$(this).css("zIndex", 10006);
},
stop: function (a, b) { $(this).css("zIndex", zindex); }
});
$(div).resizable({ minHeight: 100, minWidth: 100 }, {
stop: function (a, b) {
can.style.height = b.size.height + "px";
can.style.width = b.size.width + "px";
}
});
var canctx = can.getContext('2d');
canctx.strokeStyle = "#2c45a0";
canctx.lineWidth = 2;
canctx.beginPath();
for (var i = 0; i < main.points.length; i++) {
if (i == 0) {
canctx.moveTo(main.points[i].x - leftTopPoint.x,
main.points[i].y - leftTopPoint.y);
}
else {
canctx.lineTo(main.points[i].x - leftTopPoint.x,
main.points[i].y - leftTopPoint.y);
canctx.stroke();
}
}
ctx.strokeRect(leftTopPoint.x, leftTopPoint.y, rightBottomPoint.x - leftTopPoint.x, rightBottomPoint.y - leftTopPoint.y);
ctx.stroke();
ctx.clearRect(0, 0, canvas.width, canvas.height);
})
})
</script>
</head>
<body style=" margin:0; padding:0;">
<div>
<canvas height="500" width="800" style="border: 1px solid red" id="canvas"></canvas>
</div>
</body>
</html>