zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
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
26
27
28
29
30
31
32
33
34
35
36
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
71
72
73
74
75
76
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var fixShadow_1 = require("./helper/fixShadow");
var constant_1 = require("./constant");
var STYLE_COMMON_PROPS = [
    ['shadowBlur', 0], ['shadowOffsetX', 0], ['shadowOffsetY', 0], ['shadowColor', '#000'],
    ['lineCap', 'butt'], ['lineJoin', 'miter'], ['miterLimit', 10]
];
function createLinearGradient(ctx, obj, rect) {
    var x = obj.x == null ? 0 : obj.x;
    var x2 = obj.x2 == null ? 1 : obj.x2;
    var y = obj.y == null ? 0 : obj.y;
    var y2 = obj.y2 == null ? 0 : obj.y2;
    if (!obj.global) {
        x = x * rect.width + rect.x;
        x2 = x2 * rect.width + rect.x;
        y = y * rect.height + rect.y;
        y2 = y2 * rect.height + rect.y;
    }
    x = isNaN(x) ? 0 : x;
    x2 = isNaN(x2) ? 1 : x2;
    y = isNaN(y) ? 0 : y;
    y2 = isNaN(y2) ? 0 : y2;
    var canvasGradient = ctx.createLinearGradient(x, y, x2, y2);
    return canvasGradient;
}
function createRadialGradient(ctx, obj, rect) {
    var width = rect.width;
    var height = rect.height;
    var min = Math.min(width, height);
    var x = obj.x == null ? 0.5 : obj.x;
    var y = obj.y == null ? 0.5 : obj.y;
    var r = obj.r == null ? 0.5 : obj.r;
    if (!obj.global) {
        x = x * width + rect.x;
        y = y * height + rect.y;
        r = r * min;
    }
    var canvasGradient = ctx.createRadialGradient(x, y, 0, x, y, r);
    return canvasGradient;
}
var TextStyleOption = (function () {
    function TextStyleOption() {
    }
    return TextStyleOption;
}());
exports.TextStyleOption = TextStyleOption;
var StyleOption = (function (_super) {
    tslib_1.__extends(StyleOption, _super);
    function StyleOption() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return StyleOption;
}(TextStyleOption));
exports.StyleOption = StyleOption;
var Style = (function (_super) {
    tslib_1.__extends(Style, _super);
    function Style(opts) {
        var _this = _super.call(this) || this;
        if (opts) {
            _this.extendFrom(opts, true);
        }
        return _this;
    }
    Style.prototype.bind = function (ctx, el, prevEl) {
        var style = this;
        var prevStyle = prevEl && prevEl.style;
        var notCheckCache = !prevStyle || ctx.__attrCachedBy !== constant_1.ContextCachedBy.STYLE_BIND;
        ctx.__attrCachedBy = constant_1.ContextCachedBy.STYLE_BIND;
        for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
            var prop = STYLE_COMMON_PROPS[i];
            var styleName = prop[0];
            if (notCheckCache || style[styleName] !== prevStyle[styleName]) {
                ctx[styleName] =
                    fixShadow_1["default"](ctx, styleName, (style[styleName] || prop[1]));
            }
        }
        if ((notCheckCache || style.fill !== prevStyle.fill)) {
            ctx.fillStyle = style.fill;
        }
        if ((notCheckCache || style.stroke !== prevStyle.stroke)) {
            ctx.strokeStyle = style.stroke;
        }
        if ((notCheckCache || style.opacity !== prevStyle.opacity)) {
            ctx.globalAlpha = style.opacity == null ? 1 : style.opacity;
        }
        if ((notCheckCache || style.blend !== prevStyle.blend)) {
            ctx.globalCompositeOperation = style.blend || 'source-over';
        }
        if (this.hasStroke()) {
            var lineWidth = style.lineWidth;
            ctx.lineWidth = lineWidth / ((this.strokeNoScale && el && el.getLineScale) ? el.getLineScale() : 1);
        }
    };
    Style.prototype.hasFill = function () {
        var fill = this.fill;
        return fill != null && fill !== 'none';
    };
    Style.prototype.hasStroke = function () {
        var stroke = this.stroke;
        return stroke != null && stroke !== 'none' && this.lineWidth > 0;
    };
    Style.prototype.extendFrom = function (otherStyle, overwrite) {
        if (otherStyle) {
            for (var name_1 in otherStyle) {
                if (otherStyle.hasOwnProperty(name_1)
                    && (overwrite === true
                        || (overwrite === false
                            ? !this.hasOwnProperty(name_1)
                            : otherStyle[name_1] != null))) {
                    this[name_1] = otherStyle[name_1];
                }
            }
        }
    };
    Style.prototype.set = function (obj, value) {
        if (typeof obj === 'string') {
            this[obj] = value;
        }
        else {
            this.extendFrom(obj, true);
        }
    };
    Style.prototype.clone = function () {
        var newStyle = new Style();
        newStyle.extendFrom(this, true);
        return newStyle;
    };
    Style.getGradient = function (ctx, obj, rect) {
        var canvasGradient = obj.type === 'radial'
            ? createRadialGradient(ctx, obj, rect)
            : createLinearGradient(ctx, obj, rect);
        var colorStops = obj.colorStops;
        for (var i = 0; i < colorStops.length; i++) {
            canvasGradient.addColorStop(colorStops[i].offset, colorStops[i].color);
        }
        return canvasGradient;
    };
    Style.initDefaultProps = (function () {
        var styleProto = Style.prototype;
        styleProto.fill = '#000';
        styleProto.stroke = null;
        styleProto.opacity = 1;
        styleProto.lineDashOffset = 0;
        styleProto.shadowBlur = 0;
        styleProto.shadowOffsetX = 0;
        styleProto.shadowOffsetY = 0;
        styleProto.shadowColor = '#000';
        styleProto.lineWidth = 1;
        styleProto.lineCap = 'butt';
        styleProto.miterLimit = 10;
        styleProto.strokeNoScale = false;
        styleProto.textStrokeWidth = 0;
        styleProto.textPosition = 'inside';
        styleProto.textDistance = 5;
        styleProto.textShadowColor = 'transparent';
        styleProto.textShadowBlur = 0;
        styleProto.textShadowOffsetX = 0;
        styleProto.textShadowOffsetY = 0;
        styleProto.textBoxShadowColor = 'transparent';
        styleProto.textBoxShadowBlur = 0;
        styleProto.textBoxShadowOffsetX = 0;
        styleProto.textBoxShadowOffsetY = 0;
        styleProto.textRotation = 0;
        styleProto.textBorderWidth = 0;
        styleProto.textBorderRadius = 0;
    })();
    return Style;
}(StyleOption));
exports["default"] = Style;
;
//# sourceMappingURL=Style.js.map