symbol.js 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Symbol factory
define(function(require) {

    'use strict';

    var graphic = require('./graphic');
    var BoundingRect = require('zrender/core/BoundingRect');

    /**
     * Diamond shape
     */
    var Diamond = graphic.extendShape({
        type: 'diamond',
        shape: {
            cx: 0,
            cy: 0,
            width: 0,
            height: 0
        },
        buildPath: function (path, shape) {
            path.moveTo(shape.cx, shape.cy - shape.h)
        }
    });

    var symbols = {
L
lang 已提交
26 27 28 29 30 31 32 33 34 35 36
        line: function (x, y, w, h) {
            return new graphic.Line({
                shape: {
                    x1: x,
                    y1: y + h / 2,
                    x2: x + w,
                    y2: y + h / 2
                }
            });
        },

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        rect: function (x, y, w, h) {
            return new graphic.Rect({
                shape: {
                    x: x,
                    y: y,
                    width: w,
                    height: h
                }
            })
        },

        roundRect: function (x, y, w, h, r) {
            return new graphic.Rect({
                shape: {
                    x: x,
                    y: y,
                    width: w,
                    height: h,
                    r: r || Math.min(w, h) / 4
                }
            });
        },

        square: function (x, y, size) {
            return new graphic.Rect({
                shape: {
                    x: x,
                    y: y,
                    width: size / 2,
                    height: size / 2
                }
            });
        },

L
lang 已提交
71
        circle: function (x, y, size) {
72 73
            return new graphic.Circle({
                shape: {
L
lang 已提交
74 75 76
                    cx: x + size / 2,
                    cy: y + size / 2,
                    r: size / 2
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                }
            });
        },

        diamond: function (x, y, w, h) {
            return new Diamond({
                shape: {
                    cx: x + w,
                    cy: y + h,
                    width: w,
                    height: h
                }
            })
        },

        image: function (img, x, y, w, h) {
            return new graphic.Image({
                style: {
                    image: img,
                    x: x,
                    y: y,
                    width: w,
                    height: h
                }
            })
        },

        path: function (pathStr, x, y, w, h) {
            return graphic.makePath(pathStr, null, new BoundingRect(x, y, w, h));
        }
    };

L
lang 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    return {
        create: function (symbolType, x, y, w, h) {
            if (symbolType.indexOf('image://') === 0) {
                return symbols.image(symbolType.slice(8), x, y, w, h);
            }
            else if (symbolType.indexOf('path://') === 0) {
                return symbols.image(symbolType.slice(7), x, y, w, h);
            }
            else {
                if (symbols[symbolType]) {
                    return symbols[symbolType](x, y, w, h);
                }
                else {
                    return symbols.rect(x, y, w, h);
                }
            }
        }
    };
127
});