Kinetic提供了一个Shape对象用于在层上绘制图形,咱们能够经过Kinetic.Shape()构造方法返回一个Shape对象:html
<script>数组
var shape = new Kinetic.Shape(config);post
</script>ui
Shape方法的config参数是关于具体的绘图参数的数组对象,Kinetic就是根据这个参数里的具体信息进行绘图的。this
Config的完整参数以下表所示:spa
Property | Description | Default | Required |
---|---|---|---|
drawFunc | draw function | - | required |
fill | can be a color, linear gradient, radial gradient, or pattern | - | optional |
stroke | stroke color | - | optional |
strokeWidth | stroke width | - | optional |
lineJoin | can be miter, round, or bevel | miter | optional |
shadow | shadow object | - | optional |
detectonType | can be path or pixel | path | optional |
x | x position | 0 | optional |
y | y position | 0 | optional |
visible | whether or not the shape is visible | true | optional |
listening | whether or not to listen to events | true | optional |
id | unique shape id | - | optional |
name | shape name | - | optional |
alpha | shape opacity | 1 | optional |
scale | shape scale | [1,1] | optional |
rotation | rotation about the center point in radians | 0 | optional |
rotationDeg | rotation about the center point in degrees | 0 | optional |
centerOffset | center offset | [0,0] | optional |
draggable | whether or not the shape is draggable | false | optional |
dragConstraint | can be none, horizontal, or vertical | none | optional |
dragBounds | drag and drop bounds | - | optional |
其中最重要的参数就是drawFunc,这是一个由用户建立的方法对象,Kinetic绘图时就是调用的这个方法。.net
好比咱们能够以下在页面上画一个矩形:htm
<!DOCTYPE html>对象
<html>继承
<head>
<meta charset=“UTF-8″>
<title>KineticJS</title>
<script src=“../kinetic.js”></script>
</head>
<body>
<script>
window.onload = function() {
//建立舞台
var stage = new Kinetic.Stage({
container : “container”,
width : 600,
height : 400
});
var layer = new Kinetic.Layer();
//建立config参数
var config = {
//绘图方法,画一个矩形
drawFunc : function() {
var context = this.getContext();
context.rect(200, 150, 200, 100);
this.fill();
this.stroke();
},
//填充色
fill : “green”,
//边缘线颜色
stroke : “black”,
//边缘线宽度
strokeWidth : 4
};
//建立Shape对象
var rectShape = new Kinetic.Shape(config);
//把Shape对象添加到层里
layer.add(rectShape);
//将层添加到舞台中
stage.add(layer);
};
</script>
<div id=“container”></div>
</body>
</html>
Kinetic除了有Shape能够用于绘图外,还为咱们提供了一系列用于常见图形绘制的对象,包括矩形(Rect)、圆(Circle)、图像(Image)、精灵(Sprite)、文本(Text)、线(Line)、多边形(Polygon)、经常使用多边形(Regular Polygon)、路径(Path)、星星(Star)几种。
这几个图形对象都是继承自Shape,因此使用方法与Shape相似,以一个config对象指定绘图细节,与Shape不一样的是,不须要咱们指定drawFunc,只须要根据图形的类型指定关键参数就能够了。
在此,咱们以Shape.Rect为例,绘制矩形图形的代码以下:
<!DOCTYPE html>
<html>
<head>
<meta charset=“UTF-8″>
<title>KineticJS</title>
<script src=“../kinetic.js”></script>
</head>
<body>
<script>
window.onload = function() {
var stage = new Kinetic.Stage({
container : “container”,
width : 600,
height : 400
});
var layer = new Kinetic.Layer();
//建立config参数
var config = {
//左上角x坐标
x : 200,
//左上角y坐标
y : 150,
//矩形宽度
width : 200,
//矩形高度
height : 100,
//填充色
fill : “blue”,
//边缘线颜色
stroke : “black”,
//边缘线宽度
strokeWidth : 4
};
//建立Shape对象
var rect = new Kinetic.Rect(config);
//把Shape对象添加到层里
layer.add(rect);
//将层添加到舞台中
stage.add(layer);
};
</script>
<div id=“container”></div>
</body>
</html>
具体每种图形的config参数细节能够参见Kinetic的文档。
Kinetic提供了Group对象,用于把若干个不一样的图形对象,或者是其余的Group对象组合成一个复杂的图形进行统一管理。
好比,咱们建立一个包含一个矩形和一个圆的group,并添加到层中显示出来。
<!DOCTYPE html>
<html>
<head>
<meta charset=“UTF-8″>
<title>KineticJS</title>
<script src=“../kinetic.js”></script>
</head>
<body>
<script>
window.onload = function() {
var stage = new Kinetic.Stage({
container : “container”,
width : 600,
height : 400
});
var layer = new Kinetic.Layer();
//建立一个要加进组中的圆
var circle = new Kinetic.Circle({
x : 200,
y : 100,
radius : 50,
fill : “red”
});
//建立一个要加进组中的矩形
var rect = new Kinetic.Rect({
x : 300,
y : 200,
width : 100,
height : 100,
fill : “blue”
});
//建立group对象
var group = new Kinetic.Group();
//把多个图形对象添加到group里
group.add(circle);
group.add(rect);
//把group对象添加到层里
layer.add(group);
//将层添加到舞台中
stage.add(layer);
};
</script>
<div id=“container”></div>
</body>
</html>
因为Group继承自Node,而Shape也是继承自Node,所以,Group的一些属性和行为也和Shape比较相似,好比Group的构造方法也能够像接受一个config参数配置Group的位置、旋转、缩放等属性。
如:
var config = {
x : 220,
y : 40,
rotationDeg : 20
};
或者也能够不在建立group时经过config参数设定,而是建立group对象后经过相对应的方法设定各属性,好比x和y参数就能够分别用group.setX(220)和group.setY(20)来设定。