<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
起因是有一個專案,需要在同一個canvas中渲染一批幾何圖形,當滑鼠移動到其中某一個圖形中,對這個形狀高亮處理。基本實現方式是監聽mousemove
事件,回撥中傳入當前滑鼠的位置,同時遍歷所有圖形,判斷點是否在這個形狀中,找到當前選中的元素並重新渲染canvas。
const canvas = document.getElementById('my-canvas'); const ctx = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(event) { const x = event.clientX - canvas.offsetLeft; const y = event.clientY - canvas.offsetTop; // Check each polygon to see if the mouse is inside for (let i = 0; i < polygons.length; i++) { const polygon = polygons[i]; // Check if the mouse is inside the polygon if (isPointInside(polygon, x, y)) { console.log('Mouse is inside polygon ' + i); break; } } });
當圖形的量級持續上升,意味著JS邏輯執行時間同步增加,滑鼠移動過快必然出現卡頓(低FPS)。
這個問題有很多優化的角度:
isPointInside()
;我初步實現的isPointInside()
主要依賴幾何座標的計算,這裡主要針對矩形、圓形、多邊形實現:
/** * 判斷點是否在形狀內 * @param shape * @param point * @param type * @returns */ export const isPointInside = ( shape: IRect | ICircle | IPolygon, point: IPoint, type: EElementType, ): boolean => { if (!shape || !point) return false; switch (type) { case EElementType.Rect: { const rect = shape as IRect; return ( rect.x <= point.x && rect.x + rect.width >= point.x && rect.y <= point.y && rect.y + rect.height >= point.y ); } case EElementType.Circle: { const circle = shape as ICircle; return ( Math.sqrt( Math.pow(point.x - circle.x, 2) + Math.pow(point.y - circle.y, 2), ) <= circle.radius ); } case EElementType.Polygon: { const polygon = shape as IPolygon; // 將多邊形劃分為若干個三角形 for (let i = 1; i < polygon.length - 1; i++) { // 計算三角形的三個頂點和點 P 的叉積 const cross1 = crossProduct(polygon[0], polygon[i], point); const cross2 = crossProduct(polygon[i], polygon[i + 1], point); const cross3 = crossProduct(polygon[i + 1], polygon[0], point); // 如果三個叉積的符號都相同,則說明點在三角形內部 if (cross1 * cross2 > 0 && cross1 * cross3 > 0) { return true; } } return false; } default: return false; } };
但當我把這個問題拋給ChatGpt時發現了另一種可能性:
const canvas = document.getElementById('my-canvas'); const ctx = canvas.getContext('2d'); canvas.addEventListener('mousemove', function(event) { const x = event.clientX - canvas.offsetLeft; const y = event.clientY - canvas.offsetTop; // Check each polygon to see if the mouse is inside for (let i = 0; i < polygons.length; i++) { const polygon = polygons[i]; // Set the path and color of the polygon ctx.beginPath(); ctx.moveTo(polygon.path[0].x, polygon.path[0].y); for (let j = 1; j < polygon.path.length; j++) { ctx.lineTo(polygon.path[j].x, polygon.path[j].y); } ctx.closePath(); ctx.fillStyle = polygon.color; // Check if the mouse is inside the polygon if (ctx.isPointInPath(x, y)) { console.log('Mouse is inside polygon ' + i); break; } } });
他推薦了Canvas內建的API Context.isPointInPath(path,x,y)
判斷一個點是否被一個路徑包圍。
MDN這樣介紹:developer.mozilla.org/zh-CN/docs/…
這個API此前我並未留意,當我把自己的方法同時轉達時,ChatGpt這樣評價和對比兩種方法:
The internal APIisPointInPath() is likely to be faster than the custom implementation of isPointInside()that you have provided, as it is a built-in method of the canvas API optimized for detecting whether a point is inside a path.
isPointInPath() is optimized for performance and is likely to be faster than your implementation, which calculates the shape of the annotation and checks if the point is inside. However, the exact performance difference depends on the complexity of the shapes and the number of annotations you have on the canvas.
Therefore, it's recommended to use isPointInPath() for performance reasons, especially if you have a large number of annotations. You can create a path for each annotation, and then check if the mouse pointer is inside any of the paths using isPointInPath()
出於效能考慮內建方法更好?為什麼好?好到什麼程度?
於是就有個接下來的實驗。
const canvas = document.createElement('canvas'); document.body.appendChild(canvas); const count = 1000; const width = 1500; const height = 1500; canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); // Create random shapes const shapes = []; const createPathFromPoints = (points) => { const path = new Path3D(); path.moveTo(points[0].x, points[0].y); for (let i = 1; i < points.length; i++) { path.lineTo(points[i].x, points[i].y); } path.closePath(); return path; }; const createCirclePathByPoint = (center, radius) => { const path = new Path3D(); path.arc(center.x, center.y, radius, 0, 2 * Math.PI); path.closePath(); return path; }; for (let i = 0; i < count; i++) { const type = ['circle', 'rect', 'polygon'][Math.floor(Math.random() * 2)]; let shape; let path; switch (type) { case 'rect': { shape = { x: Math.random() * canvas.width, y: Math.random() * canvas.height, width: Math.random() * 30, height: Math.random() * 30, }; const { x, y, width, height } = shape; path = createPathFromPoints([{x, y}, {x: x + width, y: y}, {x: x + width, y: y + height}, {x, y: y + height}]); break; } case 'circle': shape = { x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 20, }; path = createCirclePathByPoint({ x: shape.x, y: shape.y }, shape.radius); break; case 'polygon': shape = [ { x: Math.random() * canvas.width, y: Math.random() * canvas.height } ]; for(let i = 1; i < Math.floor(Math.random() * 10); i++) { shape.push({ x: shape[i-1].x + Math.random() * 20, y: shape[i-1].y + Math.random() * 20 }); } path = createPathFromPoints(shape); break; } shapes.push({ shape, type, path }); } function renderAllShapes(shapes, selectedIndex) { shapes.forEach(({ shape, type}, index) => { ctx.fillStyle = randomColor(); switch (type) { case 'rect': ctx.fillRect(shape.x, shape.y, shape.width, shape.height); break; case 'circle': ctx.beginPath(); ctx.arc(shape.x, shape.y, shape.radius, 0, 2 * Math.PI); ctx.fill(); break; case 'polygon': ctx.beginPath(); ctx.moveTo(shape[0].x, shape[0].y); for (let i = 1; i < shape.length; i++) { ctx.lineTo(shape[i].x, shape[i].y); } ctx.closePath(); ctx.fill(); break; } }) } renderAllShapes(shapes); let customWin = 0; let builtinWin = 0; canvas.addEventListener('mousemove', (e) => { const point = { x: e.clientX - canvas.offsetLeft, y: e.clientY - canvas.offsetTop }; // Method 1 const start1 = performance.now(); const result1 = shapes.findIndex(({ shape, type }) => { return isPointInside(shape, point, type); }); const end1 = performance.now(); // Method 2 const start2 = performance.now(); const result2 = shapes.findIndex(({ path }) => { return ctx.isPointInPath(path, point.x, point.y); }) const end2 = performance.now(); if ((end1 - start1) < (end2 - start2)) { customWin++; } else if ((end1 - start1) > (end2 - start2)) { builtinWin++; } renderAllShapes(shapes); console.log(result1, result2); console.log(end1 - start1, end2 - start2); console.log(customWin, builtinWin); });
上述程式碼canvas中隨機建立了count個形狀,分別使用兩種方法判斷滑鼠hover形狀,採用performance.now()毫秒級的記錄執行時間。
同時執行兩種方法,當count=1000時,FPS > 55正常使用,但是當count=10000時,FPS < 20,說明批次判斷存在效能瓶頸。
Count | 自定義 | 內建 |
---|---|---|
1000 | 0.030 | 0.150 |
2000 | 0.038 | 0.243 |
3000 | 0.060 | 0.310 |
根據控制檯列印,兩種方法當前hover元素的判斷一致,但執行時間上,90%的情況下,自定義實現的isPointInside()
優於內建APIisPointInPath()
。
所以,ChatGpt可以不負責任的講結論,內建API也不一定是最優解,實踐是唯一標準。
以上就是Canvas如何判斷點在形狀內及內建API效能詳解的詳細內容,更多關於Canvas內建API效能的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45