软件项目技术点(7)——在canvas上绘制自定义图形

AxeSlide软件项目梳理   canvas绘图系列知识点整理

图形种类

目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比较合适的库。

下图中所有的图形都是我们自己写代码在canvas上绘制方法出来的,可以改变实心/空心,改变颜色,大小宽高,线条弯曲度,透明度等。

软件项目技术点(7)——在canvas上绘制自定义图形

父类shape类

实现的代码如下:所有的图形均继承自shape类,而shape类继承自CommonElement,shape中的所有图形添加到画布上都是一个元素。

这个类里面有很多功能函数:

prepareStyle() { //绘制准备context绘制属性

setPro() { }//绘制前准备参数

public thiner() {//变细

public thicker() {//加粗

changeColor(color: string, callBack: Function) {//改变绘制颜色

changeAlpha(alpha: number, callBack: Function) {//改变透明度

changeFill(isFill: boolean) {//切换实心空心

drawPath() { }//绘制图形路径,所有图形都会重写该方法

draw() {//绘制调用入口

  export class Shape extends CommonElement {
shapeParameter: ShapeParameter;
tempFill: boolean;
tempStroke: boolean;
constructor(id: string, config: Config, context: CanvasRenderingContext2D, shapeParameter: ShapeParameter, typeName) {
super(id, config, context, typeName);
this.shapeParameter = shapeParameter;
}
prepareStyle() { //绘制准备context绘制属性
if (this.shapeParameter.computeLineWidth && this.shapeParameter.isStroke) {
this.shapeParameter.lineWidth = Math.max(2, Math.min(this.config.width * editor.canvas.canvasImp.scale, this.config.height * editor.canvas.canvasImp.scale) / this.shapeParameter.lineWidthScale); }
var ctx = this.context;
ctx.fillStyle = Common.Util.makeRGBA(this.shapeParameter.fillStyle, this.shapeParameter.alpha);
ctx.strokeStyle = Common.Util.makeRGBA(this.shapeParameter.strokeStyle, this.shapeParameter.alpha);
ctx.lineWidth = this.shapeParameter.lineWidth / editor.canvas.canvasImp.scale; }
public setPro() { }//绘制前准备参数
public drawPath() { }//绘制图形路径,所有图形都会重写该方法
public draw() {//绘制调用入口
//保存context状态
this.context.save();
this.rotate();//将画布旋转到位
this.setPro();
this.prepareStyle();
this.drawPath();
//恢复context状态
this.context.restore();
}

所有图形类都有的一个属性ShapeParameter 具体内容如下,每个属性值都有各自的作用

   export class ShapeParameter {//图形属性参数
lineWidth: number;
isFill: boolean;
isStroke: boolean
fillStyle: string;
strokeStyle: string
lineWidthScale: number;
isAutiClockwise: boolean;
computeLineWidth: boolean;
alpha: number;
lineDash: any;
constructor() {
this.lineWidth = 1;
this.isStroke = true;
this.isFill = false;
this.fillStyle = editor.currentShapeColor || editor.canvas.canvasImp.theme.shapeColor;
this.strokeStyle = editor.currentShapeColor || editor.canvas.canvasImp.theme.shapeColor;
this.lineWidthScale = lineWidthScale;
this.isAutiClockwise = false;
this.computeLineWidth = true;
this.alpha = editor.currentShapAlpha || 1;
this.lineDash = [10, 15];
}
}

特殊图形绘制计算方法

下面介绍几种绘制各种图形用到的特殊计算方法

如下图中的一个线条,我们可以拖拽中间控制点形成一个弧,这个弧形是某个圆的一部分。

我们定义左侧的第一个点为startPoint,中间的点为controlPoint,右侧的点为endPoint。

我们在绘制出这个图形时,需要知道如何求这三个点确定的圆的圆心坐标怎么计算,还有通过两点计算(x1,y1)为圆心,圆上某点(x2,y2)的角度的计算方法

软件项目技术点(7)——在canvas上绘制自定义图形

         GetCenter(): Point {//获取三个点,确定的一个圆的中心点坐标
var p1 = this.startPoint;
var p2 = this.controlPoint;
var p3 = this.endPoint;
var C1 = new Point((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0);
var C2 = new Point((p2.x + p3.x) / 2.0, (p2.y + p3.y) / 2.0);
var I = new Point(0, 0);;
var k1: number, k2: number;
if ((p2.y - p1.y) == 0.0) {
if ((p3.y - p2.y) == 0.0) {
return null;
} else {
k2 = -1 * (p3.x - p2.x) / (p3.y - p2.y);
I.x = C1.x; I.y = k2 * (I.x - C2.x) + C2.y;
}
} else {
k1 = -1 * (p2.x - p1.x) / (p2.y - p1.y);
if ((p3.y - p2.y) == 0.0) {
I.x = C2.x;
I.y = k1 * (I.x - C1.x) + C1.y;
} else {
k2 = -1 * (p3.x - p2.x) / (p3.y - p2.y);
if (k1 == k2) {
return null;
} else {
I.x = (C2.y - C1.y + k1 * C1.x - k2 * C2.x) / (k1 - k2); I.y = k1 * (I.x - C1.x) + C1.y;
}
}
}
return I;
}
         //通过两点计算x1,y1为圆心,圆上某点(x2,y2)的角度(按照arc的角度计算)
static computeAng(x1, y1, x2, y2) {
var ang = (x1 - x2) / (y1 - y2);
ang = Math.atan(ang);
if (x1 == x2 && y2 > y1) {
return 0.5 * Math.PI;
}
if (x1 == x2 && y2 < y1) {
return 1.5 * Math.PI;
}
if (y1 == y2 && x2 > x1) {
return 0;
}
if (y1 == y2 && x2 < x1) {
return Math.PI;
}
if (x2 > x1 && y2 > y1) {
return Math.PI / 2 - ang;
}
else if (x2 < x1 && y2 > y1) {
return Math.PI / 2 - ang;
}
else if (x2 < x1 && y2 < y1) {
return 3 * Math.PI / 2 - ang;
}
else if (x2 > x1 && y2 < y1) {
return 3 * Math.PI / 2 - ang;
}
}

另外再介绍一下我们的这个箭头是如何实现的

软件项目技术点(7)——在canvas上绘制自定义图形

         drawIsosceles(context: CanvasRenderingContext2D) {//画等腰箭头
var triangleSide = this.triangleSide;
//First the center of the triangle base (where we start to draw the triangle)
var centerBaseArrowX = this.basePoint.x ;
var centerBaseArrowY = this.basePoint.y; //Let's calculate the first point, easy!
var ax = centerBaseArrowX + (triangleSide / 2) * Math.cos(this.centerAngle);
var ay = centerBaseArrowY + (triangleSide / 2) * Math.sin(this.centerAngle); //Now time to get mad: the farest triangle point from the arrow body
var bx = centerBaseArrowX + (1/ 2/Math.sqrt(3) ) * triangleSide * (Math.sin(-this.centerAngle));
var by = centerBaseArrowY + ( 1/ 2/Math.sqrt(3)) * triangleSide * (Math.cos(-this.centerAngle)); //Easy , like the a point
var cx = centerBaseArrowX - (triangleSide / 2) * Math.cos(this.centerAngle);
var cy = centerBaseArrowY - (triangleSide / 2) * Math.sin(this.centerAngle); context.beginPath();
//We move to the center of the base
context.moveTo(centerBaseArrowX, centerBaseArrowY);
context.lineTo(ax, ay);
context.lineTo(bx, by);
context.lineTo(cx, cy);
context.lineTo(centerBaseArrowX, centerBaseArrowY);
context.fill();
}

在canvas上绘制多边形的方法

        drawPath(context: CanvasRenderingContext2D) {
for (var ixVertex = 0; ixVertex <= this.nPoints; ++ixVertex) {
var angle = ixVertex * 2 * Math.PI / this.nPoints - this.startAngle;
var point = new Point(this.centerPoint.x + this.radius * Math.cos(angle), this.centerPoint.y + this.radius * Math.sin(angle));
context.lineTo(point.x, point.y);
}
}

在canvas上绘制五角星等多角形的方法

         drawPath(context:CanvasRenderingContext2D) {
for (var ixVertex = 0; ixVertex <= 2 * this.nPoints; ++ixVertex) {
var angle = ixVertex * Math.PI / this.nPoints - Math.PI / 2;
var radius = ixVertex % 2 == 0 ? this.outerRadius : this.innerRadius;
context.lineTo(this.centerPoint.x + radius * Math.cos(angle), this.centerPoint.y + radius * Math.sin(angle));
}
}
上一篇:js中this关键字的使用


下一篇:AlertDialog详解