Home > Article > Web Front-end > Use ActionScript-like syntax to write html5 - Part 5, Graphics drawing
Canvas itself is a Graphics and can be drawn directly
In actionscript, each Sprite has a Graphics. I will not consider Shape for now. It is easier to implement.
In Html5, drawing is are drawn on the same canvas, so we need to consider two issues now,
1. How to draw the Graphics in each Sprite at the same place at different times
2, because we are now The page is constantly refreshed, so if we use Graphics to draw, then it also needs to be constantly refreshed
Then I still assume that the Graphics stored in each Sprite only saves some drawing commands. When these drawing commands are drawn, they are all drawn on the canvas.
So according to the assumption, I need an array or class to save these drawing commands.
I will now build an LGraphics class, which should contain drawing instructions, and show method
function LGraphics(){ var self = this; self.type = "LGraphics"; self.color = "#000000"; self.i = 0; self.alpha = 1; self.setList = new Array(); self.showList = new Array(); } LGraphics.prototype = { show:function (){ var self = this; if(self.setList.length == 0)return; //绘图 } }
When I am drawing, I add all the drawing instructions to setList, and then call the show method to draw
There is also a showList to save the drawing area, which will work for a while Just got it
Let’s solve the problem of how to store instructions
Add methods to LGraphics
drawLine:function (thickness,lineColor,pointArray){ var self = this; self.setList.push(function(){ LGlobal.canvas.beginPath(); LGlobal.canvas.moveTo(pointArray[0],pointArray[1]); LGlobal.canvas.lineTo(pointArray[2],pointArray[3]); LGlobal.canvas.lineWidth = thickness; LGlobal.canvas.strokeStyle = lineColor; LGlobal.canvas.closePath(); LGlobal.canvas.stroke(); }); }, drawRect:function (thickness,lineColor,pointArray,isfill,color){ var self = this; self.setList.push(function(){ LGlobal.canvas.beginPath(); LGlobal.canvas.rect(pointArray[0],pointArray[1],pointArray[2],pointArray[3]); if(isfill){ LGlobal.canvas.fillStyle = color; LGlobal.canvas.fill(); } LGlobal.canvas.lineWidth = thickness; LGlobal.canvas.strokeStyle = lineColor; LGlobal.canvas.stroke(); }); self.showList.push({type:"rect",value:pointArray}); }, drawArc:function(thickness,lineColor,pointArray,isfill,color){ var self = this; self.setList.push(function(){ LGlobal.canvas.beginPath(); LGlobal.canvas.arc(pointArray[0],pointArray[1],pointArray[2],pointArray[3],pointArray[4],pointArray[5]); if(isfill){ LGlobal.canvas.fillStyle = color; LGlobal.canvas.fill(); } LGlobal.canvas.lineWidth = thickness; LGlobal.canvas.strokeStyle = lineColor; LGlobal.canvas.stroke(); }); self.showList.push({type:"arc",value:pointArray}); }
The three methods are to draw a line, a rectangle, and a circle
Because the instructions I stored It is function
So, when I draw, I can directly call the method
So, slightly modify the show method
show:function (){ var self = this; if(self.setList.length == 0)return; var key; for(key in self.setList){ self.setList[key](); } }
The drawing class is completed. Please see the source code for the complete class later
Then, add self.graphics = new LGraphics(); in the constructor of LSprite, and you can draw at any time
The code is as follows
backLayer = new LSprite(); addChild(backLayer); //画一圆 backLayer.graphics.drawRect(1,"black",[20, 20, 150, 20],true,"#cccccc"); //画一个矩形 backLayer.graphics.drawArc(2,"black",[100, 100, 50, 0,2*Math.PI,false],true,"#FF0000"); //画一条线 backLayer.graphics.drawLine(2,"#FF0000",[200, 20, 100, 50]);
In fact, there are still some shortcomings, Because when the mouse clicks on the LSprite to judge, I only judge the bitmap etc. saved in the LSprite. If a picture is drawn in the LSprite, it should also respond to mouse events when clicked, so it is necessary to judge whether the clicked position is in the drawing area
Actually, it’s simple. Just add an ismouseon method to LGraphics to determine whether it is clicked.
ismouseon:function(event,cood){ var self = this; var key; for(key in self.showList){ if(self.showList[key].type == "rect"){ if(event.offsetX >= self.showList[key].value[0] + cood.x && event.offsetX <= self.showList[key].value[0] + cood.x + self.showList[key].value[2] && event.offsetY >= self.showList[key].value[1] + cood.y && event.offsetY <= self.showList[key].value[1] + cood.y + self.showList[key].value[3]){ return true; } }else if(self.showList[key].type == "arc"){ var xl = self.showList[key].value[0] + cood.x - event.offsetX; var yl = self.showList[key].value[1] + cood.y - event.offsetY; return xl*xl+yl*yl <= self.showList[key].value[2]*self.showList[key].value[2]; } } return false; }
showList saves the drawing area size, which comes in handy now
init(80,"mylegend",800,480,main); var backLayer; function main(){ legendLoadOver(); backLayer = new LSprite(); addChild(backLayer); //画一圆 backLayer.graphics.drawRect(1,"black",[20, 20, 150, 20],true,"#cccccc"); //画一个矩形 backLayer.graphics.drawArc(2,"black",[100, 100, 50, 0,2*Math.PI,false],true,"#FF0000"); //画一条线 backLayer.graphics.drawLine(2,"#FF0000",[200, 20, 100, 50]); //鼠标点击判断 backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown); } function onmousedown(event){ alert("isclick"); }
above It is to use ActionScript-like syntax to write html5 - Part 5, Graphics drawing content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!