|
108 | 108 | animation.transform = new Transform(new TransformMatrix());
|
109 | 109 | }
|
110 | 110 |
|
| 111 | + if (typeof options.color != "undefined") { |
| 112 | + var colorOptions = mergeOptions(options.color, globalOptions); |
| 113 | + animation.color = new Scene($this, colorOptions, 0xffffff); |
| 114 | + } |
| 115 | + if (typeof options.backgroundColor != "undefined") { |
| 116 | + var bgColorOptions = mergeOptions(options.backgroundColor, globalOptions); |
| 117 | + animation.bgColor = new Scene($this, bgColorOptions, 0xffffff); |
| 118 | + } |
111 | 119 | if (typeof options.opacity != "undefined") {
|
112 | 120 | var opacityOptions = mergeOptions(options.opacity, globalOptions);
|
113 | 121 | animation.opacity = new Scene($this, opacityOptions, 1);
|
|
179 | 187 | this.style['-o-transform'] = transform;
|
180 | 188 | this.style.transform = transform;
|
181 | 189 | }
|
| 190 | + if (animation.color && animation.color.needsUpdate()) { |
| 191 | + var fromColor = RGBColor.fromString(animation.color.getFrom(style.color)), |
| 192 | + toColor = RGBColor.fromString(animation.color.to); |
| 193 | + fromColor.interpolate(toColor, animation.color.getProgress()); |
| 194 | + this.style.color = fromColor.toString(); |
| 195 | + } |
| 196 | + if (animation.bgColor && animation.bgColor.needsUpdate()) { |
| 197 | + var fromColor = RGBColor.fromString(animation.bgColor.getFrom(style.backgroundColor)), |
| 198 | + toColor = RGBColor.fromString(animation.bgColor.to); |
| 199 | + fromColor.interpolate(toColor, animation.bgColor.getProgress()); |
| 200 | + this.style.backgroundColor = fromColor.toString(); |
| 201 | + } |
182 | 202 | if (animation.opacity && animation.opacity.needsUpdate()) {
|
183 | 203 | var newOpacity = animation.opacity.getValue(parseFloat(style.opacity));
|
184 | 204 | this.style.opacity = newOpacity;
|
|
245 | 265 | }
|
246 | 266 | return value;
|
247 | 267 | }
|
| 268 | + |
| 269 | + function interpolate(from, to, progress) { |
| 270 | + return (to - from) * progress + from; |
| 271 | + } |
248 | 272 |
|
249 | 273 | function Scene($el, options, maxValue) {
|
250 | 274 | this.$el = $el;
|
|
306 | 330 | this.state = Scene.STATE_AFTER;
|
307 | 331 | }
|
308 | 332 | },
|
309 |
| - getValue: function(value) { |
310 |
| - typeof this.from != "undefined" || (this.from = value); |
| 333 | + getProgress: function() { |
311 | 334 | if (this.state == Scene.STATE_BEFORE) {
|
312 |
| - return this.from; |
| 335 | + return 0; |
313 | 336 | }
|
314 | 337 | else if (this.state == Scene.STATE_DURING) {
|
315 | 338 | var posPx = scrollTop - this.startPx,
|
316 | 339 | percent = posPx / this.durationPx,
|
317 |
| - aniPos = this.ease.call(this, percent); |
318 |
| - return (this.to - this.from) * aniPos + this.from; |
| 340 | + progress = this.ease.call(this, percent); |
| 341 | + return progress; |
319 | 342 | }
|
320 | 343 | else {
|
321 |
| - return this.to; |
| 344 | + return 1; |
| 345 | + } |
| 346 | + }, |
| 347 | + getFrom: function(defaultValue) { |
| 348 | + typeof this.from != "undefined" || (this.from = defaultValue); |
| 349 | + return this.from; |
| 350 | + }, |
| 351 | + getValue: function(value, progress) { |
| 352 | + this.getFrom(value); |
| 353 | + typeof progress != "undefined" || (progress = this.getProgress()); |
| 354 | + return interpolate(this.from, this.to, progress); |
| 355 | + } |
| 356 | + }; |
| 357 | + |
| 358 | + function RGBColor(r, g, b, a) { |
| 359 | + this.r = r || 0; |
| 360 | + this.g = g || 0; |
| 361 | + this.b = b || 0; |
| 362 | + this.a = typeof a === "number" ? a : 1; |
| 363 | + } |
| 364 | + RGBColor.fromArray = function(array, result) { |
| 365 | + result || (result = new RGBColor()); |
| 366 | + if (array.length < 3) { |
| 367 | + return result; |
| 368 | + } |
| 369 | + result.r = parseInt(array[0]); |
| 370 | + result.g = parseInt(array[1]); |
| 371 | + result.b = parseInt(array[2]); |
| 372 | + if (array.length > 3) { |
| 373 | + result.a = parseFloat(array[3]); |
| 374 | + } |
| 375 | + return result; |
| 376 | + }; |
| 377 | + RGBColor.fromString = function(string, result) { |
| 378 | + if (string.match(/^#([0-9a-f]{3})$/i)) { |
| 379 | + return RGBColor.fromArray([ |
| 380 | + parseInt(string.charAt(1),16)*0x11, |
| 381 | + parseInt(string.charAt(2),16)*0x11, |
| 382 | + parseInt(string.charAt(3),16)*0x11 |
| 383 | + ], result); |
| 384 | + } |
| 385 | + if (string.match(/^#([0-9a-f]{6})$/i)) { |
| 386 | + return RGBColor.fromArray([ |
| 387 | + parseInt(string.substr(1,2),16), |
| 388 | + parseInt(string.substr(3,2),16), |
| 389 | + parseInt(string.substr(5,2),16) |
| 390 | + ], result); |
| 391 | + } |
| 392 | + return RGBColor.fromArray(string.replace(/^rgb(a)?\((.*)\)$/, '$2').split(/, /), result); |
| 393 | + }; |
| 394 | + RGBColor.fromHSV = function(hsv, result) { |
| 395 | + result || (result = new RGBColor()); |
| 396 | + var r = hsv.v, |
| 397 | + g = hsv.v, |
| 398 | + b = hsv.v; |
| 399 | + if (hsv.s != 0) { |
| 400 | + var f = hsv.h / 60 - Math.floor(hsv.h / 60); |
| 401 | + var p = hsv.v * (1 - hsv.s / 100); |
| 402 | + var q = hsv.v * (1 - hsv.s / 100 * f); |
| 403 | + var t = hsv.v * (1 - hsv.s / 100 * (1 - f)); |
| 404 | + switch (Math.floor(hsv.h / 60)){ |
| 405 | + case 0: r = hsv.v; g = t; b = p; break; |
| 406 | + case 1: r = q; g = hsv.v; b = p; break; |
| 407 | + case 2: r = p; g = hsv.v; b = t; break; |
| 408 | + case 3: r = p; g = q; b = hsv.v; break; |
| 409 | + case 4: r = t; g = p; b = hsv.v; break; |
| 410 | + case 5: r = hsv.v; g = p; b = q; break; |
| 411 | + } |
| 412 | + } |
| 413 | + result.r = r * 2.55; |
| 414 | + result.g = g * 2.55; |
| 415 | + result.b = b * 2.55; |
| 416 | + result.a = hsv.a; |
| 417 | + return result; |
| 418 | + }; |
| 419 | + RGBColor.prototype = { |
| 420 | + getHue: function(maximum, range) { |
| 421 | + var hue = 0; |
| 422 | + if (range != 0) { |
| 423 | + switch (maximum){ |
| 424 | + case this.r: |
| 425 | + hue = (this.g - this.b) / range * 60; |
| 426 | + if (hue < 0) hue += 360; |
| 427 | + break; |
| 428 | + case this.g: |
| 429 | + hue = (this.b - this.r) / range * 60 + 120; |
| 430 | + break; |
| 431 | + case this.b: |
| 432 | + hue = (this.r - this.g) / range * 60 + 240; |
| 433 | + break; |
| 434 | + } |
| 435 | + } |
| 436 | + return hue; |
| 437 | + |
| 438 | + }, |
| 439 | + interpolate: function(to, progress) { |
| 440 | + var src = HSVColor.fromRGB(this), |
| 441 | + dst = HSVColor.fromRGB(to); |
| 442 | + src.interpolate(dst, progress); |
| 443 | + RGBColor.fromHSV(src, this); |
| 444 | + }, |
| 445 | + toString: function() { |
| 446 | + if (this.a !== 1) { |
| 447 | + return "rgba("+this.r.toFixed()+","+this.g.toFixed()+","+this.b.toFixed()+","+this.a.toFixed(2)+")"; |
| 448 | + } |
| 449 | + return "rgb("+this.r.toFixed()+","+this.g.toFixed()+","+this.b.toFixed()+")"; |
| 450 | + } |
| 451 | + }; |
| 452 | + |
| 453 | + function HSVColor(h, s, v, a) { |
| 454 | + this.h = h || 0; |
| 455 | + this.s = s || 0; |
| 456 | + this.v = v || 0; |
| 457 | + this.a = typeof a === "number" ? a : 1; |
| 458 | + } |
| 459 | + HSVColor.fromRGB = function(rgb, result) { |
| 460 | + result || (result = new HSVColor()); |
| 461 | + var maximum = Math.max(rgb.r, rgb.g, rgb.b); |
| 462 | + var range = maximum - Math.min(rgb.r, rgb.g, rgb.b); |
| 463 | + result.h = rgb.getHue(maximum, range); |
| 464 | + result.s = (maximum == 0 ? 0 : 100 * range / maximum); |
| 465 | + result.v = maximum / 2.55; |
| 466 | + result.a = rgb.a; |
| 467 | + return result; |
| 468 | + }; |
| 469 | + HSVColor.prototype = { |
| 470 | + interpolate: function(to, progress, precision) { |
| 471 | + this.h = interpolate(this.h, to.h, progress); |
| 472 | + this.s = interpolate(this.s, to.s, progress); |
| 473 | + this.v = interpolate(this.v, to.v, progress); |
| 474 | + this.a = interpolate(this.a, to.a, progress); |
| 475 | + }, |
| 476 | + toString: function() { |
| 477 | + if (this.a !== 1) { |
| 478 | + return "hsva("+this.h+","+this.s+","+this.v+","+this.a.toFixed(2)+")"; |
322 | 479 | }
|
| 480 | + return "hsv("+this.h+","+this.s+","+this.v+")"; |
323 | 481 | }
|
324 | 482 | };
|
325 | 483 |
|
|
0 commit comments