| 1 |
|
| 2 |
if (typeof(Kumu) == 'undefined') { |
| 3 |
Kumu = {} |
| 4 |
}else{ |
| 5 |
Kumu.dynamicLoad('dhtml'); |
| 6 |
} |
| 7 |
|
| 8 |
if (typeof(Kumu.Effect) == 'undefined') { |
| 9 |
Kumu.Effect = {}; |
| 10 |
} |
| 11 |
|
| 12 |
Kumu.Effect.makeEaseIn = function(a){ |
| 13 |
return function(state){ |
| 14 |
return Math.pow(state, a * 2); |
| 15 |
} |
| 16 |
}; |
| 17 |
|
| 18 |
Kumu.Effect.makeEaseOut = function(a){ |
| 19 |
return function(state){ |
| 20 |
return 1 - Math.pow(1 - state, a * 2); |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
Kumu.Effect.lfos = { |
| 25 |
pulse: function (pos) { |
| 26 |
return Kumu.Effect.lfos.easeInOut(1 - Math.abs(0.5-pos) * 2); |
| 27 |
}, |
| 28 |
linear : function(x){return x;}, |
| 29 |
easeIn : Kumu.Effect.makeEaseIn(1.5), |
| 30 |
easeOut : Kumu.Effect.makeEaseOut(1.5), |
| 31 |
makeEaseIn : Kumu.Effect.makeEaseIn, |
| 32 |
makeEaseOut : Kumu.Effect.makeEaseOut, |
| 33 |
cubicIn: function(t){ |
| 34 |
return t*t*t; |
| 35 |
}, |
| 36 |
cubicOut: function(t){ |
| 37 |
return ((t-1)*t*t + 1); |
| 38 |
}, |
| 39 |
cubicInOut: function(t){ |
| 40 |
if ((t*=2) < 1) return 0.5*t*t*t; |
| 41 |
return 0.5*((t-=2)*t*t + 2); |
| 42 |
}, |
| 43 |
sineIn: function(t){ |
| 44 |
return - Math.cos(t/ (Math.PI/2)) + 1; |
| 45 |
}, |
| 46 |
sineOut: function(t){ |
| 47 |
return Math.sin(t/ (Math.PI/2)); |
| 48 |
}, |
| 49 |
sineInOut: function(t){ |
| 50 |
return -0.5 * (Math.cos(Math.PI*t) - 1); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
Kumu.Effect.apply = function(el, style, options){ |
| 55 |
return Kumu.Effect.cssAnimation(style, options, el); |
| 56 |
} |
| 57 |
|
| 58 |
Kumu.Effect._getDefaultValue = function(element, property, defaultValue){ |
| 59 |
if(defaultValue){ |
| 60 |
return defaultValue; |
| 61 |
} |
| 62 |
if(property == 'opacity'){ |
| 63 |
return Kumu.DHTML.getOpacity(element); |
| 64 |
}else{ |
| 65 |
return Kumu.DHTML.getStyle(element, property) || 0; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
Kumu.Effect._parseStyleArguments = function(elements, property, fromDefaultValue, toDefaultValue){ |
| 71 |
var self = Kumu.Effect; |
| 72 |
var arg; |
| 73 |
elements = Kumu.toArray(elements); |
| 74 |
return elements.map(function(node){ |
| 75 |
var from = self._getDefaultValue(node, property, fromDefaultValue); |
| 76 |
var to = self._getDefaultValue(node, property, toDefaultValue); |
| 77 |
return [from, to]; |
| 78 |
}); |
| 79 |
}; |
| 80 |
|
| 81 |
Kumu.Effect.Effector = Kumu.create(); |
| 82 |
|
| 83 |
Kumu.extend(Kumu.Effect.Effector.prototype, { |
| 84 |
|
| 85 |
options : { |
| 86 |
interval : 30, |
| 87 |
duration : 400, |
| 88 |
onComplete : function(){}, |
| 89 |
onForward : function(){}, |
| 90 |
onBackward : function(){}, |
| 91 |
onStep : function(){}, |
| 92 |
mod : Kumu.Effect.lfos.cubicInOut |
| 93 |
}, |
| 94 |
|
| 95 |
initialize : function(options){ |
| 96 |
Kumu.extend(this.options, options); |
| 97 |
this.target = 0; |
| 98 |
this.state = 0; |
| 99 |
this.play = this._play.bindScope(this); |
| 100 |
this.effects = []; |
| 101 |
this._chainForward = null; |
| 102 |
this._chainBackward = null; |
| 103 |
|
| 104 |
}, |
| 105 |
|
| 106 |
seekTo : function(to){ |
| 107 |
this.seekFromTo(this.state, to); |
| 108 |
}, |
| 109 |
|
| 110 |
seekFromTo : function(from, to){ |
| 111 |
this.target = Math.max(0, Math.min(1, to)); |
| 112 |
this.state = Math.max(0, Math.min(1, from)); |
| 113 |
if(this.target != 1 && this.state == 1 && this._chainForward && this._propagated){ |
| 114 |
return this._chainForward(this._chainProtocol.PROPAGATE); |
| 115 |
}else if(this.target != 0 && this.state == 0 && this._chainBackward && this._rev_propagated){ |
| 116 |
return this._chainBackward(this._chainProtocol.PROPAGATE); |
| 117 |
} |
| 118 |
if(!this.intervalId){ |
| 119 |
this.intervalId = window.setInterval(this.play, this.options.interval); |
| 120 |
} |
| 121 |
}, |
| 122 |
|
| 123 |
jumpTo : function(to){ |
| 124 |
this.target = this.state = Math.max(0, Math.min(1,to)); |
| 125 |
this._play(); |
| 126 |
}, |
| 127 |
|
| 128 |
play : function(){ |
| 129 |
this.seekFromTo(0, 1); |
| 130 |
}, |
| 131 |
|
| 132 |
reverse : function(){ |
| 133 |
this.seekFromTo(0, 0); |
| 134 |
}, |
| 135 |
|
| 136 |
toggle : function(){ |
| 137 |
this.seekTo(1 - this.target); |
| 138 |
}, |
| 139 |
|
| 140 |
_chainProtocol : { |
| 141 |
PROPAGATE : -1, |
| 142 |
DISCONNECT : 0, |
| 143 |
PLAY : 1 |
| 144 |
}, |
| 145 |
/* |
| 146 |
chain : function(animation){ |
| 147 |
var self = this; |
| 148 |
var cp = this._chainProtocol; |
| 149 |
|
| 150 |
animation._rev_propagated = true; |
| 151 |
self._propagated = true; |
| 152 |
if(this._chainForward){ |
| 153 |
this._chainForward(cp.DISCONNECT); |
| 154 |
} |
| 155 |
self._chainForward = function(signal){ |
| 156 |
if(signal == cp.PROPAGATE){ |
| 157 |
if(animation._chainForward && animation._propagated){ |
| 158 |
animation.target = 0; |
| 159 |
animation._chainForward(cp.PROPAGATE); |
| 160 |
}else{ |
| 161 |
animation.seekTo(0); |
| 162 |
} |
| 163 |
}else if(signal == cp.DISCONNECT){ |
| 164 |
animation._chainBackward = null; |
| 165 |
}else if(signal == cp.PLAY){ |
| 166 |
self._propagated = true; |
| 167 |
animation._rev_propagated = true; |
| 168 |
animation.seekFromTo(animation.target == 0 ? 1 : animation.target); |
| 169 |
} |
| 170 |
} |
| 171 |
animation._chainBackward = function(signal){ |
| 172 |
if(sginal == cp.PROPAGATE){ |
| 173 |
if(self._chainBackward & self._rev_propagated){ |
| 174 |
self.target = 1; |
| 175 |
self._chainBackward(cp.PROPAGATE); |
| 176 |
}else{ |
| 177 |
self.seekTo(1); |
| 178 |
} |
| 179 |
}else if(signal == cp.DISCONNECT){ |
| 180 |
throw new Error(""); |
| 181 |
}else{ |
| 182 |
animation._rev_propagated = true; |
| 183 |
self._propagated = false; |
| 184 |
self.seekTo(self.target == 1 ? 0 : self.target); |
| 185 |
} |
| 186 |
} |
| 187 |
return this; |
| 188 |
}, |
| 189 |
*/ |
| 190 |
|
| 191 |
addEffect : function(effect){ |
| 192 |
this.effects[this.effects.length] = effect; |
| 193 |
return this; |
| 194 |
}, |
| 195 |
|
| 196 |
removeEffect : function(effect){ |
| 197 |
this.effects = this.effects.reject(function(item){return item == effect;}); |
| 198 |
}, |
| 199 |
|
| 200 |
clearEffect : function(){ |
| 201 |
this.effects = []; |
| 202 |
}, |
| 203 |
|
| 204 |
_propagateEffect : function(){ |
| 205 |
var unclampedValue = this.options.mod(this.state); |
| 206 |
var value = Math.max(0, Math.min(1, unclampedValue)); |
| 207 |
for(var i = 0; i < this.effects.length; i++){ |
| 208 |
var effect = this.effects[i] |
| 209 |
if(effect.setState){ |
| 210 |
effect.setState(value, unclampedValue, this.state); |
| 211 |
}else{ |
| 212 |
effect(value, unclampedValue, this.state); |
| 213 |
} |
| 214 |
} |
| 215 |
}, |
| 216 |
|
| 217 |
_play : function(){ |
| 218 |
var movement = (this.options.interval / this.options.duration) * (this.state < this.target ? 1 : -1); |
| 219 |
if(Math.abs(movement) >= Math.abs(this.state - this.target)){ |
| 220 |
this.state = this.target; |
| 221 |
}else{ |
| 222 |
this.state += movement; |
| 223 |
} |
| 224 |
this._propagateEffect(); |
| 225 |
this.options.onStep.call(this); |
| 226 |
if(this.target == this.state){ |
| 227 |
window.clearInterval(this.intervalId); |
| 228 |
this.intervalId = null; |
| 229 |
if(this.state == 1 && movement > 0){ |
| 230 |
this.options.onForward.call(this, this); |
| 231 |
if(this._chainForward){ |
| 232 |
this._chainForward(this._chainProtocol.PLAY); |
| 233 |
} |
| 234 |
}else if(this.state == 0 && movement < 0){ |
| 235 |
this.options.onForward.call(this,this); |
| 236 |
if(this._chainBackward){ |
| 237 |
this._chainBackward(this._chainProtocol.PLAY); |
| 238 |
} |
| 239 |
} |
| 240 |
this.options.onComplete.call(this, this); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
}); |
| 245 |
|
| 246 |
|
| 247 |
Kumu.Effect.CSSEffect = Kumu.create(); |
| 248 |
|
| 249 |
Kumu.extend(Kumu.Effect.CSSEffect.prototype, { |
| 250 |
|
| 251 |
initialize : function(elements, style1, style2){ |
| 252 |
var self = Kumu.Effect; |
| 253 |
var fromStyle; |
| 254 |
var toStyle; |
| 255 |
elements = Kumu.toArray($i(elements)); |
| 256 |
if(style2){ |
| 257 |
fromStyle = this.parseStyle(style1); |
| 258 |
toStyle = this.parseStyle(style2); |
| 259 |
}else{ |
| 260 |
toStyle = this.parseStyle(style1); |
| 261 |
var fromStyle = {}; |
| 262 |
Kumu.keys(toStyle).map(function(key){ |
| 263 |
fromStyle[key] = null; |
| 264 |
}); |
| 265 |
} |
| 266 |
this.effects = Kumu.keys(toStyle).map(function(property){ |
| 267 |
return self._createStyleEffect(elements, property, fromStyle[property], toStyle[property]); |
| 268 |
}); |
| 269 |
|
| 270 |
}, |
| 271 |
|
| 272 |
parseStyle : function(style){ |
| 273 |
var styles = style.split(';'); |
| 274 |
var re = /^\s*([a-zA-Z\-_0-9]+)\s*:\s*(\S(.+\S)?)\s*$/; |
| 275 |
var obj = {}; |
| 276 |
for(var i = 0; i < styles.length; i++){ |
| 277 |
var parts = re.exec(styles[i]); |
| 278 |
if(parts){ |
| 279 |
obj[parts[1]] = parts[2]; |
| 280 |
} |
| 281 |
} |
| 282 |
return obj; |
| 283 |
}, |
| 284 |
|
| 285 |
setState : function(state, unclampedState, rawState) { |
| 286 |
if(this.transition){ |
| 287 |
unclampedState = this.transition(rawState); |
| 288 |
state = Math.max(0, Math.min(1, unclampedState)); |
| 289 |
} |
| 290 |
for(var i = 0; i < this.effects.length; i++){ |
| 291 |
this.effects[i].setState(state, unclampedState, rawState); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
}); |
| 296 |
|
| 297 |
Kumu.Effect.PropertyEffect = Kumu.create(); |
| 298 |
|
| 299 |
Kumu.extend(Kumu.Effect.PropertyEffect.prototype, { |
| 300 |
|
| 301 |
initialize : function(elements, propertyObj){ |
| 302 |
var self = Kumu.Effect; |
| 303 |
elements = Kumu.toArray($i(elements)); |
| 304 |
this.effects = Kumu.keys(propertyObj).map(function(property){ |
| 305 |
return self._createStyleEffect(elements, property, propertyObj[property][0], propertyObj[property][1], |
| 306 |
propertyObj[property][2]); |
| 307 |
}); |
| 308 |
}, |
| 309 |
|
| 310 |
setState : function(state, unclampedState, rawState){ |
| 311 |
if(this.transition){ |
| 312 |
unclampedState = this.transition(rawState); |
| 313 |
state = Math.max(0, Math.min(1, unclampedState)); |
| 314 |
} |
| 315 |
for(var i = 0; i < this.effects.length; i++){ |
| 316 |
this.effects[i].setState(state, unclampedState, rawState); |
| 317 |
} |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
|
| 322 |
Kumu.Effect.StyleNumericEffect = Kumu.create(); |
| 323 |
|
| 324 |
Kumu.extend(Kumu.Effect.StyleNumericEffect.prototype, { |
| 325 |
|
| 326 |
initialize : function(elements, property, from, to, unit){ |
| 327 |
var self = Kumu.Effect; |
| 328 |
var argLists = self._parseStyleArguments(Kumu.toArray(elements), property, from, to); |
| 329 |
this.elements = Kumu.toArray(elements); |
| 330 |
this.property = property; |
| 331 |
this.unit = unit; |
| 332 |
this.argList = argLists.map(function(arg){ |
| 333 |
var from = this._parseUnits(arg[0], unit); |
| 334 |
var to = this._parseUnits(arg[1], unit); |
| 335 |
return [from, to]; |
| 336 |
}.bindScope(this)); |
| 337 |
|
| 338 |
}, |
| 339 |
|
| 340 |
_parseUnits : function(value, preUnits){ |
| 341 |
if(typeof(value) == 'number'){ |
| 342 |
return [value, preUnits]; |
| 343 |
} |
| 344 |
var unit = /em|ex|px|in|cm|mm|pt|pc|%/.exec(value.toLowerCase()); |
| 345 |
unit = unit ? unit[0] : preUnits; |
| 346 |
var floatValue = parseFloat(value); |
| 347 |
var intValue = parseInt(value); |
| 348 |
if(!isNaN(intValue) && (intValue == floatValue)){ |
| 349 |
return [intValue, unit]; |
| 350 |
}else{ |
| 351 |
return [floatValue, unit]; |
| 352 |
} |
| 353 |
}, |
| 354 |
|
| 355 |
setState : function(state, unclampedState, rawState){ |
| 356 |
var value; |
| 357 |
if(this.lfo){ |
| 358 |
unclampedState = this.lfo(rawState); |
| 359 |
state = Math.max(0, Math.min(1, unclampedState)); |
| 360 |
} |
| 361 |
var dh = Kumu.DHTML; |
| 362 |
for(var i = 0; i < this.elements.length; i++){ |
| 363 |
var node = this.elements[i]; |
| 364 |
var from = this.argList[i][0][0] |
| 365 |
var fromUnit = this.argList[i][0][1]; |
| 366 |
var to = this.argList[i][1][0]; |
| 367 |
var toUnit = this.argList[i][1][1]; |
| 368 |
var value; |
| 369 |
if(this.property == 'opacity'){ |
| 370 |
value = from + ((to -from) * state); |
| 371 |
}else{ |
| 372 |
var calc = from + ((to - from) * unclampedState); |
| 373 |
value = Math.round(calc) + (toUnit ? toUnit : 0); |
| 374 |
} |
| 375 |
if(this.property == 'opacity'){ |
| 376 |
dh.setOpacity(node, value); |
| 377 |
}else{ |
| 378 |
node.style[this.property.camelize()] = value; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
}); |
| 384 |
|
| 385 |
Kumu.Effect.StyleColorEffect = Kumu.create(); |
| 386 |
|
| 387 |
Kumu.extend(Kumu.Effect.StyleColorEffect.prototype, { |
| 388 |
initialize : function(elements, property, from, to){ |
| 389 |
var self = Kumu.Effect; |
| 390 |
var dh = Kumu.DHTML; |
| 391 |
this.elements = Kumu.toArray(elements); |
| 392 |
this.property = property; |
| 393 |
this.argList = self._parseStyleArguments(Kumu.toArray(elements), property, from, to); |
| 394 |
}, |
| 395 |
|
| 396 |
setState : function(state, unclampedState, rawState){ |
| 397 |
var color; |
| 398 |
if(this.lfo){ |
| 399 |
unclampedState = this.lfo(rawState); |
| 400 |
state = Math.max(0, Math.min(1, unclampedState)); |
| 401 |
} |
| 402 |
|
| 403 |
for(var i = 0; i < this.elements.length; i++){ |
| 404 |
var node = this.elements[i]; |
| 405 |
var from = this.argList[i][0] |
| 406 |
var to = this.argList[i][1]; |
| 407 |
color = Kumu.DHTML.blendColor(from, to, state); |
| 408 |
color = Kumu.DHTML.toHexString(color); |
| 409 |
node.style[this.property.camelize()] = color; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
}); |
| 414 |
|
| 415 |
|
| 416 |
Kumu.Effect._createStyleEffect = function(elements, property, from, to, unit){ |
| 417 |
var afx; |
| 418 |
var match; |
| 419 |
var units; |
| 420 |
var dh = Kumu.DHTML; |
| 421 |
var numericalRe = /^[0-9.]+(%|em|ex|px|in|cm|mm|pt|pc)?$/i; |
| 422 |
if(match = numericalRe.exec(to)){ |
| 423 |
afx = Kumu.Effect.StyleNumericEffect; |
| 424 |
var frommatch = numericalRe.exec(from); |
| 425 |
frommatch = frommatch ? frommatch[1] : null; |
| 426 |
units = unit || match[1] || frommatch; |
| 427 |
}else if(typeof(to) == 'string' && dh.parseColor(to).length == 3){ |
| 428 |
afx = Kumu.Effect.StyleColorEffect; |
| 429 |
units = null; |
| 430 |
}else{ |
| 431 |
throw new TypeError("Unrecognised format for value of " + prop + ": '" + to + "'"); |
| 432 |
} |
| 433 |
return new afx(elements, property, from, to, units); |
| 434 |
} |
| 435 |
|
| 436 |
Kumu.Effect._createCSSEFX = function(effector, style, option, elem){ |
| 437 |
var options = arguments[2]; |
| 438 |
var elements; |
| 439 |
if(options != null && !(typeof(options.length) == 'undefined' && typeof(options.nodeType) == 'undefined')){ |
| 440 |
elements = options; |
| 441 |
options = null; |
| 442 |
} |
| 443 |
if(typeof(style) == 'string'){ |
| 444 |
if(effector){ |
| 445 |
return new Kumu.Effect.Effector(options).addEffect(new Kumu.Effect.CSSEffect(elements, style)); |
| 446 |
}else{ |
| 447 |
return new Kumu.Effect.CSSEffect(elements, style); |
| 448 |
} |
| 449 |
}else if(style != null){ |
| 450 |
if(effector){ |
| 451 |
return new Kumu.Effect.Effector(options).addEffect(new Kumu.Effect.PropertyEffect(elements, style)); |
| 452 |
}else{ |
| 453 |
return new Kumu.Effect.PropertyEffect(elements, style); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
Kumu.Effect.createCSSEffector = function(){ |
| 459 |
var arg = Kumu.toArray(arguments); |
| 460 |
arg.unshift(true); |
| 461 |
return Kumu.Effect._createCSSEFX.apply(this, arg); |
| 462 |
} |
| 463 |
|
| 464 |
Kumu.Effect.createCSSEffect = function(){ |
| 465 |
var arg = Kumu.toArray(arguments); |
| 466 |
arg.unshift(false); |
| 467 |
return Kumu.Effect._createCSSEFX.apply(this, arg); |
| 468 |
} |
| 469 |
|