canvas 画板

Posted suga

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了canvas 画板相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
body {
background: black;
text-align: center;
}
#cans {
background: white;

}
</style>
<script>
window.onload=function(){


let OC=document.getElementById("cans");
let c1=document.getElementById("c1");
let color="black";
c1.onchange=function(){ //改变画笔颜色
color=this.value; //给画笔赋值,this.value即c1改变后的颜色值
}
let ctx=OC.getContext("2d");
let lastX,lastY;
OC.onmousedown=function(ev){ //按下鼠标
lastX=ev.offsetX; //获取鼠标位置x轴坐标
lastY=ev.offsetY; //获取鼠标位置y轴坐标



OC.onmousemove=function(ev){ //在画布里按下鼠标并移动鼠标
ctx.beginPath();
ctx.moveTo(lastX,lastY);
ctx.lineTo(ev.offsetX,ev.offsetY); //设置终点坐标
ctx.strokeStyle=color;
ctx.stroke();
lastX=ev.offsetX; //每次的终点坐标,即下次起笔的起点坐标(鼠标的当前位置)
lastY=ev.offsetY; //每次的终点坐标,即下次起笔的起点坐标(鼠标的当前位置)


}

document.onmouseup=function(ev){ //在整个document内松开鼠标
OC.onmousemove=null; //置空
OC.onmouseup=null; //置空


}


}



}
</script>
</head>
<body>
<input type="color" id="c1"><br />
<canvas id="cans" height="800" width="1200">该浏览器不支持canvas元素操作,请更新浏览器</canvas>
</body>
</html>

以上是关于canvas 画板的主要内容,如果未能解决你的问题,请参考以下文章

canvas画板强制横屏画不了

canvas画板绘图 矩形 圆形 椭圆 自定义多边形 画笔/铅笔 曲线 橡皮擦

canvas小创作 之 实现一个画板(功能:画笔颜色粗细可以任选;橡皮擦功能和清空画板功能)

用canvas画布画一个画板

如何用canvas画板实现一个简单的球在盒子内反弹运动

canvas 画板