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>
相關文章
- Android 貝塞爾曲線Android
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- 貝塞爾曲線基礎部分
- UIBezierPath貝塞爾曲線UI
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- 貝塞爾曲線理解與應用
- iOS UIBezierPath 貝塞爾曲線iOSUI
- bezierMaker.js——N階貝塞爾曲線生成器JS
- 如何理解並應用貝塞爾曲線
- Android 自定義貝塞爾曲線工具Android
- 貝塞爾曲線開發的藝術
- canvas實現高階貝塞爾曲線Canvas
- 簡易製作貝塞爾曲線動畫(JS+css3+canvas)動畫JSCSSS3Canvas
- canvas bezierCurveTo() 三次貝塞爾曲線Canvas
- iOS UIBezierPath貝塞爾曲線常用方法iOSUI
- 使用貝塞爾曲線裁圓優化tableView優化View
- Path從懵逼到精通(2)——貝塞爾曲線
- 安卓自定義 View 進階:貝塞爾曲線安卓View
- 貝塞爾曲線(Bezier curve)實現節點連線
- 用canvas繪製一個曲線動畫——深入理解貝塞爾曲線Canvas動畫
- javascript資料曲線圖例項程式碼JavaScript
- 自定義View合輯(6)-波浪(貝塞爾曲線)View
- canvas 二次貝塞爾曲線quadraticCurveTo()Canvas
- SVG <path> C 指令 三次貝塞爾曲線SVG
- SVG <path> Q指令 二次貝塞爾曲線SVG
- webGL入門-四階貝塞爾曲線繪製Web
- html5中canvas繪製貝塞爾曲線HTMLCanvas
- iOS開發之畫圖板(貝塞爾曲線)iOS
- 【Flutter高階玩法】 貝塞爾曲線的表象認知Flutter
- Flutter 自定義元件之貝塞爾曲線畫波浪球Flutter元件
- Android-貝塞爾曲線實現水波紋動畫Android動畫
- canvas基礎[二]教你編寫貝塞爾曲線工具Canvas
- SVG <path>元素C指令三次貝塞爾曲線SVG
- SVG <path>元素Q指令二次貝塞爾曲線SVG
- html5中canvas貝塞爾曲線繪製菊花HTMLCanvas
- 一個貝塞爾曲線編輯工具(2d)
- 貝塞爾曲線原理、推導及Matlab實現Matlab
- 用貝塞爾曲線自己寫的一個電量顯示的控制元件控制元件