canvas矩形拖拽效果
本章節分享一下使用canvas實現的拖拽效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload = function() { var can = document.getElementById('box'), gd = can.getContext('2d'), x = 0, y = 0, w = 100, h = 100; gd.fillStyle = 'red'; gd.fillRect(x, y, w, h); can.onmousedown = function(ev) { var ev = ev || window.event; var downx = ev.clientX; var downy = ev.clientY; if (downx > x && downy < x + w && downy > y && downy < y + h) { var disx = downx - x; var disy = downy - y; can.onmousemove = function(ev) { var ev = ev || window.event; x = ev.clientX - disx; y = ev.clientY - disy; gd.clearRect(0, 0, can.width, can.height); gd.fillRect(x, y, w, h); } can.onmouseup = function() { can.onmousemove = null; can.onmouseup = null; } return false; } } } </script> </head> <body> <canvas id="box" width="800" height="800"></canvas> </body> </html>
相關文章
- canvas具有漸變效果的矩形Canvas
- canvas透明度的矩形效果Canvas
- canvas 虛線矩形Canvas
- canvas 繪製矩形Canvas
- JavaScript拖拽效果JavaScript
- canvas 設定矩形樣式Canvas
- canvas 繪製圓角矩形Canvas
- canvas繪製矩形框Canvas
- canvas 繪製矩形缺角Canvas
- 原生JS拖拽效果JS
- 使用canvas畫出一個矩形Canvas
- canvas系列教程之繪製矩形Canvas
- CSS3 矩形切角效果CSSS3
- svg生成矩形效果程式碼SVG
- canvas實現手動繪製矩形Canvas
- 實現小程式canvas拖拽功能Canvas
- canvas簽名效果Canvas
- canvas小球碰壁效果Canvas
- JavaScript元素拖拽路徑回放效果JavaScript
- JavaScript 限定範圍的拖拽效果JavaScript
- canvas實現波浪效果Canvas
- canvas蔚藍星空效果Canvas
- canvas繪製流星效果Canvas
- canvas小球擺動效果Canvas
- canvas-座標系、圓角矩形、紋理、剪裁Canvas
- 微信小程式 canvas圓角矩形的繪製微信小程式Canvas
- 使用BottomSheetBehavior實現美團拖拽效果
- canvas設定陰影效果Canvas
- canvas圓形時鐘效果Canvas
- canvas簡單畫板效果Canvas
- canvas繪製風車效果Canvas
- canvas動態時鐘效果Canvas
- 原來 Canvas 也能直接繪製圓角矩形了Canvas
- canvas學習筆記-繪製矩形及路徑(一)Canvas筆記
- Canvas系列之一——濾鏡效果Canvas
- canvas環形進度條效果Canvas
- canvas實現 漂亮的下雨效果Canvas
- canvas立體效果的圓環Canvas