Because the canvas api does not have dotted lines
So you need to implement it yourself
Wouldn't it be nice to review trigonometric functions by the way?
var context=document.getElementById(canvas).getContext(2d);function drawDashedLine(context,x1,y1,x2,y2,dashlength){ dashlength=dashlength===undefined?5:dashlength; var deltaX=x2-x1; //The length of a right-angled side var deltay=y2-y1; //The length of the other instruction side var numDashes=Math.floor( Math.sqrt(deltaX*deltaX+deltay*deltay)/dashlength //Math.sqrt returns the square root of a number dashlength and the length of each point of the dashed line) var everydashLength_x=deltaX/numDashes //Determine every point on the X axis The starting point of the dotted line points var everydashLength_y=deltay/numDashes //Determine the starting point of each dashed line point on the Y axis for(var i=0;i<numDashes;i++){ context[i%2===0?'moveTo':lineTo] (x1+everydashLength_x*i,y1 +everydashLength_y*i) } context.stroke()}context.lineWidth=3context.strokeStyle=bluedrawDashedLine(context,20,20,context.canvas.width-20,20,20)
The effect is as shown in the figure
The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope everyone will support VeVb Wulin Network.