canvas绘制标尺

本文将介绍如何利用HTML5的canvas元素进行标尺的绘制,包括必要的HTML结构、CSS样式以及JavaScript实现,帮助你创建自定义的标尺预览功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


发布时间: 2019-02-19 16:38:39

/**
 * canvas 绘制标尺
 * 
 * params:{*}
 *      axisWidth:Number,轴线的宽度,单位px
 *      lineColor:String,轴线的颜色
 *      gridWidth:Number,大网格的宽度
 *      gridHeight:Number,大网格的高度
 * 
 * dom:Dom元素,canvas的元素
 */

function CanvasRuler(params, dom) {
  let AXIS_WIDTH = 1,
    LINE_COLOR = "#000000",
    GRID_WIDTH = 100,
    GRID_HEIGHT = 20;

  let canvas = dom;

  const DRAW_TYPE = {
    HORIZONTAL: 1,
    VERTICAL: 2,
    ALL: 3,
    NONE: 4
  };

  this.getDrawType = function() {
    return DRAW_TYPE;
  }

  this.setAxisWidth = function(axisWidth) {
    AXIS_WIDTH = Number(axisWidth) ? Number(axisWidth) : 1;
  }
  this.getAxisWidth = function() {
    return AXIS_WIDTH;
  }

  this.setLineColor = function(lineColor) {
    LINE_COLOR = lineColor;
  }
  this.getLineColor = function() {
    return LINE_COLOR;
  }

  this.setGridWidth = function(gridWidth) {
    GRID_WIDTH = Number(gridWidth) ? Number(gridWidth) : 1;
  }
  this.getGridWidth = function() {
    return GRID_WIDTH;
  }

  this.setGridHeight = function(gridHeight) {
    GRID_HEIGHT = Number(gridHeight) ? Number(gridHeight) : 1;
  }
  this.getGridHeight = function() {
    return GRID_HEIGHT;
  }

  this.getDomElement = function() { //清空canvas
    return canvas;
  }

  this.clearCanvas = function() {
    canvas.width = canvas.width;
  }

  this.setAxisWidth(params.axisWidth);
  this.setLineColor(params.lineColor);
  this.setGridWidth(params.gridWidth);
  this.setGridHeight(params.gridHeight);
}

CanvasRuler.prototype = {
  constructor: CanvasRuler,
  init: function(type) {
    this.clearCanvas();
    ctx = this.getDomElement().getContext('2d');
    ctx.lineWidth = this.getAxisWidth();
    ctx.strokeStyle = this.getLineColor();
    let DRAW_TYPE = this.getDrawType();
    switch (type) {
      case DRAW_TYPE.HORIZONTAL:
        this.drawHorizontalAxis(ctx);
        break;
      case DRAW_TYPE.VERTICAL:
        this.drawVerticalAxis(ctx);
        break;
      case DRAW_TYPE.ALL:
        this.drawHorizontalAxis(ctx);
        this.drawVerticalAxis(ctx);
        break;
      default:
        break;
    }
  },

  //绘制水平轴
  drawHorizontalAxis: function(ctx) {
    let gridWidth = this.getGridWidth();
    let gridHeight = this.getGridHeight();
    ctx.beginPath();
    ctx.moveTo(gridHeight + 0.5, gridHeight);
    ctx.lineTo(gridWidth + 0.5, gridHeight);
    for (let i = 0.5; i < gridWidth; i = i + gridWidth / 10) {
      ctx.moveTo(i, gridHeight);
      if (i < gridHeight) continue;
      if (i == gridWidth / 10 * 5) {
        ctx.lineTo(i, gridHeight / 2);
      } else {
        ctx.lineTo(i, gridHeight * 2 / 3);
      }
    }
    ctx.stroke();
    for (let i = gridWidth + 0.5; i < this.getDomElement().width; i = i + gridWidth) {
      ctx.beginPath();
      ctx.moveTo(i, 0);
      ctx.lineTo(i, gridHeight);
      ctx.lineTo(i + gridWidth, gridHeight);
      for (let j = i + gridWidth / 10; j < i + gridWidth; j = j + gridWidth / 10) {
        ctx.moveTo(j, gridHeight);
        if (j == i + gridWidth / 10 * 5) {
          ctx.lineTo(j, gridHeight / 2);
        } else {
          ctx.lineTo(j, gridHeight * 2 / 3);
        }
      }
      ctx.stroke();
    }
  },

  //绘制垂直轴
  drawVerticalAxis: function() {
    let gridWidth = this.getGridWidth();
    let gridHeight = this.getGridHeight();
    ctx.beginPath();
    ctx.moveTo(gridHeight, gridHeight + 0.5);
    ctx.lineTo(gridHeight, gridWidth + 0.5);
    for (let i = 0.5; i < gridWidth; i = i + gridWidth / 10) {
      ctx.moveTo(gridHeight, i);
      if (i < gridHeight) continue;
      if (i == gridWidth / 10 * 5) {
        ctx.lineTo(gridHeight / 2, i);
      } else {
        ctx.lineTo(gridHeight * 2 / 3, i);
      }
    }
    ctx.stroke();
    for (let i = gridWidth + 0.5; i < this.getDomElement().height; i = i + gridWidth) {
      ctx.beginPath();
      ctx.moveTo(0, i);
      ctx.lineTo(gridHeight, i);
      ctx.lineTo(gridHeight, i + gridWidth);
      for (let j = i + gridWidth / 10; j < i + gridWidth; j = j + gridWidth / 10) {
        ctx.moveTo(gridHeight, j);
        if (j == i + gridWidth / 10 * 5) {
          ctx.lineTo(gridHeight / 2, j);
        } else {
          ctx.lineTo(gridHeight * 2 / 3, j);
        }
      }
      ctx.stroke();
    }
  }

}

eg:预览链接

1.html

<canvas id="rulerCanvas"></canvas>

2.css

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

#rulerCanvas {
  background: #000;
}

3.js

var canvas = document.getElementById('rulerCanvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

window.canvasRuler = new CanvasRuler({
  axisWidth: 0.4,
  lineColor: '#fff',
  gridWidth: 100,
  gridHeight: 20
}, canvas);

canvasRuler.init(canvasRuler.getDrawType().ALL);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值