-
-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathlinear-gradient.js
232 lines (202 loc) · 6.19 KB
/
linear-gradient.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { Events } from '../events.js';
import { _ } from '../utils/underscore.js';
import { Stop } from './stop.js';
import { Gradient } from './gradient.js';
import { Vector } from '../vector.js';
/**
* @name Two.LinearGradient
* @class
* @extends Two.Gradient
* @param {Number} [x1=0] - The x position of the first end point of the linear gradient.
* @param {Number} [y1=0] - The y position of the first end point of the linear gradient.
* @param {Number} [x2=0] - The x position of the second end point of the linear gradient.
* @param {Number} [y2=0] - The y position of the second end point of the linear gradient.
* @param {Two.Stop[]} [stops] - A list of {@link Two.Stop}s that contain the gradient fill pattern for the gradient.
* @nota-bene The linear gradient lives within the space of the parent object's matrix space.
*/
export class LinearGradient extends Gradient {
/**
* @name Two.LinearGradient#_flagEndPoints
* @private
* @property {Boolean} - Determines whether the {@link Two.LinearGradient#left} or {@link Two.LinearGradient#right} changed and needs to update.
*/
_flagEndPoints = false;
_left = null;
_right = null;
constructor(x1, y1, x2, y2, stops) {
super(stops);
for (let prop in proto) {
Object.defineProperty(this, prop, proto[prop]);
}
this._renderer.type = 'linear-gradient';
this._renderer.flagEndPoints = FlagEndPoints.bind(this);
/**
* @name Two.LinearGradient#left
* @property {Two.Vector} - The x and y value for where the first end point is placed on the canvas.
*/
this.left = new Vector();
/**
* @name Two.LinearGradient#right
* @property {Two.Vector} - The x and y value for where the second end point is placed on the canvas.
*/
this.right = new Vector();
if (typeof x1 === 'number') {
this.left.x = x1;
}
if (typeof y1 === 'number') {
this.left.y = y1;
}
if (typeof x2 === 'number') {
this.right.x = x2;
}
if (typeof y2 === 'number') {
this.right.y = y2;
}
}
/**
* @name Two.LinearGradient.Stop
* @see {@link Two.Stop}
*/
static Stop = Stop;
static Properties = ['left', 'right'];
/**
* @name Two.LinearGradient.fromObject
* @function
* @param {Object} obj - Object notation of a {@link Two.LinearGradient} to create a new instance
* @returns {Two.LinearGradient}
* @description Create a new {@link Two.LinearGradient} from an object notation of a {@link Two.LinearGradient}.
* @nota-bene Works in conjunction with {@link Two.LinearGradient#toObject}
*/
static fromObject(obj) {
const gradient = new LinearGradient().copy(obj);
if ('id' in obj) {
gradient.id = obj.id;
}
return gradient;
}
/**
* @name Two.LinearGradient#copy
* @function
* @param {Two.LinearGradient} gradient - The reference {@link Two.LinearGradient}
* @description Copy the properties of one {@link Two.LinearGradient} onto another.
*/
copy(gradient) {
super.copy.call(this, gradient);
for (let i = 0; i < LinearGradient.Properties.length; i++) {
const k = LinearGradient.Properties[i];
if (k in gradient) {
this[k] =
gradient[k] instanceof Vector
? gradient[k]
: new Vector().copy(gradient[k]);
}
}
return this;
}
/**
* @name Two.LinearGradient#clone
* @function
* @param {Two.Group} [parent] - The parent group or scene to add the clone to.
* @returns {Two.Gradient}
* @description Create a new instance of {@link Two.LinearGradient} with the same properties of the current path.
*/
clone(parent) {
const stops = this.stops.map(function (stop) {
return stop.clone();
});
const clone = new LinearGradient(
this.left._x,
this.left._y,
this.right._x,
this.right._y,
stops
);
_.each(
Gradient.Properties,
function (k) {
clone[k] = this[k];
},
this
);
if (parent) {
parent.add(clone);
}
return clone;
}
/**
* @name Two.LinearGradient#toObject
* @function
* @returns {Object}
* @description Return a JSON compatible plain object that represents the path.
*/
toObject() {
const result = super.toObject.call(this);
result.left = this.left.toObject();
result.right = this.right.toObject();
return result;
}
/**
* @name Two.LinearGradient#_update
* @function
* @private
* @param {Boolean} [bubbles=false] - Force the parent to `_update` as well.
* @description This is called before rendering happens by the renderer. This applies all changes necessary so that rendering is up-to-date but not updated more than it needs to be.
* @nota-bene Try not to call this method more than once a frame.
*/
_update() {
if (this._flagEndPoints || this._flagSpread || this._flagStops) {
this.trigger(Events.Types.change);
}
return this;
}
/**
* @name Two.LinearGradient#flagReset
* @function
* @private
* @description Called internally to reset all flags. Ensures that only properties that change are updated before being sent to the renderer.
*/
flagReset() {
this._flagEndPoints = false;
super.flagReset.call(this);
return this;
}
}
const proto = {
left: {
enumerable: true,
get: function () {
return this._left;
},
set: function (v) {
if (this._left instanceof Vector) {
this._left.unbind(Events.Types.change, this._renderer.flagEndPoints);
}
this._left = v;
this._left.bind(Events.Types.change, this._renderer.flagEndPoints);
this._flagEndPoints = true;
},
},
right: {
enumerable: true,
get: function () {
return this._right;
},
set: function (v) {
if (this._right instanceof Vector) {
this._right.unbind(Events.Types.change, this._renderer.flagEndPoints);
}
this._right = v;
this._right.bind(Events.Types.change, this._renderer.flagEndPoints);
this._flagEndPoints = true;
},
},
};
/**
* @name FlagEndPoints
* @private
* @function
* @description Cached method to let renderers know end points have been updated on a {@link Two.LinearGradient}.
*/
function FlagEndPoints() {
this._flagEndPoints = true;
}