canvas-學寫字

1、邏輯思惟javascript

  1. 初始化的時候加入畫布,先畫田字格,方法很簡單,線條畫
  2. 四種操做,點擊(onmousedown),移開(onmouseup),移走(onmouseout),移動(onmousemove)
  3. 點擊的時候進行操做:記錄點擊區域座標,能夠畫
  4. 移開,移走的時候進行操做:不能夠畫
  5. 移動:記錄位置變化,記錄速度改變線寬,鏈接線條,上一次位置,上一次時間,上一次線寬不斷更替
  6. 速度算法,自由發揮

2、代碼html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>writeFont</title>
</head>

<body>
  <canvas id="canvas" width="500" height="500"></canvas>

  <script>
    window.onload = function () {
      var canvas = document.getElementById("canvas");
      var context = canvas.getContext('2d');
      var color = 'green';

      drawGrid();

      // 畫田字格
      function drawGrid() {
        context.save();
        context.strokeStyle = "rgb(230,11,9)";
        context.beginPath();
        context.moveTo(3, 3);
        context.lineTo(canvas.width - 3, 3);
        context.lineTo(canvas.width - 3, canvas.height - 3);
        context.lineTo(3, canvas.height - 3);
        context.closePath();

        context.lineWidth = 6;
        context.stroke();

        context.beginPath();
        context.moveTo(0, 0);
        context.lineTo(canvas.width, canvas.height);

        context.moveTo(canvas.width, 0);
        context.lineTo(0, canvas.height);

        context.moveTo(canvas.width / 2, 0);
        context.lineTo(canvas.width / 2, canvas.height);

        context.moveTo(0, canvas.width / 2);
        context.lineTo(canvas.width, canvas.height / 2);
        context.lineWidth = 1;
        context.stroke();
        context.restore();
      }

      var isMouseDown = false; //鼠標是否按下
      var lastLoc = { x: null, y: null };//鼠標上一次所在位置
      var lastTimestamp = 0;//時間戳
      var lastLineWidth = -1;//上一次線條寬度
      //鼠標按下
      canvas.onmousedown = function (e) {
        e.preventDefault();
        initLoc(e.clientX, e.clientY);
        isMouseDown = true;
      }
      //鼠標起來
      canvas.onmouseup = function (e) {
        e.preventDefault();
        isMouseDown = false;
      }
      //鼠標離開
      canvas.onmouseout = function (e) {
        e.preventDefault();
        isMouseDown = false;
      }
      //鼠標移動
      canvas.onmousemove = function (e) {
        e.preventDefault();
        if (isMouseDown) {
          //draw
          var curLoc = windowToCanvas(e.clientX, e.clientY);//得到當前座標
          var curTimestamp = new Date().getTime();//當前時間
          var s = calcDistance(curLoc, lastLoc);//得到運筆距離
          var t = curTimestamp - lastTimestamp;//運筆時間
          var lineWidth = calcLineWidth(t, s);

          context.lineWidth = lineWidth;

          context.beginPath();
          context.moveTo(lastLoc.x, lastLoc.y);
          context.lineTo(curLoc.x, curLoc.y);

          context.strokeStyle = color;
          context.lineCap = "round"
          context.lineJoin = "round"
          context.stroke();

          lastLoc = curLoc;
          lastLineWidth = lineWidth;
          lastTimestamp = curTimestamp;
        }

      }

      //得到canvas座標
      function windowToCanvas(x, y) {
        var bbox = canvas.getBoundingClientRect();
        return { x: Math.round(x - bbox.left), y: Math.round(y - bbox.top) };
        if(!lastLoc.x){
          lastLoc = { x: Math.round(x - bbox.left), y: Math.round(y - bbox.top) };
        }
      }
      //初始化上一次loc座標
      function initLoc(x, y) {
        var bbox = canvas.getBoundingClientRect();
        lastLoc = { x: Math.round(x - bbox.left), y: Math.round(y - bbox.top) };
      }
      //求兩點之間距離
      function calcDistance(loc1, loc2) {
        return Math.sqrt((loc1.x - loc2.x) * (loc1.x - loc2.x) + (loc1.y - loc2.y) * (loc1.y - loc2.y));
      }
      //求速度
      function calcLineWidth(t, s) {
        var v = s / t;
        var resultLineWidth;
        if (v <= 0.1) {
          resultLineWidth = 30;
        } else if (v >= 10) {
          resultLineWidth = 1;
        } else {
          resultLineWidth = 30 - (v - 0.1) / (10 - 0.1) * (30 - 1);
        }
        if (lastLineWidth == -1) {
          return resultLineWidth;
        }
        return lastLineWidth * 2 / 3 + resultLineWidth * 1 / 3;
      }
    }
  </script>
</body>

</html>
相關文章
相關標籤/搜索