canvas简单画板效果

快乐打工仔 分类:实例代码

canvas简单画板效果属于前端实例代码,有关更多实例代码大家可以查看

分享一段代码实例,它实现了简单的画板效果。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>犀牛前端部落</title>
<script type="text/javascript">
window.onload = function() {
  var oC = document.getElementById('c1');
  var oGC = oC.getContext('2d');
  oC.onmousedown = function(ev) {
    var ev = ev || window.event;
    oGC.moveTo(ev.clientX - oC.offsetLeft, ev.clientY - oC.offsetTop);
    document.onmousemove = function(ev) {
      var ev = ev || window.event;
      oGC.lineTo(ev.clientX - oC.offsetLeft, ev.clientY - oC.offsetTop);
      oGC.stroke();
    };
    document.onmouseup = function() {
      document.onmousemove = null;
      document.onmouseup = null;
    };
  };
};
</script>
</head>
<body>
<div>
  <canvas id="c1" width="400" height="450"></canvas>
</div>
</body>
</html>

canvas简单画板效果,这样的场景在实际项目中还是用的比较多的,关于canvas简单画板效果就介绍到这了。

回复

我来回复
  • 暂无回复内容