初学canvas画板

312 阅读1分钟

1、关于canvas

  • canvas是一个inline元素,类似image
  • canvas的高度是在html 的canvas标签中确定,在CSS中修改宽高会变成拉伸的效果,出现模糊
  • 使用documen.documenElenment来获取、设定宽度:
 canvas.width = document.documentElement.clientWidth;
 canvas.height = document.documentElement.clientHeight;
  • ctx.lineCap = "round"用于让线绘制的更加平滑
  • 绘制直线
    let ctx = canvas.getContext("2d");
    ctx.lineWidth = 20;
    ctx.beginPath();
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
  • 注意
    1. vh 和 vw 单位属于CSS单位,不能写在HTML中,HTML不支持
    2. canvas功能在微信浏览器上存在bug

2、检测是否支持触屏功能

    var isTouchDevice = "ontouchstart" in document.documentElement;
    console.log(isTouchDevice);