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
- canvas 虛線矩形Canvas
- JavaScript拖拽效果JavaScript
- canvas 繪製圓角矩形Canvas
- canvas 繪製矩形缺角Canvas
- canvas 設定矩形樣式Canvas
- canvas繪製矩形框Canvas
- 原生JS拖拽效果JS
- 自定義拖拽效果
- canvas系列教程之繪製矩形Canvas
- CSS3 矩形切角效果CSSS3
- svg生成矩形效果程式碼SVG
- html5中canvas繪製矩形HTMLCanvas
- canvas實現手動繪製矩形Canvas
- 實現小程式canvas拖拽功能Canvas
- jQuery div拖拽效果詳解jQuery
- canvas小球碰壁效果Canvas
- canvas簽名效果Canvas
- 微信小程式 canvas圓角矩形的繪製微信小程式Canvas
- canvas繪製圓角矩形程式碼例項Canvas
- css帶有空心箭頭矩形效果CSS
- CSS3矩形左右擺動效果CSSS3
- Canvas drag 實現拖拽拼圖小遊戲Canvas遊戲
- JavaScript 限定範圍的拖拽效果JavaScript
- JavaScript元素拖拽路徑回放效果JavaScript
- jQuery拖拽的彈出層效果jQuery
- canvas-座標系、圓角矩形、紋理、剪裁Canvas
- HTML5 Canvas ( 矩形的繪製 ) rect, strokeRect, fillRectHTMLCanvas
- canvas 繪製旋轉一定角度的矩形Canvas
- svg實現矩形水平運動動畫效果SVG動畫
- svg矩形圍繞自身中心旋轉效果SVG
- css3實現的矩形切角效果CSSS3
- canvas繪製流星效果Canvas
- canvas實現波浪效果Canvas
- canvas小球擺動效果Canvas