Javascript利用canvas繪制兩點間曲線和箭頭
更新時間:2023年10月24日 11:08:31 作者:相信神話2021
這篇文章主要為大家詳細介紹了Javascript如何利用canvas實現(xiàn)在兩點間繪制曲線和矩形,并且在矩形中繪制文字,感興趣的小伙伴可以跟隨小編一起學習一下
Javascript利用canvas,在兩點間繪制曲線,矩形,并且在矩形中繪制文字,實現(xiàn)居中對齊,同時實現(xiàn)了箭頭繪制,效果如圖:
一、html代碼
代碼如下
<canvas id="canvas" width="1000" height="600"></canvas>
二、css代碼
canvas { display: block; width: 1000px; height: 600px; background: conic-gradient(#eee 25%, white 0deg 50%, #eee 0deg 75%, white 0deg) 0 / 20px 20px; margin-inline: auto; } @media (max-width: 640px) { canvas { width: 100vw; height: 60vw; } }
三、Javascript代碼
代碼如下
var context = document.getElementById("canvas").getContext("2d"); ; // 繪制尺寸 var width = canvas.width; var height = canvas.height; // 兩個方塊的坐標、尺寸,顏色等數(shù)據(jù) var data = [{ x: 800, y: 180, width: 200, height: 50, color: 'deepskyblue', text:"+u+ate" }, { x: 600, y: 680, width: 200, height: 50, color: 'deeppink', text:"gress 邁步" }]; // 繪制矩形方法 var drawRect = function () { data.forEach(function (obj) { context.beginPath(); context.fillStyle = obj.color; context.fillRect(obj.x, obj.y, obj.width, obj.height); context.closePath(); context.font="30px Verdana"; context.fillStyle="#fff"; console.log(context.measureText(obj.text)) const textMetrics = context.measureText(obj.text); // 所有字在這個字體下的高度 let fontHeight = textMetrics.fontBoundingBoxAscent + textMetrics.fontBoundingBoxDescent; console.log(fontHeight) context.fillText(obj.text,0,0) context.fillText(obj.text,obj.x+(obj.width-context.measureText(obj.text).width)/2,obj.y+fontHeight); }); }; // 繪制連接曲線方法 var drawCurve = function () { // 按照坐標位置排序 var dataSort = data.sort(function (objA, objB) { return (objA.y + objA.height) - (objB.y + objB.height); }); // 知道上下數(shù)據(jù) var from = dataSort[0]; var to = dataSort[1]; // 曲線的起點終點 var fromX = from.x + from.width / 2; var fromY = from.y + from.height; var toX = to.x + to.width / 2; var toY = to.y; // 曲線控制點坐標 var cp1x = fromX; var cp1y = fromY + (toY - fromY) / 2; var cp2x = toX; var cp2y = toY - (toY - fromY) / 2; // 開始繪制曲線 context.beginPath(); context.lineWidth = 1 context.strokeStyle = '#000'; context.moveTo(fromX, fromY); // 繪制曲線點 context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX, toY); context.stroke(); }; // 繪制方法 var draw = function () { context.clearRect(0, 0, width, height); drawRect(); drawCurve(); }; draw(); canvas_arrow(ctx, 10, 30, 200, 150); canvas_arrow(ctx, 100, 200, 400, 50); canvas_arrow(ctx, 200, 30, 10, 150); canvas_arrow(ctx, 400, 200, 100, 50); ctx.stroke(); function canvas_arrow(context, fromx, fromy, tox, toy) { var headlen = 10; // length of head in pixels var dx = tox - fromx; var dy = toy - fromy; var angle = Math.atan2(dy, dx); context.moveTo(fromx, fromy); context.lineTo(tox, toy); context.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6)); context.moveTo(tox, toy); context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6)); }
ok,代碼就是如此
完整代碼
<!DOCTYPE HTML> <html> <head> <style> canvas { display: block; width: 1000px; height: 600px; background: conic-gradient(#eee 25%, white 0deg 50%, #eee 0deg 75%, white 0deg) 0 / 20px 20px; margin-inline: auto; } @media (max-width: 640px) { canvas { width: 100vw; height: 60vw; } } </style> </head> <body> <canvas id="canvas" width="2000" height="1200"></canvas> <script> var context = document.getElementById("canvas").getContext("2d"); ; // 繪制尺寸 var width = canvas.width; var height = canvas.height; // 兩個方塊的坐標、尺寸,顏色等數(shù)據(jù) var data = [{ x: 800, y: 180, width: 200, height: 50, color: 'deepskyblue', text:"+u+ate" }, { x: 600, y: 680, width: 200, height: 50, color: 'deeppink', text:"gress 邁步" }]; // 繪制矩形方法 var drawRect = function () { data.forEach(function (obj) { context.beginPath(); context.fillStyle = obj.color; context.fillRect(obj.x, obj.y, obj.width, obj.height); context.closePath(); context.font="30px Verdana"; context.fillStyle="#fff"; console.log(context.measureText(obj.text)) const textMetrics = context.measureText(obj.text); // 所有字在這個字體下的高度 let fontHeight = textMetrics.fontBoundingBoxAscent + textMetrics.fontBoundingBoxDescent; console.log(fontHeight) context.fillText(obj.text,0,0) context.fillText(obj.text,obj.x+(obj.width-context.measureText(obj.text).width)/2,obj.y+fontHeight); }); }; // 繪制連接曲線方法 var drawCurve = function () { // 按照坐標位置排序 var dataSort = data.sort(function (objA, objB) { return (objA.y + objA.height) - (objB.y + objB.height); }); // 知道上下數(shù)據(jù) var from = dataSort[0]; var to = dataSort[1]; // 曲線的起點終點 var fromX = from.x + from.width / 2; var fromY = from.y + from.height; var toX = to.x + to.width / 2; var toY = to.y; // 曲線控制點坐標 var cp1x = fromX; var cp1y = fromY + (toY - fromY) / 2; var cp2x = toX; var cp2y = toY - (toY - fromY) / 2; // 開始繪制曲線 context.beginPath(); context.lineWidth = 1 context.strokeStyle = '#000'; context.moveTo(fromX, fromY); // 繪制曲線點 context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX, toY); context.stroke(); }; // 繪制方法 var draw = function () { context.clearRect(0, 0, width, height); drawRect(); drawCurve(); }; draw(); canvas_arrow(ctx, 10, 30, 200, 150); canvas_arrow(ctx, 100, 200, 400, 50); canvas_arrow(ctx, 200, 30, 10, 150); canvas_arrow(ctx, 400, 200, 100, 50); ctx.stroke(); function canvas_arrow(context, fromx, fromy, tox, toy) { var headlen = 10; // length of head in pixels var dx = tox - fromx; var dy = toy - fromy; var angle = Math.atan2(dy, dx); context.moveTo(fromx, fromy); context.lineTo(tox, toy); context.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6)); context.moveTo(tox, toy); context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6)); } </script> </body> </html>
到此這篇關于Javascript利用canvas繪制兩點間曲線和箭頭的文章就介紹到這了,更多相關Javascript canvas內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript獲取地址欄參數(shù)的方法實現(xiàn)
這篇文章主要給大家介紹了關于JavaScript獲取地址欄參數(shù)的方法實現(xiàn),項目中經常遇到獲取上個頁面跳轉過來獲取當前的參數(shù),文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07小程序webview內網頁實現(xiàn)微信支付的代碼示例
這篇文章主要介紹了在小程序中使用webview實現(xiàn)微信支付功能,需要在小程序和webview之間進行交互,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-02-02