<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>繪製矩形</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="400" style="border:1px solid red;">
<p>您的系統不支援此程式!</p>
</canvas>
<div id="allStyleBox">
<b>選形狀:</b>
<select id="pickShape" tag="shape">
<option value="rect">畫矩形</option>
<option value="arc">畫圓</option>
<option value="line">畫線</option>
<option value="clear">橡皮擦</option>
</select>
<b>選顏色</b>
<select id="pickColor" tag="color">
<option value="red">紅色</option>
<option value="black">黑色</option>
</select>
<b>填充 還是 描邊</b>
<select id="pickStroke" tag="strokeFill">
<option value="fill">填充</option>
<option value="stroke">描邊</option>
</select>
<b>線寬</b>
<select id="pickLine" tag='line'>
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
</select>
<b>橡皮擦寬度</b>
<select id="pickClear" tag='clearWidth'>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
</div>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var drawShapeObj = {
shape: pickShape.value,
color: pickColor.value,
line: pickLine.value,
strokeFill: pickStroke.value,
clearWidth: pickClear.value
};
var imageData = '';
function dragImg(fn) {
var moving = false;
var dis = {};
canvas.onmousedown = function (e) {
moving = true;
dis = {
x: e.clientX,
y: e.clientY
}
}
canvas.onmousemove = function (e) {
if (!moving) {
return;
}
fn && fn.call(e, dis);
}
canvas.onmouseup = function (e) {
moving = false;
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imageData);
}
}
var shapeFun = {
common: function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.lineWidth = drawShapeObj.line;
ctx.strokeStyle = drawShapeObj.color;
ctx.fillStyle = drawShapeObj.color;
shapeFun.reDrawAll();
},
reDrawAll: function () {
imageData && ctx.putImageData(imageData, 0, 0);
},
rect: function (dis) {
shapeFun.common();
ctx.rect(dis.x, dis.y, Math.abs(this.clientX - dis.x), Math.abs(this.clientY - dis.y));
ctx[drawShapeObj.strokeFill]();
ctx.closePath();
},
arc: function (dis) {
shapeFun.common();
var radius = Math.abs(this.clientX - dis.x);
ctx.moveTo(dis.x + radius, dis.y);
ctx.arc(dis.x, dis.y, radius, 0, 2 * Math.PI, true);
ctx[drawShapeObj.strokeFill]();
ctx.closePath();
},
line: function (dis) {
shapeFun.common();
ctx.moveTo(dis.x, dis.y);
ctx.lineTo(this.clientX, this.clientY);
ctx.stroke();
},
clear: function (dis) {
console.log('clear');
ctx.clearRect(this.clientX, this.clientY, drawShapeObj.clearWidth, drawShapeObj.clearWidth);
}
}
dragImg(shapeFun[drawShapeObj.shape]);
allStyleBox.addEventListener('change', function (ev) {
var target = ev.target,
tag = target.getAttribute('tag');
drawShapeObj[tag] = target.value;
dragImg(shapeFun[drawShapeObj.shape]);
})
</script>
</body>
</html>