用JS怎么写个画板功能?
Admin 2021-05-17 群英技术资讯 1484 次浏览
用JS怎么写个画板功能?对于画板功能,我们比较常见是电子教室里的电子黑板,能够经常画画和签字操作,那么我们想要用JS来实现要怎么做呢?
本文特点:
原生JS
封装好的模块
最简代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
let c = document.getElementById('canvas');
c.width = window.innerWidth;
c.height = window.innerHeight;
let ctx = c.getContext('2d');
// draw one black board
ctx.fillStyle = "black";
ctx.fillRect(0,0,600,300);
// 按下标记
let onoff = false,
oldx = -10,
oldy = -10;
// 设置颜色
let linecolor = "white";
// 设置线宽
let linw = 4;
// 添加鼠标事件
// 按下
c.addEventListener('mousedown', event => {
onoff = true;
// 位置 - 10是为了矫正位置,把绘图放在鼠标指针的顶端
oldx = event.pageX - 10;
oldy = event.pageY - 10;
},false);
// 移动
c.addEventListener('mousemove', event => {
if(onoff == true){
let newx = event.pageX - 10,
newy = event.pageY - 10;
// 绘图
ctx.beginPath();
ctx.moveTo(oldx,oldy);
ctx.lineTo(newx,newy);
ctx.strokeStyle = linecolor;
ctx.lineWidth = linw;
ctx.lineCap = "round";
ctx.stroke();
// 每次移动都要更新坐标位置
oldx = newx,
oldy = newy;
}
}, true);
// 弹起
c.addEventListener('mouseup', ()=> {
onoff = false;
},false);
</script>
</body>
</html>
代码讲解
思路
1、鼠标按下,开始描画。鼠标按下事件。
2、鼠标弹起,结束描画。鼠标弹起事件。
3、鼠标按下移动,路径画线。鼠标移动事件。
代码讲解
整体思路:按下鼠标,触发移动的开关,移动后开始记录线条(用移动后的坐标-移动前的坐标,然后绘线),每次移动都会更新旧坐标。松开鼠标后,释放移动开关。
1、只有在鼠标按下,才会触发移动绘图的效果,所以需要增加一个状态判断。
2、因为鼠标指针和实际位置有一个偏移量,所以在坐标定位的时候,需要增加pagex-10从而使坐标位于指针的尖端处。
3、每次移动都要更新坐标位置,用小段的线段来模拟不规则的线。
封装模块
<canvas id="canvas"></canvas>
<script>
class Board{
constructor(canvasName = 'canvas', data = new Map([
["onoff", false],
["oldx", -10],
["oldy", -10],
["fillStyle", "black"],
["lineColor", "white"],
["lineWidth", 4],
["lineCap", "round"],
["canvasWidth", window.innerWidth],
["canvasHeight", window.innerHeight]
])){
// this.data = data;
this.c = document.getElementById(canvasName);
this.ctx = this.c.getContext('2d');
this.onoff = data.get("onoff");
this.oldx = data.get("oldx");
this.oldy = data.get("oldy");
this.lineColor = data.get("lineColor");
this.lineWidth = data.get("lineWidth");
this.lineCap = data.get("lineCap");
this.c.width = data.get("canvasWidth");
this.c.height = data.get("canvasHeight");
this.ctx.fillStyle = data.get("fillStyle");
this.ctx.fillRect(0,0,600,300);
}
eventOperation(){
// 添加鼠标事件
// 按下
this.c.addEventListener('mousedown', event => {
this.onoff = true;
// 位置 - 10是为了矫正位置,把绘图放在鼠标指针的顶端
this.oldx = event.pageX - 10;
this.oldy = event.pageY - 10;
},false);
// 移动
this.c.addEventListener('mousemove', event => {
if(this.onoff == true){
let newx = event.pageX - 10,
newy = event.pageY - 10;
// 绘图
this.ctx.beginPath();
this.ctx.moveTo(this.oldx,this.oldy);
this.ctx.lineTo(newx,newy);
this.ctx.strokeStyle = this.lineColor;
this.ctx.lineWidth = this.lineWidth;
this.ctx.lineCap = this.lineCap;
this.ctx.stroke();
// 每次移动都要更新坐标位置
this.oldx = newx,
this.oldy = newy;
}
}, true);
// 弹起
this.c.addEventListener('mouseup', ()=> {
this.onoff = false;
},false);
}
}
let board = new Board();
board.eventOperation();
</script>
以上就是使用JS实现画板功能的介绍啦,上文有具体的实现思路以及代码,具有一定的借鉴价值,需要的朋友可以参考,希望对大家学习和工作有帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
高阶函数就是函数式编程的一种实现方案,通常把接受一个或多个函数作为参数,或者返回一个函数的函数叫做高阶函数。本文主要介绍vue常用高阶函数以及案例,感兴趣的朋友继续往下看吧。
这篇文章我们来了解jQuery中元素增加属性值的方法,给元素增加属性值是JavaScript学习中比较基础的内容,在jQuery中可以利用attr()方法来实现,下面我们来详细的了解看看怎样做,有需要的朋友可以参考。
这篇文章主要介绍了angular手写树形二级表格的完整代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
首先了解一下 Map再来了解一下 Set总结Map和Set的区别结语:首先了解一下 MapMap 是一组键值对的结构,和 JSON 对象类似。(1) Map数据结构如下这里我们可以看到的是Map的数
今天给大家分享一道JavaScript的面试题,这道问题可是难道不少人呢。JS中我们经常会使用foreach这个方法来遍历数组,那么forEach能否跳出循环?如果能,forEach如何跳出循环?下面我们一起来探讨一下。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008