一波HTML5 Canvas基础绘图实例代码集合

2019-01-28 15:28:56刘景俊

基本绘制

XML/HTML Code复制内容到剪贴板
  1. var canvas = document.getElementById('canvas');    if (canvas.getContext) {   
  2.     var context = canvas.getContext('2d');        // 线宽   
  3.     context.lineWidth = 4;        // 画笔颜色   
  4.     context.strokeStyle = 'red';        // 填充色   
  5.     context.fillStyle = "red";        // 线帽类型   
  6.     context.lineCap = 'butt'; // round, square        // 开始路径   
  7.     context.beginPath();        // 起点   
  8.     context.moveTo(10,10);        // 终点   
  9.     context.lineTo(150,50);        // 绘制   
  10.     context.stroke();    }  

    
矩形

XML/HTML Code复制内容到剪贴板
  1. var canvas = document.getElementById('canvas');    if (canvas.getContext) {   
  2.     context.beginPath();        context.strokeRect(10,10,70,40);   
  3.     // 矩形的另一种方式        context.rect(10,10.70,40);   
  4.     context.stroke();            
  5.     // 实心矩形        context.beginPath();   
  6.     context.fillRect(10,10,70,40);        // 另一种方式实心矩形   
  7.     context.beginPath();        context.rect(10,10,70,40);   
  8.     context.fill();    }  

     
 圆形

XML/HTML Code复制内容到剪贴板