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
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Line Basic</title> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta charset="utf-8"> <style> html, body { height: 100%; } body { margin: 0; padding: 0; background-color: rgb(42, 42, 42); } </style> <body> <canvas id="canvas-1" width="1024" height="768" style="background-color: antiquewhite;margin:30px;"></canvas> </body> <script> let canvas = document.getElementById('canvas-1'); let context = canvas.getContext('2d'); let width = canvas.width; let height = canvas.height;
let origin = [width/2, height/2];
let x1 = origin[0]+100; let y1 = origin[1]+40;
let x2 = origin[0] + 140; let y2 = origin[1] + 120;
function calcRadian(x1,y1,x2,y2){ let radian = Math.atan2(y2-y1,x2-x1); return radian; }
function calcPoints(x,y,len,lineRadian,arrowRadian){ let x1=len*Math.cos(lineRadian+arrowRadian); let y1=len*Math.sin(lineRadian+arrowRadian); let x2=len*Math.cos(lineRadian-arrowRadian); let y2=len*Math.sin(lineRadian-arrowRadian); return [[x1+x,y1+y],[x2+x,y2+y]]; }
let lineRadian = calcRadian(x2,y2,x1,y1); let rowRadian = Math.PI/4; let len=20; let points = calcPoints(x2, y2, len, lineRadian, rowRadian);
context.moveTo(x1, y1); context.lineTo(x2, y2); context.stroke(); context.lineTo(points[0][0], points[0][1]); context.stroke(); context.lineTo(points[1][0], points[1][1]); context.stroke(); context.lineTo(x2, y2); context.stroke(); context.fill();
let startLineRadian = calcRadian(x2,y2,x1,y1); let startPoints = calcPoints(x1, y1, len, startLineRadian-Math.PI, rowRadian); context.lineTo(x1, y1); context.lineTo(startPoints[0][0], startPoints[0][1]); context.lineTo(startPoints[1][0], startPoints[1][1]); context.lineTo(x1, y1); context.stroke(); context.fill(); </script> </html>
|