LGraphics Class
The LGraphics class contains a set of methods that you can use to create a vector shape. Display objects that support drawing include LSprite and LShape objects. Each of these classes includes a graphics property that is a LGraphics object. The following are among those helper functions provided for ease of use: drawRect(), drawRoundRect(), drawArc(), and drawEllipse().
Constructor
LGraphics
()
public
Example:
LInit(50, "legend", 800, 480, main);
function main () {
var shape = new LShape();
addChild(shape);
shape.graphics.drawRect(2, "#ff0000", [10, 10, 50, 100], true, "#880088");
}
Item Index
Methods
Properties
Methods
add
-
func
canvas's methods that you can use to create a vector shape.
Parameters:
-
func
Functiona function
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.add(function(){
var ctx = LGlobal.canvas;
ctx.beginPath();
ctx.strokeStyle = "#FF0000";
ctx.lineWidth = 2;
ctx.moveTo(10,10);
ctx.lineTo(130,30);
ctx.stroke();
});
arc
-
x
-
y
-
radius
-
startAngle
-
endAngle
-
counterclockwise
Creates an arc/curve (used to create circles, or parts of circles)
Parameters:
-
x
FloatThe x-coordinate of the center of the circle
-
y
FloatThe y-coordinate of the center of the circle
-
radius
FloatThe radius of the circle
-
startAngle
FloatThe starting angle, in radians (0 is at the 3 o'clock position of the arc's circle)
-
endAngle
FloatThe ending angle, in radians
-
counterclockwise
BooleanOptional. Specifies whether the drawing should be counterclockwise or clockwise. False is default, and indicates clockwise, while true indicates counter-clockwise.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.arc(100,75,50,0,2*Math.PI);
shape.graphics.stroke();
beginBitmapFill
-
bitmap
Fills a drawing area with a bitmap image.
Parameters:
-
bitmap
LBitmapDataA transparent or opaque bitmap image that contains the bits to be displayed.
Example:
LInit(50, "legend", 800, 480, main);
function main () {
var loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE, loadBitmapdata);
loader.load("face.jpg", "bitmapData");
}
function loadBitmapdata (event) {
var bitmapdata = new LBitmapData(event.currentTarget);
var backLayer;
backLayer = new LSprite();
addChild(backLayer);
backLayer.graphics.beginBitmapFill(bitmapdata);
backLayer.graphics.drawArc(1,"#000000",[150,50,50,0,Math.PI*2]);
backLayer = new LSprite();
addChild(backLayer);
backLayer.graphics.beginBitmapFill(bitmapdata);
backLayer.graphics.drawRect(1,"#000000",[10,100,70,100]);
backLayer = new LSprite();
addChild(backLayer);
backLayer.graphics.beginBitmapFill(bitmapdata);
backLayer.graphics.drawVertices(1,"#000000",[[120,100],[100,200],[200,150]]);
}
beginPath
()
public
Begins a path, or resets the current path
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.lineWidth(5);
shape.graphics.strokeStyle("#FF0000");
shape.graphics.moveTo(0,75);
shape.graphics.lineTo(250,75);
shape.graphics.stroke();
shape.graphics.beginPath();
shape.graphics.strokeStyle("#00FF00");
shape.graphics.moveTo(50,0);
shape.graphics.lineTo(150,130);
shape.graphics.stroke();
callParent
-
functionName
-
arguments
call the method of parent。
Parameters:
-
functionName
Stringfunction's name
-
arguments
ArrayFixed value : arguments
Example:
function funA(){
LExtends(this,LObject,[]);
}
funA.prototype.myName = function(){
return "AAA";
}
function funB(){
LExtends(this,funA,[]);
}
funB.prototype.myName = function(){
return "BBB";
}
function funC(){
LExtends(this,funA,[]);
}
funC.prototype.myName = function(){
return this.callParent("myName",arguments);
}
LInit(1000/50,"legend",800,150,main);
function main(){
LGlobal.setDebug(true);
var objB = new funB();
trace(objB.myName());//BBB
var objC = new funC();
trace(objC.myName());//AAA
}
clear
()
public
Clear all the vector shapes.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.rect(20,20,150,100);
shape.graphics.clear();
shape.graphics.arc(100,75,50,0,2*Math.PI);
shape.graphics.stroke();
closePath
()
public
Creates a path from the current point back to the starting point
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(20,100);
shape.graphics.lineTo(70,100);
shape.graphics.closePath();
shape.graphics.stroke();
drawArc
-
thickness
-
color
-
param
-
isFill
-
fillColor
Draws a Circle or Sector.
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[x,y,r,sAngle,eAngle,counterclockwise,isSector]:[The x-coordinate of the center of the circle,The y-coordinate of the center of the circle,radius The radius of the circle,startAngle The starting angle,endAngle The ending angle,Specifies whether the drawing should be counterclockwise or clockwise,whether the drawing should be a sector]。
-
isFill
BooleanWhether to Fills the current drawing
-
fillColor
StringFills color
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawArc(2, "#ff0000", [50, 50, 40, 0, 2*Math.PI]);
shape.graphics.drawArc(1, "#000000", [50, 150, 40, 0, 2*Math.PI], true, "#880088");
shape.graphics.drawArc(2, "#ff0000", [150, 50, 40, 0, 50*Math.PI/180,false,true]);
shape.graphics.drawArc(1, "#000000", [150, 150, 40, 0, 230*Math.PI/180,false,true], true, "#880088");
drawEllipse
-
thickness
-
color
-
param
-
isFill
-
fillColor
Draws an ellipse.
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[x,y,width,height]:[The x location of the top-left of the bounding-box of the ellipse relative to the registration point of the parent display object (in pixels),The y location of the top left of the bounding-box of the ellipse relative to the registration point of the parent display object (in pixels),The width of the ellipse (in pixels),The height of the ellipse (in pixels)]。
-
isFill
BooleanWhether to Fills the current drawing
-
fillColor
StringFills color
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawEllipse(2, "#ff0000", [10, 10, 100, 50]);
shape.graphics.drawEllipse(1, "#000000", [10, 100, 50, 100], true, "#880088");
drawLine
-
thickness
-
color
-
param
Draw a line
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[startX,startY,endX,endY]:[The x-coordinate of the start,The y-coordinate of the start,The x-coordinate of the end,The y-coordinate of the end]。
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawLine(2, "#ff0000", [10, 10, 100, 50]);
shape.graphics.drawLine(1, "#000000", [10, 100, 50, 100]);
drawRect
-
thickness
-
color
-
param
-
isFill
-
fillColor
Draws a rectangle.
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[x,y,width,height]:[A number indicating the horizontal position relative to the registration point of the parent display object,A number indicating the vertical position relative to the registration point of the parent display object,The width of the rectangle,The height of the rectangle]。
-
isFill
BooleanWhether to Fills the current drawing
-
fillColor
StringFills color
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawRect(2, "#ff0000", [10, 10, 100, 50]);
shape.graphics.drawRect(1, "#000000", [10, 100, 50, 100], true, "#880088");
drawRoundRect
-
thickness
-
color
-
param
-
isFill
-
fillColor
Draws a rounded rectangle.
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[x,y,width,height,radius]:[A number indicating the horizontal position relative to the registration point of the parent display object,A number indicating the vertical position relative to the registration point of the parent display object,The width of the rectangle,The height of the rectangle,The size of the ellipse used to draw the rounded corners]。
-
isFill
BooleanWhether to Fills the current drawing
-
fillColor
StringFills color
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawRoundRect(2, "#ff0000", [10, 10, 100, 50, 10]);
shape.graphics.drawRoundRect(1, "#000000", [10, 100, 50, 100, 10], true, "#880088");
drawTriangles
-
vertices
-
indices
-
uvtData
-
thickness
-
color
Renders a set of triangles, typically to distort bitmaps and give them a three-dimensional appearance. The drawTriangles() method maps either the current fill, or a bitmap fill, to the triangle faces using a set of (u,v) coordinates.
Parameters:
-
vertices
ArrayA Vector of Numbers where each pair of numbers is treated as a coordinate location (an x, y pair). The vertices parameter is required.
-
indices
ArrayA Vector of integers or indexes, where every three indexes define a triangle. If the indexes parameter is null then every three vertices (six x,y pairs in the vertices Vector) defines a triangle. Otherwise each index refers to a vertex, which is a pair of numbers in the vertices Vector. For example indexes[1] refers to (vertices[2], vertices[3]). The indexes parameter is optional, but indexes generally reduce the amount of data submitted and the amount of data computed.
-
uvtData
ArrayA Vector of normalized coordinates used to apply texture mapping. Each coordinate refers to a point on the bitmap used for the fill. You must have one UV or one UVT coordinate per vertex. In UV coordinates, (0,0) is the upper left of the bitmap, and (1,1) is the lower right of the bitmap.
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
Example:
var bitmapdata = new LBitmapData(event.currentTarget);
var backLayer = new LSprite();
addChild(backLayer);
var vertices = new Array();
vertices.push(0, 0);
vertices.push(-40, 90);
vertices.push(0, 200);
vertices.push(80, 0);
vertices.push(90, 30);
vertices.push(70,200);
vertices.push(130, 10);
vertices.push(140, 40);
vertices.push(120,220);
var indices = new Array();
indices.push(0, 3, 1);
indices.push(3, 1, 4);
indices.push(1, 4, 2);
indices.push(4, 2, 5);
indices.push(3, 6, 4);
indices.push(6, 4, 7);
indices.push(4, 7, 5);
indices.push(7, 5, 8);
var uvtData = new Array();
uvtData.push(0, 0);
uvtData.push(0, 0.5);
uvtData.push(0, 1);
uvtData.push(0.5, 0);
uvtData.push(0.5, 0.5);
uvtData.push(0.5, 1);
uvtData.push(1, 0);
uvtData.push(1, 0.5);
uvtData.push(1, 1);
backLayer.graphics.beginBitmapFill(bitmapdata);
backLayer.graphics.drawTriangles(vertices, indices, uvtData);
drawVertices
-
thickness
-
color
-
param
-
isFill
-
fillColor
Draws a Polygon.
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
-
param
Array[[x1,y1],[x2,y2],[x3,y3],......]
-
isFill
BooleanWhether to Fills the current drawing
-
fillColor
StringFills color
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.drawVertices(2, "#ff0000", [[10, 10], [60, 100], [100, 50]]);
shape.graphics.drawVertices(2, "#ff0000", [[10, 160], [60, 250], [100, 200]], true, "#880088");
fill
()
public
Fills the current drawing (path)
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.fillStyle("#FF0000");
shape.graphics.rect(20,20,150,100);
shape.graphics.fill();
fillStyle
-
style
Sets or returns the color, gradient, or pattern used to fill the drawing
Parameters:
-
style
Stringcolor|gradient|pattern
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.fillStyle("#FF0000");
shape.graphics.rect(20,20,150,100);
shape.graphics.fill();
lineCap
-
value
Sets or returns the style of the end caps for a line
Parameters:
-
value
StringThe value "round" and "square" make the lines slightly longer.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.lineWidth(10);
shape.graphics.lineCap("butt");
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(200,20);
shape.graphics.stroke();
shape.graphics.beginPath();
shape.graphics.lineCap("round");
shape.graphics.moveTo(20,40);
shape.graphics.lineTo(200,40);
shape.graphics.stroke();
shape.graphics.beginPath();
shape.graphics.lineCap("square");
shape.graphics.moveTo(20,60);
shape.graphics.lineTo(200,60);
shape.graphics.stroke();
lineJoin
-
value
The lineJoin property sets or returns the type of corner created, when two lines meet.
Parameters:
-
value
Stringbevel|round|miter.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.lineWidth(10);
shape.graphics.lineJoin("round");
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(100,50);
shape.graphics.lineTo(20,100);
shape.graphics.stroke();
lineStyle
-
thickness
-
color
Sets the style of line
Parameters:
-
thickness
IntAn integer that indicates the thickness of the line in points;
-
color
Stringcolor|gradient|pattern
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.lineStyle(5,"#FF0000");
shape.graphics.rect(20,20,150,100);
shape.graphics.stroke();
lineTo
-
x
-
y
Adds a new point and creates a line from that point to the last specified point in the canvas
Parameters:
-
x
FloatThe x-coordinate of where to create the line to
-
y
FloatThe y-coordinate of where to create the line to
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(70,100);
shape.graphics.stroke();
lineWidth
-
value
Sets or returns the current line width
Parameters:
-
value
Stringthe current line width.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.lineWidth(2);
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(200,50);
shape.graphics.stroke();
shape.graphics.beginPath();
shape.graphics.lineWidth(10);
shape.graphics.moveTo(20,40);
shape.graphics.lineTo(200,40);
shape.graphics.stroke();
moveTo
-
x
-
y
Moves the path to the specified point in the canvas, without creating a line
Parameters:
-
x
FloatThe x-coordinate of where to move the path to
-
y
FloatThe x-coordinate of where to move the path to
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.beginPath();
shape.graphics.moveTo(20,20);
shape.graphics.lineTo(70,100);
shape.graphics.stroke();
rect
-
x
-
y
-
width
-
height
Creates a rectangle
Parameters:
-
x
FloatThe x-coordinate of the upper-left corner of the rectangle
-
y
FloatThe y-coordinate of the upper-left corner of the rectangle
-
width
FloatThe width of the rectangle, in pixels
-
height
FloatThe height of the rectangle, in pixels
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.rect(20,20,150,100);
shape.graphics.stroke();
stroke
()
public
Actually draws the path you have defined
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.strokeStyle("#FF0000");
shape.graphics.lineWidth(5);
shape.graphics.lineJoin("round");
shape.graphics.rect(20,20,150,100);
shape.graphics.stroke();
strokeStyle
-
value
Sets or returns the color, gradient, or pattern used for strokes.
Parameters:
-
value
Stringcolor|gradient|pattern.
Example:
var shape = new LShape();
addChild(shape);
shape.graphics.strokeStyle("#FF0000");
shape.graphics.lineWidth(5);
shape.graphics.lineJoin("round");
shape.graphics.rect(20,20,150,100);
shape.graphics.stroke();
Properties
objectIndex
Int
public
ID of the object