canvas画线

参考地址 https://segmentfault.com/q/1010000018554255

自写案例

画线 移入线条路径内高亮显示

html

1
<canvas class="canvasbox" id="canvas"></canvas>

js

1
2
3
4
5
6
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
canvas.style.width = 1455 + 'px';
canvas.style.height = 300 + 'px';
canvas.width = 1455;
canvas.height = 300;

模糊问题 把长宽高 写到 上面 不要写css 里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ctx.beginPath()						//开始画线
// ctx.strokeStyle = 'green' //颜色
// ctx.lineWidth = 1 //宽度
// ctx.moveTo(168, 0) //起点坐标
// ctx.lineTo(615, 100) //终点坐标
//ctx.setLineDash(1,1); //虚线 步长 (线段长 , 间距
// ctx.stroke() //结束

// ctx.beginPath()
// ctx.strokeStyle = 'green'
// ctx.lineWidth = 1
// ctx.moveTo(168, 0)
// ctx.lineTo(840, 100)
// ctx.stroke()

// ctx.beginPath()
// ctx.strokeStyle = 'green'
// ctx.lineWidth = 1
// ctx.moveTo(168, 0)
// ctx.lineTo(1065, 100)
// ctx.stroke()

坐标获取可用

1
2
3
4
5
6
7
<canvas class="canvasbox" id="canvas" @mousemove="mousemoveFN"></canvas>
mousemoveFN(event) {
// console.log('event', event);
// console.log('x', event.offsetX)
// console.log('y', event.offsetY)
},

选中高亮核心属性

ctx.isPointInStroke(x,y)
// 测试后发现这个方法不适合,这个适用于图形范围
// if (ctx.isPointInPath(event.offsetX, event.offsetY)) {
// console.log(‘在区域上。。。。。。。。。。。’);
// }
// 测试后发现这个方法可以(主要用于边缘线
if (ctx.isPointInStroke(event.offsetX, event.offsetY)) {
console.log(‘在綫上。。。。。。。。。。。’);
ctx.stroke();
};
isPointInStroke 只能监听到最后一次画线的路径 所以要循环渲染

canvas 添加监听属性

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
canvas.addEventListener('mousemove', (event) => {
this.clickFN()

console.log(event.offsetX, event.offsetY);

for (var k in this.lineData) {
this.lineData[k].forEach((row, index) => {



this.drawWire(ctx, row)
if (ctx.isPointInStroke(event.offsetX, event.offsetY)) {
console.log('在綫上。。。。。。。。。。。', `k:${k}`, `index:${index}`);
ctx.stroke();
console.log(1111111111111,this.lineData[k][index]);
this.lineData[k][index].color = 'red'
this.lineData[k][index].lineWidth = 3
this.lineData[k][index].setLineDash = [1, 0],
this.drawWire(ctx, row)
}else{
this.lineData[k][index].color = 'green'
this.lineData[k][index].lineWidth = 1
this.lineData[k][index].setLineDash = [4, 2],
this.drawWire(ctx, row)
}
});
}

// 测试后发现这个方法不适合,这个适用于图形范围
// if (ctx.isPointInPath(event.offsetX, event.offsetY)) {
// console.log('在区域上。。。。。。。。。。。');
// }
// 测试后发现这个方法可以(主要用于边缘线
if (ctx.isPointInStroke(event.offsetX, event.offsetY)) {
console.log('在綫上。。。。。。。。。。。');
ctx.stroke();
};

})

vanvas 重画重叠问题 清除canvas 画布
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();

//根据传进来的坐标点数组画出一条线
drawWire(ctx, row) {
ctx.beginPath()
ctx.strokeStyle = row.color
ctx.lineWidth = row.lineWidth
ctx.setLineDash(row.setLineDash);
console.log(row.moveTo.join(','));
ctx.moveTo(Number(row.moveTo[0]), Number(row.moveTo[1]))
ctx.lineTo(Number(row.lineTo[0]), Number(row.lineTo[1]))
ctx.stroke()
},

测试数据

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
lineData: {
CPU0: [
{
color: 'green',
lineWidth: 1,
moveTo: [168, 0],
lineTo: [390, 100],
setLineDash: [4, 2],
isMouseOver: false,
},
{
color: 'green',
lineWidth: 1,
moveTo: [168, 0],
lineTo: [615, 100],
setLineDash: [4, 2],
isMouseOver: false,
},
{
color: 'green',
lineWidth: 1,
moveTo: [168, 0],
lineTo: [840, 100],
setLineDash: [4, 2],
isMouseOver: false,
},
{
color: 'green',
lineWidth: 1,
moveTo: [168, 0],
lineTo: [1065, 100],
setLineDash: [4, 2],
isMouseOver: false,
},
],
},

参考文献

参考 https://segmentfault.com/q/1010000018554255

https://blog.csdn.net/weixin_48963720/article/details/125219790

ctx.isPointInPath(80,80):判断点是否在上面路径的区域内。

ctx.isPointInStroke(80,50):判断点是否在上面路径的边缘线上

https://blog.csdn.net/zyz00000000/article/details/82714454


canvas画线
http://www.dwyblog.cn/2024/01/09/canvas画线/
作者
幸福来敲门
发布于
2024年1月9日
许可协议