CANVAS绘制线段

本文将介绍如何利用HTML5的Canvas API来绘制虚线,包括设置线型样式和进行虚线绘制的方法。
摘要由CSDN通过智能技术生成
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
<!DOCTYPE html>
< html >
     < head >
         < title >rect
         </ title >
         < style >
           body{
             background: #eeeeee;
           }
 
             #controls{
                 position: absolute;
                 left: 25px;
                 top: 25px;
             }
 
             #canvas{
                 background: #ffffff;
                 cursor: pointer;
                 margin-left:10px;
                 margin-top: 10px;
                 -webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
                 -moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
                 -box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
             }
         </ style >
     </ head >
     < body >
 
         < canvas  id = "canvas"  width = "600"  height = "400" >
             Canvas not supported
         </ canvas >
 
         < div  id = "controls" >
             Stroke color: < select  id = "strokeStyleSelect" >
             < option  value = "red" >red</ option >
             < option  value = "green" >green</ option >
             < option  value = "blue" >blue</ option >
             < option  value = "orange" >orange</ option >
     < option  value = "cornflowerblue"  selected>cornflowerblue</ option >
             < option  value = "goldenrod" >goldenrod</ option >
             < option  value = "navy" >navy</ option >
             < option  value = "purple" >purple</ option >
             </ selectid >
 
             Guidewires:
             < input  id = "guidewireCheckbox"  type = "checkbox"  checked/>
     < input  id = "eraseAllButton"  type  "button"  value = "Erase all" />
         </ div >
 
         < script  src = "chapetal2.js" ></ script >
     </ body >
</ html >


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var  canvas = document.getElementById( "canvas" ),
     context = canvas.getContext( "2d" ),
     eraseAllButton = document.getElementById( "eraseAllButton" ),
     strokeStyleSelect = document.getElementById( "strokeStyleSelect" ),
     guidewireCheckbox = document.getElementById( "guidewireCheckbox" ),
     drawingSurfaceImageData,
     mousedown = {},
     rubberbandRect = {},
     dragging =  false ,
     guidewires = guidewireCheckbox.checked;
 
function  drawGrid(color, step, stepy){
     //page 96
}
 
function  windowToCanvas(x, y){
     var  bbox = canvas.getBoundingClientRect();
     return  { x: x - bbox.left * (canvas.width / bbox.width),
              y: y - bbox.top * (canvas.height / bbox.height) };
}
 
//Save and restore drawing surface...............................
function  saveDrawingSurface(){
     drawingSurfaceImageData = context.getImageData(0, 0,
                                                     canvas.width,
                                                     canvas.height);
}
 
function  restoreDrawingSurface(){
     context.putImageData(drawingSurfaceImageData,0,0);
}
 
//Rubber bands..................................................
 
function  updateRubberbandRectangle(loc){
     rubberbandRect.width = Math.abs(loc.x - mousedown.x);
     rubberbandRect.height = Math.abs(loc.y - mousedown.y);
 
     if (loc.x > mousedown.x)  rubberbandRect.left = mousedown.x;
     else                      rubberbandRect.left = loc.x;
 
     if (loc.y > mousedown.y) rubberbandRect.top = mousedown.y;
     else                     rubberbandRect.top = loc.y;
}
 
function  drawRubberbandShape(loc){
     context.beginPath();
     context.moveTo(mousedown.x, mousedown.y);
     context.lineTo(loc.x, loc.y);
     context.stroke();
}
 
function  updateRubberband(loc){
     updateRubberbandRectangle(loc);
     drawRubberbandShape(loc);
}
 
//Guidewires.......................................................
 
function  drawHorizontalLine(y){
     context.beginPath();
     context.moveTo(0, y + 0.5);
     context.lineTo(context.canvas.width, y + 0.5);
     context.stroke();
}
 
function  drawVerticalLine(x){
     context.beginPath();
     context.moveTo(x+0.5, 0);
     context.lineTo(x+0.5, context.canvas.height);
     context.stroke();
}
 
function  drawGuidewires(x, y){
     context.save();
     context.strokeStyle =  'rgba(0, 0, 230, 0.4)' ;
     context.lineWidth = 0.5;
     drawVerticalLine(x);
     drawHorizontalLine(y);
     context.restore();
}
 
//Canvas event handlers.....................................................
 
canvas.onmousedown =  function (e){
     var  loc = windowToCanvas(e.clientX, e.clientY);
     e.preventDefault();  //Prevent cursor change
     saveDrawingSurface();
     mousedown.x = loc.x;
     mousedown.y = loc.y;
     dragging =  true ;
};
 
canvas.onmousemove =  function (e){
     var  loc;
     if (dragging){
         e.preventDefault(); //Prevent selections
 
         loc = windowToCanvas(e.clientX, e.clientY);
         restoreDrawingSurface();
         updateRubberband(loc);
 
         if (guidewires){
             drawGuidewires(loc.x, loc.y);
         }
 
     }
};
 
canvas.onmouseup =  function (e){
     loc = windowToCanvas(e.clientX, e.clientY);
     restoreDrawingSurface();
     updateRubberband(loc);
     dragging =  false ;
};
 
//Controls event handlers...................................................
 
eraseAllButton.onclick =  function (e){
     context.clearRect(0, 0, canvas.width, canvas.height);
     drawGrid( 'lightgray' , 10, 10);
     saveDrawingSurface();
};
 
strokeStyleSelect.onchange =  function (e){
     context.strokeStyle = strokeStyleSelect.value;
};
 
guidewireCheckbox.onchange =  function (e){
     guidewires = guidewireCheckbox.checked;
};
 
//Initialization............................................................
context.strokeStyle = strokeStyleSelect.value;
drawGrid( 'lightgray' , 10, 10);


DRAW DASHEDLINE:

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
<!DOCTYPE html>
< html >
     < head >
         < title >rect
         </ title >
         < style >
           body{
             background: #eeeeee;
           }
 
             #canvas{
                 background: #ffffff;
 
             }
         </ style >
     </ head >
     < body >
 
         < canvas  id = "canvas"  width = "600"  height = "400" >
             Canvas not supported
         </ canvas >
 
         < script  src = "chapetal2.js" ></ script >
     </ body >
</ html >


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
var  canvas = document.getElementById( "canvas" ),
     context = canvas.getContext( "2d" ),
     moveToFunction = CanvasRenderingContext2D.prototype.moveTo;
CanvasRenderingContext2D.prototype.lastMoveToLocation = {};
 
CanvasRenderingContext2D.prototype.moveTo =  function (x, y){
     moveToFunction.apply(context, [x, y]);
     this .lastMoveToLocation.x = x;
     this .lastMoveToLocation.y = y;
};
 
CanvasRenderingContext2D.prototype.dashedLineTo =
     function (x, y, dashLength){
     dashLength = dashLength === undefined ? 5 : dashLength;
 
     var  startX =  this .lastMoveToLocation.x;
     var  startY =  this .lastMoveToLocation.y;
 
     var  deltaX = x - startX;
     var  deltaY = y - startY;
     var  numDashes = Math.floor(Math.sqrt(deltaX * deltaX
                                     + deltaY * deltaY) / dashLength);
 
     for ( var  i = 0 ; i < numDashes; ++i){
         this [ i % 2 === 0 ?  'moveTo'  'lineTo' ]
             (startX + (deltaX / numDashes) * i,
                 startY + (deltaY / numDashes) * i);
     }
     this .moveTo(x, y);
};
 
 
function  drawDashedLine(context, x1, y1, x2, y2, dashLength){
     dashLength = dashLength === undefined ? 5 : dashLength;
 
     var  deltaX = x2 - x1;
     var  deltaY = y2 - y1;
     var  numDashes = Math.floor(
         Math.sqrt(deltaX * deltaX + deltaY * deltaY) / dashLength);
 
     for ( var  i = 0 ; i < numDashes; ++i){
         context[i% 2 === 0 ?  'moveTo'  'lineTo' ]
             (x1 + (deltaX / numDashes) * i, y1 + (deltaY / numDashes) * i);
     }
 
     context.stroke();
};
 
//context.lineWidth = 3;
//context.strokeStyle = 'blue';
//drawDashedLine(context, 20, 20, context.canvas.width - 20, 20);
//drawDashedLine(context, context.canvas.width-20, 20,
//        context.canvas.width - 20, context.canvas.height - 20, 10);
//drawDashedLine(context, context.canvas.width -20,
//        context.canvas.height - 20, 20, context.canvas.height-20, 15);
//drawDashedLine(context, 20, context.canvas.height-20, 20, 20, 2);
 
context.lineWidth = 3;
context.strokeStyle =  'blue' ;
context.moveTo(20, 20);
context.dashedLineTo(context.canvas.width-20,20);
context.dashedLineTo(context.canvas.width-20,
                      context.canvas.height-20);
context.dashedLineTo(20, context.canvas.height-20);
context.dashedLineTo(20, 20);
context.dashedLineTo(context.canvas.width - 20,
                      context.canvas.height - 20);
context.dashedLineTo(context.canvas.width/2+20,
                      context.canvas.height/2-20);
context.stroke();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值