123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- var fixShadow = require("./helper/fixShadow");
- var _constant = require("./constant");
- var ContextCachedBy = _constant.ContextCachedBy;
- var STYLE_COMMON_PROPS = [['shadowBlur', 0], ['shadowOffsetX', 0], ['shadowOffsetY', 0], ['shadowColor', '#000'], ['lineCap', 'butt'], ['lineJoin', 'miter'], ['miterLimit', 10]];
- var Style = function (opts) {
- this.extendFrom(opts, false);
- };
- 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;
- }
- Style.prototype = {
- constructor: Style,
-
- fill: '#000',
-
- stroke: null,
-
- opacity: 1,
-
- fillOpacity: null,
-
- strokeOpacity: null,
-
- lineDash: null,
-
- lineDashOffset: 0,
-
- shadowBlur: 0,
-
- shadowOffsetX: 0,
-
- shadowOffsetY: 0,
-
- lineWidth: 1,
-
- strokeNoScale: false,
-
-
-
- text: null,
-
- font: null,
-
- textFont: null,
-
- fontStyle: null,
-
- fontWeight: null,
-
- fontSize: null,
-
- fontFamily: null,
-
- textTag: null,
-
- textFill: '#000',
-
- textStroke: null,
-
- textWidth: null,
-
- textHeight: null,
-
- textStrokeWidth: 0,
-
- textLineHeight: null,
-
- textPosition: 'inside',
-
- textRect: null,
-
- textOffset: null,
-
- textAlign: null,
-
- textVerticalAlign: null,
-
- textDistance: 5,
-
- textShadowColor: 'transparent',
-
- textShadowBlur: 0,
-
- textShadowOffsetX: 0,
-
- textShadowOffsetY: 0,
-
- textBoxShadowColor: 'transparent',
-
- textBoxShadowBlur: 0,
-
- textBoxShadowOffsetX: 0,
-
- textBoxShadowOffsetY: 0,
-
- transformText: false,
-
- textRotation: 0,
-
- textOrigin: null,
-
- textBackgroundColor: null,
-
- textBorderColor: null,
-
- textBorderWidth: 0,
-
- textBorderRadius: 0,
-
- textPadding: null,
-
- rich: null,
-
- truncate: null,
-
- blend: null,
-
- bind: function (ctx, el, prevEl) {
- var style = this;
- var prevStyle = prevEl && prevEl.style;
-
- var notCheckCache = !prevStyle || ctx.__attrCachedBy !== ContextCachedBy.STYLE_BIND;
- ctx.__attrCachedBy = 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(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);
- }
- },
- hasFill: function () {
- var fill = this.fill;
- return fill != null && fill !== 'none';
- },
- hasStroke: function () {
- var stroke = this.stroke;
- return stroke != null && stroke !== 'none' && this.lineWidth > 0;
- },
-
- extendFrom: function (otherStyle, overwrite) {
- if (otherStyle) {
- for (var name in otherStyle) {
- if (otherStyle.hasOwnProperty(name) && (overwrite === true || (overwrite === false ? !this.hasOwnProperty(name) : otherStyle[name] != null))) {
- this[name] = otherStyle[name];
- }
- }
- }
- },
-
- set: function (obj, value) {
- if (typeof obj === 'string') {
- this[obj] = value;
- } else {
- this.extendFrom(obj, true);
- }
- },
-
- clone: function () {
- var newStyle = new this.constructor();
- newStyle.extendFrom(this, true);
- return newStyle;
- },
- getGradient: function (ctx, obj, rect) {
- var method = obj.type === 'radial' ? createRadialGradient : createLinearGradient;
- var canvasGradient = method(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;
- }
- };
- var styleProto = Style.prototype;
- for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
- var prop = STYLE_COMMON_PROPS[i];
- if (!(prop[0] in styleProto)) {
- styleProto[prop[0]] = prop[1];
- }
- }
- Style.getGradient = styleProto.getGradient;
- var _default = Style;
- module.exports = _default;
|