美文网首页
angular5中使用canvas简单画板

angular5中使用canvas简单画板

作者: 梧桐芊雨 | 来源:发表于2019-05-23 20:40 被阅读0次

angular中使用canvas可以实现一个简单的画板绘制。
功能如下:
调整笔尖的大小
清空画板绘制内容
鼠标移动是绘制线条
画笔颜色修改


绘制.png
清空数据.png

blackboard.component.html

  <div class="page_title">
    白板测试
  </div>

    <nav class="menus">
     <div class="first">画笔大小选取
       <input type="number" [(ngModel)]="lineWidth" (click)="getlineWidth()">
     </div>
     <div class="colors">颜色值:
       <span class="colorItem" *ngFor="let item of colorList" [style.background]="item" (click)="getBgColor(item)"></span>
       <span class="eraser" (click)="getEraser()">清空/橡皮擦</span>
     </div>
    </nav>
    <section>
      <canvas id="canvas" [width]="canvasObj.canvasWith" [height]="canvasObj.canvasHeight">不支持canvas显示</canvas>
    </section>

</div>

.colorItem{
  display: inline-block;
  width: 30px;
  height: 30px;
  border: 1px solid gray;
  margin: 5px;
}
.menus{
  display: flex;
}
.colors{
    display: flex;
    align-items: center;
    justify-content: flex-start;
}

#canvas{
  background: white;
  border: 1px solid red;
}
.eraser, .picture{
  display: inline-block;
  height: 30px;
  line-height: 30px;
  background: #4693ff;
  color: white;
  padding: 0px 15px;
  margin: 10px;
}
import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { MatTableDataSource, MatDialog } from '@angular/material';

import { ApiService } from './../../core/service/api.service';
import { DeleteConfirmComponent } from './../../shared/components/delete-confirm/delete-confirm.component';

@Component({
  selector: 'app-blackboard',
  templateUrl: './blackboard.component.html',
  styleUrls: ['./blackboard.component.scss'],
  host: {
    '[class.right_content]': 'true',
  }
})
export class BlackboardComponent implements OnInit {

  canvasId: any;
  canvasPen: any;
  canvasObj: any = {//canvas的大小设置
    canvasWith: 800,
    canvasHeight: 800,
  };
  colorList = ['#f00', '#0f0', '#00f', '#000', '#fff'];//指定画笔颜色
  bgColor: string;
  lineWidth: number = 1;//画笔的大小
  isMouseDown = false;
  lastLoc = { x: 0, y: 0 };//初始位置值
  constructor(
    private dialog: MatDialog,
    private api: ApiService,
    private router: Router
  ) { }

  ngOnInit() {
    this.drawCanvas();
  }

//绘制画板中内容
  drawCanvas() {
    this.canvasId = document.getElementById('canvas');
    this.canvasPen = this.canvasId.getContext("2d");
    if (this.bgColor) {
      this.canvasPen.fillStyle = this.bgColor;
      this.canvasPen.strokeStyle = this.bgColor;
    }
    if (this.lineWidth) {
      this.canvasPen.lineWidth = this.lineWidth;
    }
    this.canvasId.onmousedown = (e) => {
      e.preventDefault();
      console.log("...按下onmousedown", e.pageX, e.pageY, e.clientX - 300, e.clientY - 194);
      this.isMouseDown = true;
      this.lastLoc = this.windowCanvas(e.clientX, e.clientY);
      this.drawPicture()
      // if(this.shapeObj.shapeUse){
      //   this.drawShapes(this.canvasId,this.canvasPen,this.shapeObj);
      //   console.log('靠,根本绘制不出来啊这个',this.drawShapes(this.canvasId,this.canvasPen,this.shapeObj));
      // }

      console.log(this.isMouseDown, '当前的位置坐标')
    }
  //鼠标按下,松开,移动,离开事件执行
    this.canvasId.onmouseup = (e) => {
      e.preventDefault();
      console.log("...松开onmouseup", e.pageX, e.pageY);
      this.isMouseDown = false;
    }
    this.canvasId.onmouseout = (e) => {
      e.preventDefault();
      console.log("...离开onmouseout");
      this.isMouseDown = false;
    }
    this.canvasId.onmousemove = (e) => {
      e.preventDefault();
      if (this.isMouseDown) {
        var curLoc = this.windowCanvas(e.clientX, e.clientY);
        this.canvasPen.beginPath();
        this.canvasPen.moveTo(this.lastLoc.x, this.lastLoc.y);
        this.canvasPen.lineTo(curLoc.x, curLoc.y);
        this.canvasPen.lineCap = "round";
        this.canvasPen.lineJoin = "round";
        this.canvasPen.stroke();
        this.lastLoc = curLoc;
      }
      console.log("...移动onmousemove");
    }
  }
//设置画笔大小
  getlineWidth() {
    this.drawCanvas();
  }
//设置画笔颜色
  getBgColor(item) {
    this.bgColor = item;
    this.drawCanvas();
  }

  /**
   * 获取canvas坐标
   */
  windowCanvas(x, y) {
    var ctxbox = this.canvasId.getBoundingClientRect();
    console.log('canvas坐标', Math.round(x - ctxbox.left), Math.round(y - ctxbox.top));
    return { x: Math.round(x - ctxbox.left), y: Math.round(y - ctxbox.top) };
  }

  /**
   * 橡皮擦:canvas的高度及宽度重置
   */
  getEraser() {
      this.canvasId.width = this.canvasObj.canvasWith;
      this.canvasId.height = this.canvasObj.canvasHeight;
  }

}

参看文档:
https://www.cnblogs.com/wanf/p/7238600.html

相关文章

  • angular5中使用canvas简单画板

    angular中使用canvas可以实现一个简单的画板绘制。功能如下:调整笔尖的大小清空画板绘制内容鼠标移动是绘制...

  • Canvas绘制圆形

    使用canvas绘制圆形步骤: 1、在页面中创建canvas的节点(相当于创建一个画板),设置CSS样式。(注意:...

  • 使用实现画板

    源码预览 是 HTML5新增的元素,可用于通过使用JavaScript中的脚本来绘制图形。例如,它可以用于绘制图形...

  • canvas2

    canvas简易画板

  • 基于canvas画板原理(H5)

    一个简单的小事例,利用canvas实现画板效果。实现原理:鼠标事件+canvas效果图: 代码: 如有问题欢迎交流...

  • 聊聊Android绘制中画笔Paint的用法

    Android的绘制和现实中的绘制相似,同样离不开画笔和画板,canvas就是Android绘制中的画板,提供了一...

  • Canvas画板

    一、实现思路 (非触屏设备) 监听鼠标事件①按下鼠标:onmousedown;滑动鼠标:onmousemove;松...

  • js相关html转canvas,canvas转pdf

    html转canvas html2canvas 安装 简单使用 拓展使用 canvas转pdf jsPDF 安装 ...

  • canvas2-text

    canvas画板结合JS事件实现写字效果

  • 用Canvas写消除泡泡游戏!!!

    这几天刚接触了canvas,写完一个画板游戏后,顿时感觉太这个项目太简单了,毕竟在网上看见了那么多的canvas项...

网友评论

      本文标题:angular5中使用canvas简单画板

      本文链接:https://www.haomeiwen.com/subject/dqqizqtx.html