我有几个形状定义存储在文件中,即
Kinetic.Rect({width : 150,height : 50,stroke : 'black',fill :'#00D2FF',strokeWidth : .5,cornerRadius : 25,name : 'rect'});
此行(与其他行一起)可通过数组获得。通常我按如下方式创建这个形状:
rect = new Kinetic.Rect({
width: 150,
height: 50,
stroke: 'black',
fill: fill,
strokeWidth: .5,
cornerRadius: 25,
name: 'rect'
});
我如何从数组/字符串创建此形状?
(rect = new“array from array [xx]”)?
答案 0 :(得分:0)
KineticJS形状(如Rect)是使用包含所需属性的普通javascript对象定义的。
注意:您的填充定义依赖于变量fill
。请确保fill
有效!
// define a rectangle
var rectDefinition1={
width: 150,
height: 50,
stroke: 'black',
strokeWidth: .5,
cornerRadius: 25
};
您可以将此定义提供给这样的函数,从您的定义中创建Kinetic.Rect并将特定的新名称应用于该对象。
// function to create a Kinetic.Rect from the rectangle definition
function makeRectFromObject(myObject,newName,newFill){
var rect=new Kinetic.Rect(myObject);
rect.name(newName);
rect.fill(newFill);
return(rect);
}
你可以使用这样的创作函数:
// Use like this
var rect1=makeRectFromObject(rectDefinition1,"rect1","red");
// add the newly created rect to the layer and redisplay the layer
layer.add(rect1);
layer.draw();
如果要存储和检索对象定义,可以使用JSON:
// serialize your javascript object to a JSON text string
// This is a regular text string that can be stored in a file or database.
var myRectDefinitionAsJSONText = JSON.stringify(rectDefinition1);
// create a javascript object from your JSON text string
var rectDefinition1= JSON.parse( myRectDefinitionAsJSONText );