html5 canvas 标签

Posted 心随所遇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html5 canvas 标签相关的知识,希望对你有一定的参考价值。

<canvas id="board" width="500" height="400"></canvas>
<script type="text/javascript">
function drawImg(canvasId, lineWidth, lineColor){
  var canvas = document.getElementById(canvasId);
  var canvasIdJq= ‘#‘ + canvasId;
  if (canvas.getContext){
    var ctx = canvas.getContext("2d");
    ctx.lineWidth = lineWidth; 
    ctx.strokeStyle = lineColor;
    var draw = false;
    $(canvasIdJq).mousedown(function(e){
      var mouseX = e.pageX - this.offsetLeft;//页面横坐标 减去 画板对象的左边距
      var mouseY = e.pageY - this.offsetTop; 
      ctx.moveTo(mouseX,mouseY); //定位到按下鼠标时的位置
      draw = true;
    });
    $(canvasIdJq).mouseup(function(e){
       draw = false;
    });
    $(canvasIdJq).mousemove(function(e){
      var mouseX = e.pageX - this.offsetLeft;
      var mouseY = e.pageY - this.offsetTop; 
        if(draw){
            ctx.lineTo(mouseX,mouseY); //终止位置
            ctx.stroke(); //结束图形
         }
    });
  }
}

drawImg(‘board‘,3,‘red‘);
</script>

 

以上是关于html5 canvas 标签的主要内容,如果未能解决你的问题,请参考以下文章

[html5] 学习笔记-Canvas标签的使用

html5 canvas 标签

HTML5 Canvas 支持和 Android Webview

HTML5:Canvas 标签

HTML5 - Canvas标签<使用;

HTML5 - Canvas标签<介绍;