canvas简单的实现画笔功能

还是直接上代码。(自己写的,可能代码布局有点乱,进步中,加油)

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			* {
				padding: 0;
				margin: 0;
			}
			
			body {
				width: 100%;
				height: 100%;
				background: black;
			}
			
			#can {
				background: #fff;
			}
			
			div {
				width: 800px;
				height: 800px;
				margin: auto;
			}
			
			input {
				width: 70px;
				height: 50px;
				font-size: 20px;
			}
		</style>
		<script type="text/javascript">
			window.onload = function() {
				var mycolor = 'black';
				//b表示画布
				var b = 1;
				//a表示橡皮
				var a = 50;
				var oC = document.getElementById("can")
				var oGC = oC.getContext("2d")
				oC.onmousedown = function(e) {
					//计算鼠标在画布的距离
					var disX = e.clientX - oC.offsetLeft
					var disY = e.clientY - oC.offsetTop
					//每次必须重新开始,让它们变成多个。
					oGC.beginPath();
					if(mycolor == 'white') {
						//设置画线的宽,与颜色
						oGC.lineWidth = a;
						oGC.strokeStyle = mycolor;
						//设置画的起始点
						oGC.moveTo(disX, disY)
					} else {
						//设置画线的宽,与颜色
						oGC.lineWidth = b;
						oGC.strokeStyle = mycolor;
						//设置画的起始点
						oGC.moveTo(disX, disY)
					}
					document.onmousemove = function(e) {
						var disX = e.clientX - oC.offsetLeft
						var disY = e.clientY - oC.offsetTop
						//移动时设置画线的结束位置。并且显示
						oGC.lineTo(disX, disY) //鼠标点下去的位置
						oGC.stroke()
					}
					//鼠标离开时清空鼠标按下与移动事件,停止画
					document.onmouseup = function() {
						document.onmousedown = null //鼠标放开的位置
						document.onmousemove = null
					}

				}
				//加减粗细
				var ainp = document.getElementsByTagName('input')
				//画笔的加减
				ainp[0].onclick = function() {
					b++;
				}
				ainp[1].onclick = function() {
					if(b > 1) {
						b--;
					}
				}
				//橡皮的加减
				ainp[7].onclick = function() {
					a += 5;
				}
				ainp[8].onclick = function() {
					if(a > 5) {
						a -= 5;
					}
				}
				//改变颜色
				for(var i = 2; i < ainp.length - 2; i++) {
					ainp[i].onclick = function() {
						mycolor = this.value;
					}
				}
				//rgb颜色
				ainp[9].onchange=function(){
					mycolor=this.value;
				}
			}
		</script>
	</head>

	<body>
		<div>
			<canvas id="can" width="800" height="800"></canvas>
			<input type="button" value="+" />
			<input type="button" value="-" />
			<input type="button" value="red" style="background: red;" />
			<input type="button" value="yellow" style="background: yellow;" />
			<input type="button" value="blue" style="background: blue;" />
			<input type="button" value="black" style="background: black; color: white;" />
			<input type="button" value="white" style="background:;" />
			<input type="button" value="橡皮+" />
			<input type="button" value="橡皮-" />
			<!--添加一个rgb色-->
			<input type="color"/>
		</div>
	</body>
</html>

效果如图
在这里插入图片描述

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值