js控制貝塞爾曲線程式碼例項
本章節分享一段程式碼例項,它實現了控制貝塞爾取消效果。
通過拖動幾個控制點即可實現,程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body { margin: 0; padding: 0; } canvas { background-color: #ccc; } </style> </head> <body> <canvas id="canvas" width="1175" height="600"></canvas> <script> var canvas = document.getElementById("canvas"), context = canvas.getContext("2d"), isDragging = false, radius = 12; /*先設定圓的四個點引數*/ circles = [{ x: 200, y: 250, isSelected: false }, { x: 280, y: 120, isSelected: false }, { x: 380, y: 120, isSelected: false }, { x: 450, y: 250, isSelected: false }]; canvas.onmousedown = canvasClick; canvas.onmouseup = stopDragging; canvas.onmouseout = stopDragging; canvas.onmousemove = moveDraw; /*控制圓是否可以拖動*/ function stopDragging() { isDragging = false; } /*設定一個暫存點選圓引數*/ var previousCircle; /*點選canvas*/ function canvasClick(e) { var clickX = e.pageX - canvas.offsetLeft; var clickY = e.pageY - canvas.offsetTop; for (i = circles.length - 1; i >= 0; i--) { var circle = circles[i]; var distance = Math.sqrt(Math.pow(circle.x - clickX, 2) + Math.pow(circle.y - clickY, 2)); if (distance <= radius) { if (previousCircle != null) { previousCircle.isSelected = false; } previousCircle = circle; isDragging = true; drawCircle(); return; } } } /*滑鼠拖動圓點或者滑鼠選擇圓點*/ function moveDraw(e) { var moveX = e.pageX - canvas.offsetLeft; var moveY = e.pageY - canvas.offsetTop; if (isDragging == true) { previousCircle.x = moveX; previousCircle.y = moveY; drawCircle(); } else { canvas.style.cursor = "default"; for (i = circles.length - 1; i >= 0; i--) { var circle = circles[i]; var distance = Math.sqrt(Math.pow(circle.x - moveX, 2) + Math.pow(circle.y - moveY, 2)); if (distance <= radius) { circle.isSelected = true; canvas.style.cursor = "move"; } else { circle.isSelected = false; } } drawCircle(); } } /*繪製圓、線條、曲線*/ drawCircle() function drawCircle() { context.clearRect(0, 0, canvas.width, canvas.height); context.globalAlpha = 0.85; //連線 context.beginPath(); context.moveTo(circles[0].x, circles[0].y); context.lineTo(circles[1].x, circles[1].y); context.lineTo(circles[2].x, circles[2].y); context.lineTo(circles[3].x, circles[3].y); context.lineWidth = 3; context.strokeStyle = "#ffffff"; context.stroke(); for (i = 0; i < circles.length; i++) { var circle = circles<i> //畫圓 context.beginPath(); context.arc(circle.x, circle.y, radius, 0, Math.PI * 2); context.strokeStyle = "#ffffff"; context.lineWidth = 4; if (circle.isSelected == true) { context.fillStyle = "#00ff00"; } else { context.fillStyle = "#ff0000"; } context.fill(); context.stroke(); } /*二次貝塞爾曲線*/ context.beginPath(); context.moveTo(circles[0].x, circles[0].y); context.bezierCurveTo(circles[1].x, circles[1].y, circles[2].x, circles[2].y, circles[3].x, circles[3].y); context.lineWidth = 4.5; context.strokeStyle = 'yellow'; context.stroke(); xy_text() } /*繪製座標*/ function xy_text() { console.log(circles) for (i = 0; i < circles.length; i++) { context.beginPath(); context.font = '12pt Arial'; context.fillStyle = "#0000ff"; var x_text = Math.floor(circles[i].x); var y_text = Math.floor(circles[i].y); context.fillText(x_text + "," + y_text, circles[i].x, circles[i].y + 30); context.closePath(); } } </script> </body> </html>
相關文章
- 貝塞爾曲線
- 貝塞爾曲線基礎部分
- 貝塞爾曲線理解與應用
- canvas 二次貝塞爾曲線quadraticCurveTo()Canvas
- canvas bezierCurveTo() 三次貝塞爾曲線Canvas
- 如何理解並應用貝塞爾曲線
- 貝塞爾曲線(Bezier curve)實現節點連線
- 自定義View合輯(6)-波浪(貝塞爾曲線)View
- webGL入門-四階貝塞爾曲線繪製Web
- 貝塞爾曲線原理、推導及Matlab實現Matlab
- VBA,Shapes.AddCurve SafeArrayOfPoints:=pts 畫貝塞爾曲線
- canvas基礎[二]教你編寫貝塞爾曲線工具Canvas
- 【Flutter高階玩法】 貝塞爾曲線的表象認知Flutter
- Flutter 自定義元件之貝塞爾曲線畫波浪球Flutter元件
- 一個貝塞爾曲線編輯工具(2d)
- Android-貝塞爾曲線實現水波紋動畫Android動畫
- 用貝塞爾曲線自己寫的一個電量顯示的控制元件控制元件
- Android日常學習:OpenGL 實踐之貝塞爾曲線繪製Android
- 自定義View合輯(8)-跳躍的小球(貝塞爾曲線)View
- SVG之Path路徑詳解(二),全面解析貝塞爾曲線SVG
- Android繪圖最終篇之大戰貝塞爾三次曲線Android繪圖
- 使用二階貝塞爾曲線實現新增購物車動畫動畫
- 白話經典貝塞爾曲線及其在 Android 中的應用Android
- 【乾貨滿滿】貝塞爾曲線(Bézier curve)——什麼神仙操作
- Flutter 實戰 - 用貝塞爾曲線畫一個帶文字的波浪球 WidgetFlutter
- Flutter BottomAppBar 自定義路徑 + 貝塞爾曲線實現閒魚底部導航FlutterAPP
- 貝塞爾曲線的css實現——淘寶加入購物車基礎動畫CSS動畫
- Android教你一步一步從學習貝塞爾曲線到實現波浪進度條Android
- JS 預編譯程式碼例項分析JS編譯
- table細線表格例項程式碼
- jquery.idTabs.min.js選項卡程式碼例項jQueryJS
- Android 窗簾(Curtain)效果四之賽貝爾曲線優化AndroidAI優化
- CSS transition animation的使用(內含貝賽爾曲線詳解)CSS
- 你知道嗎, CoreGraphics繪圖系統和Bezier貝塞爾曲線座標系的順時針方向是相反的!繪圖
- canvas繪製拋物線程式碼例項Canvas線程
- jQuery控制div顯示和隱藏程式碼例項jQuery
- 【flutter高階玩法】貝塞爾實戰1 - 波浪Flutter
- dom操作程式碼例項
- css梯形程式碼例項CSS