canvas矩形拖拽效果

admin發表於2018-07-09

本章節分享一下使用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>

相關文章