canvas基本圖形繪制應該都會了,那么這篇講下給canvas繪制的圖形添加事件
兩個方式:
1. 使用ctx.isPointInPath(x, y),判斷點在不在圖形內,精確但效率低
2. 使用矩形范圍,判斷點是否在圖形的矩形范圍內,不精確但效率高
效果:

代碼:
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas圖形事件</title>
<style>
*{margin: 0;padding: 0;}
</style>
</head>
<body>
<canvas id="canvas" data-percent="10"></canvas>
<script>
var $ = document.querySelector.bind(document);
var canvas = $('#canvas'),
canvasWth = 500;
canvasHgt = 500;
canvas.width = canvasWth;
canvas.height = canvasHgt;
canvas.style.backgroundColor = '#999';
var ctx = canvas.getContext('2d');
// 圖形點存儲
var pointsObj = {};
// 矩形(使用fillRect方法不行)
ctx.fillStyle = '#F86731';
ctx.rect(100, 100, 200, 100);
ctx.fill();
// 點在圖形內
for(var i=100; i<300; i++) {
for(var j=100; j<200; j++) {
if(ctx.isPointInPath(i, j)) {
if(!pointsObj['rect']) {
pointsObj['rect'] = {};
}
pointsObj['rect']['x_'+i] = i;
pointsObj['rect']['y_'+j] = j;
}
}
}
// 三角形
ctx.beginPath();
ctx.moveTo(100, 300);
ctx.lineTo(160, 380);
ctx.lineTo(20, 320);
ctx.lineTo(100, 300);
ctx.fillStyle = '#12C1BC';
ctx.fill();
ctx.closePath();
// 點在圖形內
for(var i=20; i<160; i++) {
for(var j=300; j<380; j++) {
if(ctx.isPointInPath(i, j)) {
if(!pointsObj['triangle']) {
pointsObj['triangle'] = {};
}
pointsObj['triangle']['x_'+i] = i;
pointsObj['triangle']['y_'+j] = j;
}
}
}
// 圓形
ctx.beginPath();
ctx.arc(250, 330, 50, 0, 2*Math.PI, true);
ctx.fillStyle = '#0ACB15';
ctx.fill();
ctx.closePath();
// 點在圖形矩形范圍內
pointsObj['circle'] = {
'xMin': 200,
'xMax': 300,
'yMin': 280,
'yMax': 380
}
// 點擊事件
canvas.addEventListener('click', function(e) {
var x = e.pageX,
y = e.pageY;
// 矩形
if(pointsObj['rect']['x_'+x] && pointsObj['rect']['y_'+y]) {
console.log('rect');
}
// 三角形
else if(pointsObj['triangle']['x_'+x] && pointsObj['triangle']['y_'+y]) {
console.log('triangle');
}
// 圓
else if(pointsObj['circle'].xMin<x && x<pointsObj['circle'].xMax && pointsObj['circle'].yMin<y && y<pointsObj['circle'].yMax) {
console.log('circle');
}
});
// 鼠標移動事件
canvas.addEventListener('mousemove', function(e) {
var x = e.pageX,
y = e.pageY;
// 矩形
if(pointsObj['rect']['x_'+x] && pointsObj['rect']['y_'+y]) {
canvas.style.cursor = 'pointer';
}
// 三角形
else if(pointsObj['triangle']['x_'+x] && pointsObj['triangle']['y_'+y]) {
canvas.style.cursor = 'pointer';
}
// 圓
else if(pointsObj['circle'].xMin<x && x<pointsObj['circle'].xMax && pointsObj['circle'].yMin<y && y<pointsObj['circle'].yMax) {
canvas.style.cursor = 'pointer';
// 改變圓顏色
ctx.clearRect(200, 280, 100, 100);
ctx.beginPath();
ctx.arc(250, 330, 50, 0, 2*Math.PI, true);
ctx.fillStyle = '#1886EA';
ctx.fill();
ctx.closePath();
}
// 默認
else {
canvas.style.cursor = 'default';
// 恢復圓顏色
ctx.clearRect(200, 280, 100, 100);
ctx.beginPath();
ctx.arc(250, 330, 50, 0, 2*Math.PI, true);
ctx.fillStyle = '#0ACB15';
ctx.fill();
ctx.closePath();
}
});
</script>
</body>
</html>
~~~
**休息了。。。**

- 事件
- mouse縮放與拖動
- drag拖動
- 事件兼容
- animation/transition
- canvas
- 改變圖片顏色
- html轉圖片
- 視頻操作
- 圖片縮放、水印、放大鏡
- 虛線
- 圓環進度條
- 形狀事件
- 圓角矩形
- 繪制注意
- arcTo與貝塞爾
- 橢圓及橢圓進度
- 五角星進度
- 常用圖形
- 計算顯示文本寬度
- 算法
- 幾何算法
- 地圖應用相關
- 運行符
- web安全
- 新窗口打開
- xss
- 分享交流
- php環境搭建及xhr交互
- node環境搭建及xhr交互
- node之socketio
- svg之入門介紹
- svg動畫
- vue之搜索聯想
- vue之登錄和echarts
- vue之組件交互與slot
- vue之loading
- vue之上傳進度
- webpack及cli
- 開發技巧
- 常用
- 移動端
- 錯誤處理
- 預加載
- 代理判斷
- 數組擴展
- 對象擴展
- 字符串擴展
- 語音播報
- 收集
- 文章/日記
- 框架/庫/插件
- 工具
- 學習網站
- 專業術語
- 正則
- 常用驗證
- 方法基礎
- es6擴展
- 深入實踐
- 快捷使用
- html
- css
- http協議
- http
- https
- socket
- 地圖/圖表
- mapbox
- echarts
- arcgis
- MapView及事件
- 添加WMS/WMTS層
- 增刪點線面
- 入門使用
- popup彈層
- 大數據處理
- 批量點
- 批量線
- 在線繪制
- GraphicLayer顯示/隱藏
- 動態改變位置
- 去除版權信息
- 添加控件
- Symbol
- 自定義path標記
- 圖片標記
- 文本標記
- 旋轉
- UI
- 自定義
- 3D地圖
- 創建實例
- basemap
- 底圖切換
- 自定義底圖
- 中心和范圍
- pupup彈層更新
- 坐標轉換
- 方向線
- leaflet
- amap
- 框架/類庫/腳手架
- vue
- 常見問題
- 組件框架
- vue-router
- 命名視圖
- url參數映射到prop
- sublime支持
- 隨手記
- 常用功能
- threejs
- 常用效果
- 其他特效
- requirejs
- 簡單使用
- jquery
- 方法擴展
- 使用筆記
- 組件擴展
- react
- 黨見問題
- 學習筆記
- 學習筆記-進階
- react-redux
- react-router
- redux
- 其他模塊說明
- 組件框架
- sublime支持
- gulp
- 安裝使用
- js壓縮
- css壓縮
- 組合使用
- copy文件
- 項目使用
- protobuf
- 入門
- layui
- 登錄驗證
- laydate
- 安裝工具
- yarn
- reactNative
- 入門介紹
- vueNative
- 入門介紹
- 版本控制
- git常用
- git擴展
- git問題
- git其他
- git擴展2
- 編輯器
- vscode
- atom
- webstorm
- 插件
- clipboard
- 奇淫巧技
- js
- 個性打印
- css
- 濾鏡效果
- 文本省略
- 當前色
- 新特性
- 花樣邊框效果
- 波紋效果
- 個性placeholder
- 偽元素內容
- 容器居中
- 知識點
- js
- 遞歸
- 沙箱
- 內存泄漏
- es6語法
- 變量介紹
- FileRead
- ajax
- web存儲
- css
- rem布局