-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
893 lines (756 loc) · 25.5 KB
/
main.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*
FILE ARCHIVED ON 8:58:14 May 30, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 15:51:22 Aug 21, 2016.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
// Color Cycling in HTML5 Canvas
// BlendShift Technology conceived, designed and coded by Joseph Huckaby
// Copyright (c) 2001-2002, 2010 Joseph Huckaby.
// Released under the LGPL v3.0: /web/20160530085814/http://www.opensource.org/licenses/lgpl-3.0.html
FrameCount.visible = false;
var CanvasCycle = {
cookie: new CookieTree(),
query: parseQueryString( location.href ),
ctx: null,
imageData: null,
clock: 0,
inGame: false,
bmp: null,
globalTimeStart: (new Date()).getTime(),
inited: false,
optTween: null,
winSize: null,
globalBrightness: 1.0,
lastBrightness: 0,
sceneIdx: -1,
highlightColor: -1,
defaultMaxVolume: 0.5,
TL_WIDTH: 80,
TL_MARGIN: 15,
OPT_WIDTH: 150,
OPT_MARGIN: 15,
settings: {
showOptions: false,
targetFPS: 60,
zoomFull: false,
blendShiftEnabled: true,
speedAdjust: 1.0,
sound: true
},
contentSize: {
width: 640,
optionsWidth: 0,
height: 480 + 40,
scale: 1.0
},
init: function() {
// called when DOM is ready
if (!this.inited) {
this.inited = true;
$('container').style.display = 'block';
$('d_options').style.display = 'none';
$('d_timeline').style.display = 'none';
FrameCount.init();
this.handleResize();
var pal_disp = $('palette_display');
for (var idx = 0, len = 256; idx < len; idx++) {
var div = document.createElement('div');
div._idx = idx;
div.id = 'pal_' + idx;
div.className = 'palette_color';
div.onmouseover = function() { CanvasCycle.highlightColor = this._idx; };
div.onmouseout = function() { CanvasCycle.highlightColor = -1; };
//pal_disp.appendChild( div );
}
var div = document.createElement('div');
div.className = 'clear';
//pal_disp.appendChild( div );
// pick starting scene
// var initialSceneIdx = Math.floor( Math.random() * scenes.length );
// var initialSceneIdx = 0;
var monthIdx = (new Date()).getMonth();
var initialSceneIdx = -1;
for (var idx = 0, len = scenes.length; idx < len; idx++) {
var scene = scenes[idx];
if (scene.monthIdx == monthIdx) {
initialSceneIdx = idx;
idx = len;
}
}
if (initialSceneIdx == -1) initialSceneIdx = 0;
// populate scene menu
var html = '';
html += '<select id="fe_scene" onChange="CanvasCycle.switchScene(this)">';
for (var idx = 0, len = scenes.length; idx < len; idx++) {
var scene = scenes[idx];
html += '<option value="'+scene.name+'" '+((idx == initialSceneIdx) ? ' selected="selected"' : '')+'>'+scene.title+'</option>';
}
html += '</select>';
$('d_scene_selector').innerHTML = html;
// read prefs from cookie
var prefs = this.cookie.get('settings');
if (!prefs) prefs = {
showOptions: false,
targetFPS: 60,
zoomFull: false,
blendShiftEnabled: true,
speedAdjust: 1.0,
sound: true
};
// allow query to override prefs
for (var key in this.query) {
prefs[key] = this.query[key];
}
if (prefs) {
if (prefs.showOptions) this.toggleOptions();
this.setRate( prefs.targetFPS );
this.setZoom( prefs.zoomFull );
this.setSpeed( prefs.speedAdjust );
this.setBlendShift( prefs.blendShiftEnabled );
//this.setSound( prefs.sound );
}
// start synced to local time
var now = new Date();
this.timeOffset = (now.getHours() * 3600) + (now.getMinutes() * 60) + now.getSeconds();
this.updateTimelineDisplay();
// setup timeline drag
$('d_tl_thumb').addEventListener('mousedown', function(e) {
CC.tl_mouseDown = true;
CC.tl_mouseOriginY = e.pageY;
CC.tl_timeOrigin = CC.timeOffset;
e.preventDefault();
e.stopPropagation();
}, false );
window.addEventListener('mouseup', function(e) {
CC.tl_mouseDown = false;
}, false );
window.addEventListener( "mousemove", function(e) {
if (CC.tl_mouseDown) {
// visual thumb top range: 8px - 424px (416)
var yDelta = e.pageY - CC.tl_mouseOriginY;
CC.timeOffset = CC.tl_timeOrigin + Math.floor(yDelta * (86400 / 416));
if (CC.timeOffset < 0) CC.timeOffset = 0;
else if (CC.timeOffset >= 86400) CC.timeOffset = 86399;
CC.updateTimelineDisplay();
}
}, false );
// keyboard shortcuts
window.addEventListener('keydown', function(e) {
if (!e.shiftKey && !e.metaKey && !e.ctrlKey && !e.altKey) {
switch (e.keyCode) {
case 9: // tab
if (CC.inGame) {
CC.stop();
if (CC.audioTrack) {
try { CC.audioTrack.pause(); } catch(e) {;}
}
}
else {
CC.run();
if (CC.audioTrack && CC.settings.sound) {
try { CC.audioTrack.play(); } catch(e) {;}
}
}
break;
case 38: // up arrow
CC.timeOffset -= 60;
if (CC.timeOffset < 0) CC.timeOffset += 86400;
CC.updateTimelineDisplay();
break;
case 40: // down arrow
CC.timeOffset += 60;
if (CC.timeOffset >= 86400) CC.timeOffset -= 86400;
CC.updateTimelineDisplay();
break;
case 80: // P
CC.toggleOptions();
break;
case 66: // B
CC.setBlendShift( !CC.settings.blendShiftEnabled );
break;
}
e.preventDefault();
e.stopPropagation();
}
}, false );
// load initial scene
this.sceneIdx = initialSceneIdx;
this.loadScene( initialSceneIdx );
}
},
updateTimelineDisplay: function() {
// sync the timeline thumb position to the current time
$('d_tl_thumb').style.top = '' + Math.floor(8 + (this.timeOffset / (86400 / 416))) + 'px';
// also update the clocky
var ampm = 'AM';
var hour = Math.floor(this.timeOffset / 3600);
if (hour >= 12) {
ampm = 'PM';
if (hour > 12) hour -= 12;
}
else if (hour == 0) hour = 12;
if (hour < 10) hour = '0' + hour;
var minute = Math.floor( (this.timeOffset / 60) % 60 );
if (minute < 10) minute = '0' + minute;
var second = Math.floor( this.timeOffset % 60 );
if (second < 10) second = '0' + second;
$('d_tl_clock').innerHTML = '' + hour + ':' + minute + ' ' + ampm;
},
jumpScene: function(dir) {
// next or prev scene
this.sceneIdx += dir;
if (this.sceneIdx >= scenes.length) this.sceneIdx = 0;
else if (this.sceneIdx < 0) this.sceneIdx = scenes.length - 1;
$('fe_scene').selectedIndex = this.sceneIdx;
this.switchScene( $('fe_scene') );
},
switchScene: function(menu) {
// switch to new scene (grab menu selection)
this.stopSceneAudio();
var name = menu.options[menu.selectedIndex].value;
this.sceneIdx = menu.selectedIndex;
if (ua.mobile) {
// no transitions on mobile devices, just switch as fast as possible
this.inGame = false;
this.ctx.clearRect(0, 0, this.bmp.width, this.bmp.height);
this.ctx.fillStyle = "rgb(0,0,0)";
this.ctx.fillRect (0, 0, this.bmp.width, this.bmp.height);
CanvasCycle.globalBrightness = 1.0;
CanvasCycle.loadScene( this.sceneIdx );
}
else {
TweenManager.removeAll({ category: 'scenefade' });
TweenManager.tween({
target: { value: this.globalBrightness, newSceneIdx: this.sceneIdx },
duration: Math.floor( this.settings.targetFPS / 2 ),
mode: 'EaseInOut',
algo: 'Quadratic',
props: { value: 0.0 },
onTweenUpdate: function(tween) {
CanvasCycle.globalBrightness = tween.target.value;
},
onTweenComplete: function(tween) {
CanvasCycle.loadScene( tween.target.newSceneIdx );
},
category: 'scenefade'
});
}
},
loadScene: function(idx) {
// load image JSON from local .js files
this.stop();
this.showLoading();
var scene = scenes[idx];
switch(scene.name)
{
case "V26janclr":
var objScene = V26janclrx;
break;
case "V26SNOWjansnow":
var objScene = V26SNOWjansnowx;
break;
case "V19febclr":
var objScene = V19febclrx;
break;
case "V19febcldy":
var objScene = V19febcldyx;
break;
case "V30aprclr":
var objScene = V30aprclrx;
break;
case "V30RAINaprrain":
var objScene = V30RAINaprrainx;
break;
case "V08mayclr":
var objScene = V08mayclrx;
break;
case "V08maycldy":
var objScene = V08maycldyx;
break;
case "V08RAINmayrain":
var objScene = V08RAINmayrainx;
break;
case "V20JOEjunday":
var objScene = V20JOEjundayx;
break;
case "V25julyclr":
var objScene = V25julyclrx;
break;
case "V25julycldy":
var objScene = V25julycldyx;
break;
case "CORAL":
var objScene = CORALx;
break;
case "V29septclr":
var objScene = V29septclrx;
break;
case "V29septcldy":
var objScene = V29septcldyx;
break;
case "V05AMoctbegclr":
var objScene = V05AMoctbegclrx;
break;
case "V05octendclr":
var objScene = V05octendclrx;
break;
case "V05RAINoctrain":
var objScene = V05RAINoctrainx;
break;
case "V16novclr":
var objScene = V16novclrx;
break;
case "V16RAINnovrain":
var objScene = V16RAINnovrainx;
break;
case "V12BASICdecclr":
var objScene = V12BASICdecclrx;
break;
}
this.initScene(objScene);
/*
var url = 'scene.php?file='+scene.name+'&month='+scene.month+'&script='+scene.scpt+'&callback=CanvasCycle.initScene';
var scr = document.createElement('SCRIPT');
scr.type = 'text/javascript';
scr.src = url;
document.getElementsByTagName('HEAD')[0].appendChild(scr);
*/
},
showLoading: function() {
// show spinning loading indicator
var loading = $('d_loading');
var kicker = this.settings.showOptions ? (this.TL_WIDTH + this.TL_MARGIN) : 0;
loading.style.left = '' + Math.floor( kicker + (((this.contentSize.width * this.contentSize.scale) / 2) - 16) ) + 'px';
loading.style.top = '' + Math.floor( ((this.contentSize.height * this.contentSize.scale) / 2) - 16 ) + 'px';
loading.show();
},
hideLoading: function() {
// hide spinning loading indicator
$('d_loading').hide();
},
initScene: function(scene) {
// initialize, receive image data from server
this.initPalettes( scene.palettes );
this.initTimeline( scene.timeline );
// force a full palette and pixel refresh for first frame
this.oldTimeOffset = -1;
// create an intermediate palette that will hold the time-of-day colors
this.todPalette = new Palette( scene.base.colors, scene.base.cycles );
// process base scene image
this.bmp = new Bitmap(scene.base);
this.bmp.optimize();
var canvas = $('mycanvas');
if (!canvas.getContext) return; // no canvas support
if (!this.ctx) this.ctx = canvas.getContext('2d');
this.ctx.clearRect(0, 0, this.bmp.width, this.bmp.height);
this.ctx.fillStyle = "rgb(0,0,0)";
this.ctx.fillRect (0, 0, this.bmp.width, this.bmp.height);
if (!this.imageData) {
if (this.ctx.createImageData) {
this.imageData = this.ctx.createImageData( this.bmp.width, this.bmp.height );
}
else if (this.ctx.getImageData) {
this.imageData = this.ctx.getImageData( 0, 0, this.bmp.width, this.bmp.height );
}
else return; // no canvas data support
}
this.bmp.clear( this.imageData );
if (ua.mobile) {
// no transition on mobile devices
this.globalBrightness = 1.0;
}
else {
this.globalBrightness = 0.0;
TweenManager.removeAll({ category: 'scenefade' });
TweenManager.tween({
target: { value: 0 },
duration: Math.floor( this.settings.targetFPS / 2 ),
mode: 'EaseInOut',
algo: 'Quadratic',
props: { value: 1.0 },
onTweenUpdate: function(tween) {
CanvasCycle.globalBrightness = tween.target.value;
},
category: 'scenefade'
});
}
this.hideLoading();
this.run();
//this.startSceneAudio();
},
initPalettes: function(pals) {
// create palette objects for each raw time-based palette
var scene = scenes[this.sceneIdx];
this.palettes = {};
for (var key in pals) {
var pal = pals[key];
if (scene.remap) {
for (var idx in scene.remap) {
pal.colors[idx][0] = scene.remap[idx][0];
pal.colors[idx][1] = scene.remap[idx][1];
pal.colors[idx][2] = scene.remap[idx][2];
}
}
var palette = this.palettes[key] = new Palette( pal.colors, pal.cycles );
palette.copyColors( palette.baseColors, palette.colors );
}
},
initTimeline: function(entries) {
// create timeline with pointers to each palette
this.timeline = {};
for (var offset in entries) {
var palette = this.palettes[ entries[offset] ];
if (!palette) return alert("ERROR: Could not locate palette for timeline entry: " + entries[offset]);
this.timeline[offset] = palette;
}
},
run: function () {
// start main loop
if (!this.inGame) {
this.inGame = true;
this.animate();
}
},
stop: function() {
// stop main loop
this.inGame = false;
},
animate: function() {
// animate one frame. and schedule next
if (this.inGame) {
var colors = this.bmp.palette.colors;
if (this.settings.showOptions) {
for (var idx = 0, len = colors.length; idx < len; idx++) {
var clr = colors[idx];
var div = $('pal_'+idx);
//div.style.backgroundColor = 'rgb(' + clr.red + ',' + clr.green + ',' + clr.blue + ')';
}
// if (this.clock % this.settings.targetFPS == 0) $('d_debug').innerHTML = 'FPS: ' + FrameCount.current;
//$('d_debug').innerHTML = 'FPS: ' + FrameCount.current + ((this.highlightColor != -1) ? (' - Color #' + this.highlightColor) : '');
}
var optimize = true;
var newSec = FrameCount.count();
if (newSec && !this.tl_mouseDown) {
// advance time
this.timeOffset++;
if (this.timeOffset >= 86400) this.timeOffset = 0;
this.updateTimelineDisplay();
}
if (this.timeOffset != this.oldTimeOffset) {
// calculate time-of-day base colors
this.setTimeOfDayPalette();
optimize = false;
}
if (this.lastBrightness != this.globalBrightness) optimize = false;
if (this.highlightColor != this.lastHighlightColor) optimize = false;
// cycle palette
this.bmp.palette.cycle( this.bmp.palette.baseColors, GetTickCount(), this.settings.speedAdjust, this.settings.blendShiftEnabled );
if (this.highlightColor > -1) {
this.bmp.palette.colors[ this.highlightColor ] = new Color(0, 0, 0);
}
if (this.globalBrightness < 1.0) {
// bmp.palette.fadeToColor( pureBlack, 1.0 - globalBrightness, 1.0 );
this.bmp.palette.burnOut( 1.0 - this.globalBrightness, 1.0 );
}
// render pixels
this.bmp.render( this.imageData, optimize );
this.ctx.putImageData( this.imageData, 0, 0 );
this.lastBrightness = this.globalBrightness;
this.lastHighlightColor = this.highlightColor;
this.oldTimeOffset = this.timeOffset;
TweenManager.logic( this.clock );
this.clock++;
this.scaleAnimate();
if (this.inGame) setTimeout( function() { CanvasCycle.animate(); }, 1 );
}
},
setTimeOfDayPalette: function() {
// fade palette to proper time-of-day
// locate nearest timeline palette before, and after current time
// auto-wrap to find nearest out-of-bounds events (i.e. tomorrow and yesterday)
var before = {
palette: null,
dist: 86400,
offset: 0
};
for (var offset in this.timeline) {
if ((offset <= this.timeOffset) && ((this.timeOffset - offset) < before.dist)) {
before.dist = this.timeOffset - offset;
before.palette = this.timeline[offset];
before.offset = offset;
}
}
if (!before.palette) {
// no palette found, so wrap around and grab one with highest offset
var temp = 0;
for (var offset in this.timeline) {
if (offset > temp) temp = offset;
}
before.palette = this.timeline[temp];
before.offset = temp - 86400; // adjust timestamp for day before
}
var after = {
palette: null,
dist: 86400,
offset: 0
};
for (var offset in this.timeline) {
if ((offset >= this.timeOffset) && ((offset - this.timeOffset) < after.dist)) {
after.dist = offset - this.timeOffset;
after.palette = this.timeline[offset];
after.offset = offset;
}
}
if (!after.palette) {
// no palette found, so wrap around and grab one with lowest offset
var temp = 86400;
for (var offset in this.timeline) {
if (offset < temp) temp = offset;
}
after.palette = this.timeline[temp];
after.offset = temp + 86400; // adjust timestamp for day after
}
// copy the 'before' palette colors into our intermediate palette
this.todPalette.copyColors( before.palette.baseColors, this.todPalette.colors );
// now, fade to the 'after' palette, but calculate the correct 'tween' time
this.todPalette.fade( after.palette, this.timeOffset - before.offset, after.offset - before.offset );
// finally, copy the final colors back to the bitmap palette for cycling and rendering
this.bmp.palette.importColors( this.todPalette.colors );
},
scaleAnimate: function() {
// handle scaling image up or down
if (this.settings.zoomFull) {
// scale up to full size
var totalNativeWidth = this.contentSize.width + this.contentSize.optionsWidth;
var maxScaleX = (this.winSize.width - 30) / totalNativeWidth;
var totalNativeHeight = this.contentSize.height;
var maxScaleY = (this.winSize.height - 30) / totalNativeHeight;
var maxScale = Math.min( maxScaleX, maxScaleY );
if (this.contentSize.scale != maxScale) {
this.contentSize.scale += ((maxScale - this.contentSize.scale) / 8);
if (Math.abs(this.contentSize.scale - maxScale) < 0.001) this.contentSize.scale = maxScale; // close enough
var sty = $('mycanvas').style;
if (ua.webkit) sty.webkitTransform = 'translate3d(0px, 0px, 0px) scale('+this.contentSize.scale+')';
else if (ua.ff) sty.MozTransform = 'scale('+this.contentSize.scale+')';
else if (ua.op) sty.OTransform = 'scale('+this.contentSize.scale+')';
else sty.transform = 'scale('+this.contentSize.scale+')';
sty.marginRight = '' + Math.floor( (this.contentSize.width * this.contentSize.scale) - this.contentSize.width ) + 'px';
$('d_header').style.width = '' + Math.floor(this.contentSize.width * this.contentSize.scale) + 'px';
this.repositionContainer();
}
}
else {
// scale back down to native
if (this.contentSize.scale > 1.0) {
this.contentSize.scale += ((1.0 - this.contentSize.scale) / 8);
if (this.contentSize.scale < 1.001) this.contentSize.scale = 1.0; // close enough
var sty = $('mycanvas').style;
if (ua.webkit) sty.webkitTransform = 'translate3d(0px, 0px, 0px) scale('+this.contentSize.scale+')';
else if (ua.ff) sty.MozTransform = 'scale('+this.contentSize.scale+')';
else if (ua.op) sty.OTransform = 'scale('+this.contentSize.scale+')';
else sty.transform = 'scale('+this.contentSize.scale+')';
sty.marginRight = '' + Math.floor( (this.contentSize.width * this.contentSize.scale) - this.contentSize.width ) + 'px';
$('d_header').style.width = '' + Math.floor(this.contentSize.width * this.contentSize.scale) + 'px';
this.repositionContainer();
}
}
},
repositionContainer: function() {
// reposition container element based on inner window size
var div = $('container');
if (div) {
this.winSize = getInnerWindowSize();
div.style.left = '' + Math.floor((this.winSize.width / 2) - (((this.contentSize.width * this.contentSize.scale) + this.contentSize.optionsWidth) / 2)) + 'px';
div.style.top = '' + Math.floor((this.winSize.height / 2) - ((this.contentSize.height * this.contentSize.scale) / 2)) + 'px';
}
},
handleResize: function() {
// called when window resizes
this.repositionContainer();
if (this.settings.zoomFull) this.scaleAnimate();
},
saveSettings: function() {
// save settings in cookie
this.cookie.set( 'settings', this.settings );
this.cookie.save();
},
startSceneAudio: function() {
// start audio for current scene, if applicable
var scene = scenes[ this.sceneIdx ];
if (scene.sound && this.settings.sound && window.Audio) {
if (this.audioTrack) {
try { this.audioTrack.pause(); } catch(e) {;}
}
TweenManager.removeAll({ category: 'audio' });
var ext = (ua.ff || ua.op) ? 'ogg' : 'mp3';
var track = this.audioTrack = new Audio( 'audio/' + scene.sound + '.' + ext );
track.volume = 0;
track.loop = true;
track.autobuffer = false;
track.autoplay = true;
track.addEventListener('canplaythrough', function() {
track.play();
TweenManager.tween({
target: track,
duration: Math.floor( CanvasCycle.settings.targetFPS * 2 ),
mode: 'EaseOut',
algo: 'Linear',
props: { volume: scene.maxVolume || CanvasCycle.defaultMaxVolume },
category: 'audio'
});
CanvasCycle.hideLoading();
CanvasCycle.run();
}, false);
if (ua.iphone || ua.ipad) {
// these may support audio, but just don't invoke events
// try to force it
setTimeout( function() {
track.play();
track.volume = 1.0;
CanvasCycle.hideLoading();
CanvasCycle.run();
}, 1000 );
}
if (ua.ff || ua.mobile) {
// loop doesn't seem to work on FF or mobile devices, so let's force it
track.addEventListener('ended', function() {
track.currentTime = 0;
track.play();
}, false);
}
track.load();
} // sound enabled and supported
else {
// no sound for whatever reason, so just start main loop
this.hideLoading();
this.run();
}
},
stopSceneAudio: function() {
// fade out and stop audio for current scene
var scene = scenes[ this.sceneIdx ];
if (scene.sound && this.settings.sound && window.Audio && this.audioTrack) {
var track = this.audioTrack;
if (ua.iphone || ua.ipad) {
// no transition here, so just stop sound
track.pause();
}
else {
TweenManager.removeAll({ category: 'audio' });
TweenManager.tween({
target: track,
duration: Math.floor( CanvasCycle.settings.targetFPS / 2 ),
mode: 'EaseOut',
algo: 'Linear',
props: { volume: 0 },
onTweenComplete: function(tween) {
// ff has weird delay with volume fades, so allow sound to continue
// will be stopped when next one starts
if (!ua.ff) track.pause();
},
category: 'audio'
});
}
}
},
toggleOptions: function() {
var startValue, endValue;
TweenManager.removeAll({ category: 'options' });
if (!this.settings.showOptions) {
startValue = 0;
if (this.optTween) startValue = this.optTween.target.value;
endValue = 1.0;
$('d_options').style.display = '';
$('d_options').style.opacity = startValue;
$('btn_options_toggle').innerHTML = '« Hide Options';
$('d_timeline').style.width = '0px';
$('d_timeline').style.display = '';
$('d_timeline').style.opacity = startValue;
}
else {
startValue = 1.0;
if (this.optTween) startValue = this.optTween.target.value;
endValue = 0;
$('btn_options_toggle').innerHTML = 'Show Options »';
}
this.optTween = TweenManager.tween({
target: { value: startValue },
duration: Math.floor( this.settings.targetFPS / 3 ),
mode: 'EaseOut',
algo: 'Quadratic',
props: { value: endValue },
onTweenUpdate: function(tween) {
// $('d_options').style.left = '' + Math.floor(tween.target.value - 150) + 'px';
$('d_options').style.opacity = tween.target.value;
$('btn_options_toggle').style.left = '' + Math.floor(tween.target.value * 128) + 'px';
var tl_sty = $('d_timeline').style;
tl_sty.opacity = tween.target.value;
tl_sty.width = '' + Math.floor(tween.target.value * CC.TL_WIDTH) + 'px';
tl_sty.marginRight = '' + Math.floor(tween.target.value * CC.TL_MARGIN) + 'px';
$('d_header').style.marginLeft = '' + Math.floor(tween.target.value * (CC.TL_WIDTH + CC.TL_MARGIN)) + 'px';
CanvasCycle.contentSize.optionsWidth = Math.floor( tween.target.value * (CC.OPT_WIDTH + CC.OPT_MARGIN + CC.TL_WIDTH + CC.TL_MARGIN) );
CanvasCycle.handleResize();
},
onTweenComplete: function(tween) {
if (tween.target.value == 0) {
$('d_options').style.display = 'none';
$('d_timeline').style.display = 'none';
}
CanvasCycle.optTween = null;
},
category: 'options'
});
this.settings.showOptions = !this.settings.showOptions;
this.saveSettings();
},
setZoom: function(enabled) {
if (enabled != this.settings.zoomFull) {
this.settings.zoomFull = enabled;
this.saveSettings();
$('btn_zoom_actual').setClass('selected', !enabled);
$('btn_zoom_max').setClass('selected', enabled);
}
},
setSound: function(enabled) {
$('btn_sound_on').setClass('selected', enabled);
$('btn_sound_off').setClass('selected', !enabled);
this.settings.sound = enabled;
if (this.sceneIdx > -1) {
if (enabled) {
// enable sound
if (this.audioTrack) this.audioTrack.play();
else this.startSceneAudio();
}
else {
// disable sound
if (this.audioTrack) this.audioTrack.pause();
}
}
this.saveSettings();
},
setRate: function(rate) {
/* $('btn_rate_30').setClass('selected', rate == 30);
$('btn_rate_60').setClass('selected', rate == 60);
$('btn_rate_90').setClass('selected', rate == 90); */
this.settings.targetFPS = rate;
this.saveSettings();
},
setSpeed: function(speed) {
$('btn_speed_025').setClass('selected', speed == 0.25);
$('btn_speed_05').setClass('selected', speed == 0.5);
$('btn_speed_1').setClass('selected', speed == 1);
$('btn_speed_2').setClass('selected', speed == 2);
$('btn_speed_4').setClass('selected', speed == 4);
this.settings.speedAdjust = speed;
this.saveSettings();
},
setBlendShift: function(enabled) {
$('btn_blendshift_on').setClass('selected', enabled);
$('btn_blendshift_off').setClass('selected', !enabled);
this.settings.blendShiftEnabled = enabled;
this.saveSettings();
}
};
var CC = CanvasCycle; // shortcut