HTML5 canvas 在线画笔绘图工具(四)

HTML5画图命令

图形的绘制是由TDrawHandler与TCommand 协同工作完成。

TDrawHandler需要完成以下工作

1、聚集类用于管理绘图的命令 TCommand

2、管理鼠标事件 ,鼠标点击第一下开始绘图,鼠标点击第二下绘图完成。在点第一次和第二次之间在画板上拖动鼠标时系统动态的根据鼠标位置绘图。

3、将所有绘图命令生成json数据,以便于保存。

4、打开新的图形

TCommand类由 直线、矩形、圆几个基本命令组成。

画图控制类 TDrawHandler

如下代码所示

TDrawhandler 首先管理着一个CommandList记载得所有的绘图命令。并通过RedrawScreen 函数完成图形的绘制,该函数循环调用所有的绘图命令的RunCommand函数,完成在画布上的各自绘图任务,最终完成完整的图形绘制。

当检测到画布的第一次鼠标单击事件时,调用 Toolbar的getNewCommand函数,Toolbar会根据当前在Toolbar上选中的命令按钮生成对应的TCommand对象。

</pre><p></p><p><span style="font-size:14px"></span></p><pre name="code" class="javascript">function TDrawHandler(Canvas,Toolbar)
{
    var Context=Canvas.getContext(‘2d‘);
    this.Canvas=Canvas;
    this.Toolbar=Toolbar;
    var  isDrawing=false;
    var  CommandList=new Array();
    var  CommandCount=0;
    var  Title=‘未命名图片‘;
    var  Author=‘‘;
    var  Date=‘‘;
    var  Id=‘‘;
    var  Open=false;
    InstallEvents();
    function domousedown(event)
    {

       if (isDrawing==false)
       {

          CurrentCommand=Toolbar.getNewCommand(Canvas);
          CurrentCommand.setShapeProperty(Toolbar.getShapeProperty());
          if (CurrentCommand=="undefined") return;
          isDrawing=true;
          CommandList[CommandCount++]=CurrentCommand;
          CurrentCommand.setFirstPoint(event.clientX,event.clientY);
       } else
       {
          CurrentCommand.OnClick();
          if (CurrentCommand.Finished())
          {
            isDrawing=false;
          }
       }
    }
    function domouseup(event)
    {
       //isMouseDown=false;
    }
    function ClearScreen()
    {
      Context.clearRect(0,0,Canvas.width,Canvas.height);
    }

    function RedrawScreen()
    {

       ClearScreen();
       for (var i=0;i<CommandList.length;i++)
       {
          CommandList[i].RunCommand();
       }
    }
    function domousemove(event)
    {
       if (isDrawing==true)
       {
          CurrentCommand.setSecondPoint(event.clientX,event.clientY);
          //CurrentCommand.RunCommand();
          RedrawScreen();
       }
    }
    function InstallEvents()
    {
       Canvas.onmousedown=function(event)
       {
           domousedown(event);
       };
       Canvas.onmousemove=function(event)
       {
          domousemove(event);
       };
       Canvas.onmouseup=function(event)
       {
          domouseup(event);
       };
    }
    this.NewDrawing=function()
    {
       CommandList=new Array();
       CommandCount=0;
       RedrawScreen();
       Open=false;
    };
    this.SaveDrawing=function()
    {

    };
    this.haveCommand=function()
    {
    	return CommandList.length>0;
    };
    function getOneCommand(CommandItem)
    {
       var JSONCommand="{";
       JSONCommand=JSONCommand+‘"CommandType":"‘+CommandItem.CommandType()+‘",‘;
       JSONCommand=JSONCommand+‘"LineWidth":"‘+CommandItem.getLineWidth()+‘",‘;
       JSONCommand=JSONCommand+‘"BorderColor":"‘+CommandItem.getBorderColor()+‘",‘;
       var Point=CommandItem.getPoints();
       if (Point.length==0)
       {
          JSONCommand=JSONCommand+‘"Points":[{"x":"‘+CommandItem.getFirstPoint().x+‘","y":"‘+CommandItem.getFirstPoint().y+‘"},‘;
          JSONCommand=JSONCommand+‘{"x":"‘+CommandItem.getSecondPoint().x+‘","y":"‘+CommandItem.getSecondPoint().y+‘"}‘;
          JSONCommand=JSONCommand+"]";
       }else
       {

       }  

       JSONCommand=JSONCommand+"}";
       return JSONCommand;
    }
    this.DrawingToJSON=function()
    {
       var JSONData=‘{‘;
       JSONData=JSONData+‘"Title":"‘+Title+‘",‘;
       JSONData=JSONData+‘"Author":"‘+Author+‘",‘;
       JSONData=JSONData+‘"Date":"‘+Date+‘",‘;
       JSONData=JSONData+‘"Commands":[‘;
       for (var i=0;i<CommandList.length;i++)
       {
          if (CommandList[i].Finished())
            JSONData=JSONData+getOneCommand(CommandList[i]);
          if (i<CommandList.length-1)
            JSONData=JSONData+‘,‘;
       }
       JSONData=JSONData+‘]‘;
       JSONData=JSONData+‘}‘;
       return JSONData;
    };
    this.setId=function(id)
    {
       Id=id;
    };
    this.getId=function()
    {
       return Id;
    };
    function LoadJsonObject(DrawingGraphics) {

		Title = DrawingGraphics.Title;
		Author = DrawingGraphics.Author;
		CommandList.length=0;
		for (var i = 0; i < DrawingGraphics.Commands.length; i++) {
			var NewCommand = new TCommand(Canvas, DrawingGraphics.Commands[i].CommandType);
			CommandList[i] = NewCommand;

			if (DrawingGraphics.Commands[i].Points.length == 2) {
				NewCommand.setFirstPoint(DrawingGraphics.Commands[i].Points[0].x, DrawingGraphics.Commands[i].Points[0].y);
				NewCommand.setSecondPoint(DrawingGraphics.Commands[i].Points[1].x, DrawingGraphics.Commands[i].Points[1].y);
				ShapeProperty = new TShapeProperty();
				ShapeProperty.setLineWidth(DrawingGraphics.Commands[i].LineWidth);
				ShapeProperty.setLineColor(DrawingGraphics.Commands[i].BorderColor);
				NewCommand.setShapeProperty(ShapeProperty);
			} else {

			};
		}
		CommandCount=CommandList.length;
		RedrawScreen();
	}
    this.openDrawing=function(DrawingGraphics,drawingid)
    {
       id=drawingid;
       LoadJsonObject(DrawingGraphics);
       Open=true;
    };
    this.getOpen=function()
    {
       return Open;
    };
}

画图命令 TCommand

在TCommand中有三个内部类,分别是TLineCommand、TArcCommand,TRectCommand司职具体的图形绘制任务。

在建立TCommand时自动调用 CreateCommand根据命令的类型建立相应的Command对象。每一个Command内部类均有一个draw函数来完成具体的图形绘制任务。

function TCommand(Canvas,CommandType)
{
	var ctLine=1;
	var ctRect=2;
	var ctArc=3;
	var commandtype=CommandType;
	var Command;
	var firstPoint=new function(){var x;var y;};
	var secondPoint=new function(){var x;var y;};
    var Context=Canvas.getContext("2d");
    var BorderColor=‘#990000‘;
    var LineWidth=2;
    var FillColor=‘black‘;
    var ShapeProperty;
    csRunning=‘running‘;
    csFinish=‘finish‘;
    var State=csRunning;
    var Points=new Array();
    var Offset=new function(){var x=0;var y=0;};
	CreateCommand(CommandType);
	Offset.x=0;
	Offset.y=0;
	function CreateCommand(ct)
	{

		if (ct==1)
		  Command=new TLineCommand();
		else if (ct==2)
		  Command=new TRectCommand();
		else if (ct==3)
		  Command=new TArcCommand();
	}
	this.RunCommand=function()
	{
		Command.draw();
	};
	function LX(x)
	{
		return x-Canvas.offsetLeft+Offset.x;

	}
	function LY(y)
	{
		return y-Canvas.offsetTop+Offset.y;

	}
	function TLineCommand()
	{
		this.draw=function()
		{
			 Context.strokeStyle=BorderColor;
			 Context.lineWidth=LineWidth;
			 Context.beginPath();
			 Context.moveTo(LX(firstPoint.x),LY(firstPoint.y));
			 Context.lineTo(LX(secondPoint.x),LY(secondPoint.y));
			 Context.stroke();
			 Context.closePath();
		};
	}
	function TRectCommand()
	{
	    this.draw=function()
		{
			 Context.strokeStyle=BorderColor;
			 Context.lineWidth=LineWidth;
			 Context.strokeRect(LX(firstPoint.x),LY(firstPoint.y),LX(secondPoint.x)-LX(firstPoint.x),LY(secondPoint.y)-LY(firstPoint.y));
		};
	}
	function TArcCommand()
	{
	    this.draw=function()
		{

        Context.beginPath();
        dx=LX(secondPoint.x)-LX(firstPoint.x);
        dy=LY(secondPoint.y)-LY(firstPoint.y);
        r=dx>dy?dx:dy;
        if (r<0) return;
        Context.arc(LX(firstPoint.x),LY(firstPoint.y),r,0,2*Math.PI,1);
        Context.strokeStyle = BorderColor;
        Context.lineWidth = LineWidth;
        Context.stroke();
        Context.closePath();
		};
	}
	this.getLineWidth=function()
	{
		return LineWidth;
	};
	this.getBorderColor=function()
	{
		return BorderColor;
	};
	this.CommandType=function()
	{
		return commandtype;

	};
	this.setFirstPoint=function(x,y)
	{
		firstPoint.x=x;
		firstPoint.y=y;
	};
	this.getFirstPoint=function()
	{
		return firstPoint;
	};
	this.getSecondPoint=function()
	{
		return secondPoint;
	};
	this.setSecondPoint=function(x,y)
	{
		secondPoint.x=x;
		secondPoint.y=y;
	};
	this.setOffset=function (x,y)
	{
		Offset.x=x;
		Offset.y=y;
	};
	var Finish=function()
	{
		State=csFinish;
	};
	this.Finished=function()
	{
		return (State==csFinish);
	};
	this.OnClick=function()
	{
		if ((commandtype==ctLine)||(commandtype==ctRect)||(commandtype==ctArc))
		{
			Finish();
		}
	};
	this.setShapeProperty=function(value)
	{
		ShapeProperty=value;
		LineWidth=ShapeProperty.getLineWidth();
		BorderColor=ShapeProperty.getLineColor();
	};
	this.getPoints=function()
	{
		return Points;
	};
}
时间: 2024-08-06 17:33:23

HTML5 canvas 在线画笔绘图工具(四)的相关文章

HTML5 canvas 在线画笔绘图工具(三)

组装画板(TDrawBuilder) 在这一小节中我们要把工具条和画板组装起来,让他们可以协同进行工作. 画板通过一个命名为TDrawBuilder来进行组装.在详细讲解TDrawBuilder对象之前我们来看一下程序的HTML代码. 画布由三个Canvas组成 toolbar用于绘制工具条,drawCanvas 用于画图的画布,openCanvas 用于在打开保存的图片时显示小图片. 通过建立一个新的TDrawBuilder对象 new TDrawBuilder(toolbar,drawcan

HTML5 canvas 在线画笔绘图工具(一)

HTML5 canvas 在线画笔绘图工具(一) 功能介绍 这是我用Javascript写的第一个程序,在写的过程中走了很多弯路,所以写完之后想分享出来,给与我一样的初学者做为学习的参考,同时在编写这个程序时我也碰到一些问题,这里我也会一并的提出来给大家讨论,让我们都能得到进步.因为是初学javascript,所以水平较低,欢迎大家提出批评意见. 程序包含以下内容 1.工具条  2.基本的绘图命令,目前仅有 直线.矩形.圆 ,以后有空还会增加一些命令 3.线型选择 4.颜色选择 5.保存图素 6

HTML5 canvas 在线画笔绘图工具(二)

Canvas+Javascript 带图标的工具条制作 TToolbar 工具条是由一个TToolbar对象和两个按钮对象(TImageButton.TColorButton)组成,因为之前我大部分时间是使用d.e.l.p.h.i进行开发,所以命名方面比较偏向于d.e.l.p.h.i的风格,请处女座的同学忍耐将就一下. 图标按钮 TImageButton TImageButton 是一个图标按钮对象,可以设置三个图标文件,分别是正常状态,鼠标移上状态,鼠标点击状态. 下面我们介绍一下TImage

HTML5 canvas 在线画笔绘图工具(一) 功能介绍

这是我用Javascript写的第一个程序,在写的过程中走了很多弯路,所以写完之后想分享出来,给与我一样的初学者做为学习的参考,同时在编写这个程序时我也碰到一些问题,这里我也会一并的提出来给大家讨论,让我们都能得到进步.因为是初学javascript,所以水平较低,欢迎大家提出批评意见. 程序包含以下内容 1.工具条  2.基本的绘图命令,目前仅有 直线.矩形.圆 ,以后有空还会增加一些命令 3.线型选择 4.颜色选择 5.保存图素 6.打开图形. 下面是我儿子的作品,麻烦大家赞一个,谢谢 打开

HTML5 canvas 在线涂鸦

插件地址 http://bencentra.github.io/jq-signature/ 采用技术 jq-signature.min.js Developed using jQuery 2.1.4. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="j

10款最佳HTML5绘图工具

HTML5无疑是当前最受宠的一项技术,每个web设计者都在热烈的讨论这种神奇的标记语言的兴起.HTML5是一种学起来毫不费力的标记语言,但它却能提供迷人的.富有艺术性的特征,帮助web设计人员完成他们的构思想象.HTML5对于一个设计人员来说能毫不费力的理解,轻松的掌握. 草绘和制图是设计工作者工作中一个重要的特征.HTML5将会这方面显露出不可限量的前途. 今天,我给web设计人员收集了几款最好的HTML5绘图或涂鸦工具.这些绘图工具大多数是用HTML5画布(Canvas)实现的,部分辅以Ja

[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)

之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, 半径 ) cx,cy表示控制点的坐标,x2,y2表示结束点的坐标,如果我们想画一条弧线,需要提供3个 http://pic.cnhubei.com/space.php?uid=4593&do=album&id=1092946http://pic.cnhubei.com/space.php?ui

html5 canvas绘图-贝塞尔曲线

贝塞尔曲线(ezier curve)最迟是由法国物理学家与数学家paul de Casteljau发明的.它的广泛运用则要归功于法国工程师皮埃尔 贝塞尔 贝塞尔曲线期初被用在汽车车身的设计上.现在则多用于计算机图形系统中.例如Adobe Illustrator/Apple的Cocoa框架以及在Html5的canvas. 贝塞尔曲线分为两种:平方(quadratic)贝塞尔曲线及立方(cubic)贝塞尔曲线.平方贝塞尔曲线是一种二次曲线(second degree curve),意思就是说,它们是

HTML5 Canvas 绘图入门

HTML5 Canvas 绘图入门 HTML5 Canvas 绘图入门,仅供学习参考 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>HTML5 移动Web开发指南</title> <style type="text/css"> h1, h5 { text-align: center; } canvas {