Canvas绘制文字示例

 
<template>
  <div id="system-run-status-con"
       class=""
       style="width: 100%; height: 100%;"
       ref="srsCanvas"
  >
  </div>
</template>

<script>
export default {
   data () {
      // 系统运行状态信息
      sysRunInfo: "当前运行状况良好,无故障待处理",
      isSysRunHealth: false,

      // Deprecated
      canvasWidth: null,
      canvasHeight: null,
      sysRunInfoShow: 1,
      canvasPoints: [],
      canvasCtx: null
   },
   methods: {
       initCanvasWH: function () {
        this.canvasWidth = document.getElementById('system-run-status-chart').clientWidth;
        this.canvasHeight = document.getElementById('system-run-status-chart').clientHeight;
      },
      drawCanvasText: function () {
        let that = this;
        that.isHealth = false;

        isHealth.initCanvasWH();

        debugger
        let canvas = isHealth.$refs.srsCanvas;
        // 获取上下文
        this.context = canvas.getContext('2d');
        canvas.width = this.canvasWidth;
        canvas.height = this.canvasHeight;
        // 绘制文字
        const font = '当前运行状况良好\n\n无故障待处理';
        this.context.clearRect(0, 0, canvas.width, canvas.height);
        this.context.save(); // 保存
        // 绘制视窗
        this.context.beginPath();

        this.context.font = '0.26rem Verdana';
        // 创建渐变
        let gradient = this.context.createLinearGradient(0, 0, canvas.width, 0);
        /*gradient.addColorStop(0, 'magenta');
        gradient.addColorStop(0.5, 'blue');
        gradient.addColorStop(1.0, 'red');*/
        gradient.addColorStop(0, 'rgb(201, 62, 255)');
        gradient.addColorStop(0.5, 'rgb(137, 150, 254)');
        gradient.addColorStop(1.0, 'rgb(81, 247, 253)');
        // 用渐变色
        this.context.fillStyle = gradient;
        // measureText方法获取文字的宽度,为了能够在画布中间绘制文字.
        const measure = this.context.measureText(font);

        this.context.fillText(font, (this.canvasWidth - measure.width)/2, this.canvasHeight/2);

        this.context.restore(); // 状态恢复


        // 延迟清除画布
        /*setTimeout(function () {
          that.context.clearRect(0, 0, canvas.width, canvas.height);
          that.sysRunInfoShow = true;
        }, 3000);*/
      }
   }
}

</script>

<style>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyJiang.2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值