首页 > 其他 > 详细

(五)实现画笔和橡皮的功能

时间:2018-08-02 14:18:07      阅读:146      评论:0      收藏:0      [点我收藏+]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>鼠标画线和方块移动</title>
</head>
<style>
* {margin: 0;padding: 0;}
body {background-color: #000;}
#c1 {background-color: #fff}
.active {background-color: red;color: #fff}
</style>
<body>
<canvas id="c1" width="400" height="400"></canvas>
<input class="active" type="button" value="画笔">
<input type="button" value="橡皮擦">
<script>
var oC = document.getElementById(c1);
var aInput = document.getElementsByTagName("input");
var num = 0;
for(var i=0;i<aInput.length;i++) {
aInput[i].index = i;
aInput[i].onclick = function () {
for(var j= 0;j<aInput.length;j++) {
aInput[j].className = "";
}
this.className = "active";
num = this.index;
}
}
var ctx = oC.getContext("2d");
oC.onmousedown = function(ev) {
var x = ev.pageX - oC.offsetLeft;
var y = ev.pageY - oC.offsetTop;
ctx.beginPath();
ctx.moveTo(x,y);
oC.onmousemove = function(ev) {
var x = ev.pageX - oC.offsetLeft;
var y = ev.pageY - oC.offsetTop;
if(num ==0) {
ctx.lineTo(x,y);
ctx.stroke();
} else if(num ==1) {
ctx.clearRect(x,y,20,20);
}
};
oC.onmouseup = function (ev) {
oC.onmousemove = null;
oC.onmouseup = null;
ctx.closePath();
};
return false;
}
</script>
</body>
</html>

 

(五)实现画笔和橡皮的功能

原文:https://www.cnblogs.com/bgwhite/p/9406809.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!