zoukankan      html  css  js  c++  java
  • KineticJS教程(3)

    KineticJS教程(3)

     

     3.图形对象

     

     

    3.1.Shape

     

    Kinetic提供了一个Shape对象用于在层上绘制图形,我们可以通过Kinetic.Shape()构造方法返回一个Shape对象:

     

     

    <script>

     

    var shape = new Kinetic.Shape(config);

     

    </script>

     

     

    Shape方法的config参数是关于具体的绘图参数的数组对象,Kinetic就是根据这个参数里的具体信息进行绘图的。

     

    Config的完整参数如下表所示:

     

    PropertyDescriptionDefaultRequired
    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 miterround, 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 nonehorizontal, or vertical none optional
    dragBounds drag and drop bounds - optional

     

    其中最重要的参数就是drawFunc,这是一个由用户创建的方法对象,Kinetic绘图时就是调用的这个方法。

     

    比如我们可以如下在页面上画一个矩形:

     

     

    <!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>

     

     

    3.2.常用图形

     

    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的文档。

     

    3.3.图形组

     

    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对象后通过相对应的方法设定各属性,比如xy参数就可以分别用group.setX(220)group.setY(20)来设定。

  • 相关阅读:
    TextBox 只有下划线
    can't find web control library(web控件库)
    DropDownListSalesAC”有一个无效 SelectedValue,因为它不在项目列表中。
    IDE、SATA、SCSI、SAS、FC、SSD 硬盘类型
    如何打印1px表格
    CSS控制打印 分页
    Virtual Server could not open its emulated Ethernet switch driver. To fix this problem, reenable the Virtual Server Emulated Et
    Xml中SelectSingleNode方法中的xpath用法
    热带水果莫入冰箱?水果存放冰箱大法
    探索Asp.net的Postback机制
  • 原文地址:https://www.cnblogs.com/zhangleblog/p/3912169.html
Copyright © 2011-2022 走看看