一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - js教程 - JS canvas實現畫板和簽字板功能

JS canvas實現畫板和簽字板功能

2022-01-22 20:04莫兮是我 js教程

這篇文章主要為大家詳細介紹了JS canvas實現畫板和簽字板功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JS canvas實現畫板/簽字板功能的具體代碼,供大家參考,具體內容如下

前言

常見的電子教室里的電子黑板。

本文特點:

原生JS
封裝好的模塊

最簡代碼樣例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!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>

結果展示

JS canvas實現畫板和簽字板功能

代碼講解

思路

1、鼠標按下,開始描畫。鼠標按下事件。
2、鼠標彈起,結束描畫。鼠標彈起事件。
3、鼠標按下移動,路徑畫線。鼠標移動事件。

代碼講解

整體思路:按下鼠標,觸發移動的開關,移動后開始記錄線條(用移動后的坐標-移動前的坐標,然后繪線),每次移動都會更新舊坐標。松開鼠標后,釋放移動開關。

1、只有在鼠標按下,才會觸發移動繪圖的效果,所以需要增加一個狀態判斷。
2、因為鼠標指針和實際位置有一個偏移量,所以在坐標定位的時候,需要增加pagex-10從而使坐標位于指針的尖端處。
3、每次移動都要更新坐標位置,用小段的線段來模擬不規則的線。

封裝模塊

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u013362192/article/details/113934773

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色综合久久综合网欧美综合网 | 日韩制服丝袜在线观看 | 日本乱人伦中文在线播放 | 九九精品视频在线观看 | 99自拍视频在线观看 | 国产亚洲精品精品国产亚洲综合 | 亚洲国产精品综合久久一线 | 亚洲青草视频 | 婷婷综合在线 | 出a级黑粗大硬长爽猛视频 吃胸膜奶视频456 | 国内在线播放 | 天天做天天爱天天爽综合区 | jzzjzz视频免费播放 | 99资源站 | 999精品视频在线观看 | 亚洲精品卡一卡2卡3卡4卡 | 深夜免费在线视频 | 欧洲vodafone精品性 | 国产v日韩v欧美v精品专区 | 国色天香社区在线视频播放 | 亚洲精品九色在线网站 | 日韩欧美推理片免费看完整版 | 精品久久99麻豆蜜桃666 | 日本一道高清不卡免费 | 91精品国产91久久久久久麻豆 | 色播艾小青国产专区在线播放 | 动漫jk美女被爆羞羞漫画 | 久久久无码精品亚洲A片猫咪 | 色综合视频一区二区三区 | 欧美精品日韩一区二区三区 | 乌克兰粉嫩摘花第一次 | 成人网欧美亚洲影视图片 | 美女班主任让我爽了一夜视频 | 麻豆网站在线看 | 白丝h视频| 91精品国产91久久久久久麻豆 | babes性欧美30 | 无码人妻精品一区二区蜜桃在线看 | 欧美久在线观看在线观看 | 天堂在线国产 | 无人在线高清观看 |