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
| <!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> <style> canvas{ border: 1px solid red; display: block; margin: 0 auto; } </style> </head> <body> <canvas width="500px" height="500px" id="canvas"></canvas> <script> let canvas = document.getElementById('canvas'); let c = canvas.getContext('2d'); drawLine(100,100,400,100,'red',10); drawLine(400,100,400,400,'blue',10); drawLine(400,400,100,400,'green',10); drawLine(100,400,100,100,'orange',10); /** * @description: * @param {*} x1 * @param {*} x2 * @param {*} y1 * @param {*} y2 * @param {*} color * @param {*} width * @return {*} */ function drawLine(x1,x2,y1,y2,color,width){ c.beginPath(); c.moveTo(x1,y1); c.lineTo(x2,y2); c.strokeStyle = color; c.lineWidth = width; c.stroke(); c.closePath(); } </script> </body> </html>
|