js-canvas(基本用法)

###1. canvas(画布)

  <canvas>是HTML 5 新增的元素,可用于通过使用JavaScript中的脚本来绘制图形

    默认宽高为300px*150px

  基本概念和方法入门推荐: http://www.w3school.com.cn/tags/html_ref_canvas.asp

###2.渲染上下文

  <canvas>元素只是创造了一个固定大小的画布,要想在它上面绘制内容,我们需要找到它的渲染上下文

    <canvas>元素有一个getContex()方法,这个方法是用来获取渲染上下文和它的绘画功能

    

  基本用法:

      <canvas id="cav" width="300px" height="150px"></canvas>

      var canvas = document.querySelector("#cav")

      if(canvas.getContext){    //  必须判断是否存在该方法,即判断浏览器是否支持canvas

        var ctx = canvas.getContext("2d");

      }

附加: canvas作品资源  https://www.html5tricks.com/tag/html5-canvas/

猜你喜欢

转载自www.cnblogs.com/john-hwd/p/10556500.html