-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.js
1280 lines (1013 loc) · 39.3 KB
/
editor.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Created by DD on 2015/6/20.
*/
// using 'self' instead of 'window' for compatibility with both NodeJS and IE10.
( function () {
var lastTime = 0;
var vendors = [ 'ms', 'moz', 'webkit', 'o' ];
for ( var x = 0; x < vendors.length && !self.requestAnimationFrame; ++ x ) {
self.requestAnimationFrame = self[ vendors[ x ] + 'RequestAnimationFrame' ];
self.cancelAnimationFrame = self[ vendors[ x ] + 'CancelAnimationFrame' ] || self[ vendors[ x ] + 'CancelRequestAnimationFrame' ];
}
if ( self.requestAnimationFrame === undefined && self['setTimeout'] !== undefined ) {
self.requestAnimationFrame = function ( callback ) {
var currTime = Date.now(), timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
var id = self.setTimeout( function() { callback( currTime + timeToCall ); }, timeToCall );
lastTime = currTime + timeToCall;
return id;
};
}
if( self.cancelAnimationFrame === undefined && self['clearTimeout'] !== undefined ) {
self.cancelAnimationFrame = function ( id ) { self.clearTimeout( id ) };
}
// Fix up for AudioContext
self.AudioContext = self.AudioContext||self.webkitAudioContext;
}() );
var DDMUG = DDMUG || { VISION: '0.1' };
DDMUG.Keys = {
L1 : 68,//D
L2 : 70,//F
R1 : 74,//J
R2 : 75//K
}
DDMUG.MusicNode = function(time, position){
this.time = time;
this.position = position;
}
DDMUG.Edioter = function(parameters){
parameters = parameters || {};
var _width = parameters.width !== undefined ? parameters.width : 400,
_height = parameters.height !== undefined ? parameters.height : 302,
_trackNum = parameters.trackNum !== undefined ? parameters.trackNum : 4,
_stageRender = parameters.stageRender !== undefined ? parameters.stageRender : new DDMUG.StageRender(),
_self = this;
this.stageRender = _stageRender;
this.domElement = _stageRender.domElement;
function buildStage(musicUrl){
console.log('DDMUG.Edioter.buildStage()');
_stageRender.setSize(_width, _height);
var audio = new DDMUG.Audio();
audio.buildUp(musicUrl, function(){
_self.pannel = new DDMUG.Pannel({x: 0, y: 0, width: _width, height: 300, audio: audio});
_stageRender.addElement(_self.pannel);
});
};
function bindMouseEvent(){
// calculate position of the canvas DOM element on the page
var canvasPosition = {
x: _stageRender.domElement.offsetLeft,
y: _stageRender.domElement.offsetTop
};
_stageRender.domElement.addEventListener('click', function(e){
e.preventDefault();
// use pageX and pageY to get the mouse position relative to the browser window
// now you have local coordinates,
// which consider a (0,0) origin at the top-left of canvas element
var mouse = {
x: e.pageX - canvasPosition.x,
y: e.pageY - canvasPosition.y
}
// iterate through all elements in stage and call the onclick handler of each
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onClick(mouse.x, mouse.y, _self);
}
return false;
});
_stageRender.domElement.addEventListener('dblclick', function(e){
e.preventDefault();
var mouse = {
x: e.pageX - canvasPosition.x,
y: e.pageY - canvasPosition.y
};
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onDblClick(mouse.x, mouse.y, _self);
}
return false;
});
_stageRender.domElement.addEventListener('mousedown', function(e){
e.preventDefault();
var mouse = {
x: e.pageX - canvasPosition.x,
y: e.pageY - canvasPosition.y
};
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onMouseDown(mouse.x, mouse.y, _self);
}
return false;
});
_stageRender.domElement.addEventListener('mousemove', function(e){
e.preventDefault();
var mouse = {
x: e.pageX - canvasPosition.x,
y: e.pageY - canvasPosition.y
};
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onMouseMove(mouse.x, mouse.y, _self);
}
return false;
});
_stageRender.domElement.addEventListener('mouseup', function(e){
e.preventDefault();
var mouse = {
x: e.pageX - canvasPosition.x,
y: e.pageY - canvasPosition.y
}
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onMouseUp(mouse.x, mouse.y, _self);
}
return false;
});
};
function bindKey(){
//fix canvas`s key event
var newAttr = document.createAttribute('tabindex');
newAttr.nodeValue = '0';
_stageRender.domElement.setAttributeNode(newAttr);
_stageRender.domElement.addEventListener('mouseenter', function(e){
this.focus();
});
_stageRender.domElement.addEventListener('keydown', function(e){
var keyCode = e.which;
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onKeyDown(keyCode, _self);
}
});
_stageRender.domElement.addEventListener('keyup', function(e){
var keyCode = e.which;
for (var i=0; i < _stageRender.elements.length; i++) {
_stageRender.elements[i].onKeyUp(keyCode, _self);
}
});
};
this.init = function(musicUrl){
buildStage(musicUrl);
bindMouseEvent();
bindKey();
_stageRender.start();//启动渲染线程
};
}
DDMUG.StageRender = function(parameters){
parameters = parameters || {};
var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement('canvas'),
_context = parameters.context !== undefined ? parameters.context : null,
_viewportWidth = _canvas.width,
_viewportHeight = _canvas.height;
var _canvasCtx = _context || _canvas.getContext("2d"),
_render;//
// public properties
this.domElement = _canvas;
this.elements = [];//【瞩目】这个需要暴露给其他对象修改的属性,如果用var _element = [];this.element = _element;
//这样的写法会导致问题,要时刻记得在赋值时要同时对2个属性进行操作。
//如果在clear()方法中,_elements = _elements.slice(0, i).concat(_elements.slice(i+1, _elements.length));这时候
//_elements被赋值了新的数组,这时_element和element指向的是2个不同的数组。
this.setSize = function ( width, height, updateStyle ) {
_canvas.width = width;
_canvas.height = height;
if ( updateStyle !== false ) {
_canvas.style.width = width + 'px';
_canvas.style.height = height + 'px';
}
};
this.addElement = function(element){
this.elements.push(element);
};
this.render = function(){
_canvasCtx.clearRect(0, 0, _viewportWidth, _viewportHeight);
for (var i=0; i < this.elements.length; i++) {
this.elements[i].draw(_canvasCtx);
}
};
this.update = function(){
for (var i=0; i < this.elements.length; i++) {
this.elements[i].update();
}
};
this.clear = function(){
for (var i=0; i < this.elements.length; i++) {
if(!this.elements[i].alive){
delete this.elements[i];//删除相应对象
this.elements = this.elements.slice(0, i).concat(this.elements.slice(i+1, this.elements.length));//清除数组中该位置
}
}
};
this.start = function(){
if(_render != null) return;//防止重复启动
//使用html5新增的requestAnimFrame API
var that = this;
( function animate (){
//that.clear();
//that.update();
that.render();
_render = requestAnimationFrame(animate, that);
} )();
};
this.stop = function(){
if(_render == null) return;
self.cancelAnimationFrame(_render);
_render = null;
};
this.pause = function(){
};
}
DDMUG.Element = function(parameters){
parameters = parameters || {};
var _position = {
x: parameters.x !== undefined ? parameters.x : 0,
y: parameters.y !== undefined ? parameters.y : 0
},
_size = {
width: parameters.width !== undefined ? parameters.width : 0,
height: parameters.height !== undefined ? parameters.height : 0
},
_speed = {
x: parameters.speedX !== undefined ? parameters.speedX : 0,
y: parameters.speedY !== undefined ? parameters.speedY : 0
};
this.alive = true;
Object.defineProperties( this, {
'position': { value: _position },
'size': { value: _size },
'speed': { value: _speed }
} );
this.draw = function(ctx){
};
this.update = function(){
};
}
DDMUG.EventElement = function(parameters){
DDMUG.Element.call(this, parameters);
/*矩形碰撞检测*/
this.checkCollosion = function(x, y) {
// perform hit test between bounding box and mouse coordinates
if (this.position.x <= x &&
this.position.x + this.size.width > x &&
this.position.y <= y &&
this.position.y + this.size.height > y) {
return true;
}
return false;
};
this.onClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//console.log(context)
}
}
this.onDoubleClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//console.log(context)
}
}
this.dragging = false;
this.onMouseDown = function(x, y, context){
if(this.checkCollosion(x, y)){
this.dragging = true;
}
}
this.onMouseMove = function(x, y, context){
if(this.dragging){
}
}
this.onMouseUp = function(x, y, context){
if(this.dragging){
this.dragging = false;
}
}
this.onKeyDown = function(keyCode, context){
}
this.onKeyUp = function(keyCode, context){
}
}
DDMUG.EventElement.prototype = Object.create( DDMUG.Element.prototype );
DDMUG.EventElement.prototype.constructor = DDMUG.Element;
DDMUG.Audio = function(parameters){
parameters = parameters || {};
var _context = new AudioContext(),
_audio = new Audio('audio');
Object.defineProperties( this, {
'audio': { value: _audio }
} );
this.buildUp = function(url, callback){
//【注意】不用这种方式,因为创建出的AudioBufferSourceNode对象,只能start和stop一次。详见http://www.w3.org/TR/webaudio/#AudioBufferSourceNode
//var source = _context.createBufferSource(); // creates a sound source
//source.buffer = buffer; // tell the source which sound to play
//通过audio标签获取music,不用等到mp3全部加载完成。
//_audio = new Audio('audio');
_audio.src = url;
var that = this;
_audio.addEventListener("loadedmetadata", function(e) {//【不能用canplay事件否则每次修改currentTime都会触发】
//使用MediaElementAudioSourceNode 对象可以通过修改currentTime指定从哪里开始播放,而且可以多次指定。
var source = _context.createMediaElementSource(_audio);
// Create a gain node.
var gainNode = _context.createGain();
// Connect the source to the gain node.
source.connect(gainNode);
// Connect the gain node to the destination.
gainNode.connect(_context.destination);
// Reduce the volume.
gainNode.gain.value = 0.5;
//this.source = source;
//console.log(that.audio.duration)
that.duration = _audio.duration;
//this.loaded = true;
callback();
});
}
this.play = function() {
_audio.play();
};
this.seek = function(offset) {
_audio.currentTime = offset;
};
this.stop = function() {
_audio.pause();
};
}
DDMUG.Pannel = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _buttonSize = 34,
_buttonMargin = 5,//对外
_buttonPadding = 5.5,//对内
_headerHeight = _buttonSize,//头部控制栏高度,和按钮同高
_trackLabelWidth = _buttonSize*4,//左侧宽度
_timeLineWidth = this.size.width - _trackLabelWidth,
_timeScrollHeight = 20;//时间缩放高度
this.audio = parameters.audio;
var playBtn = new DDMUG.PlayButton({x: this.position.x + 0, y: this.position.y + 0, width: _buttonSize, height: _buttonSize, buttonMargin: _buttonMargin, buttonPadding: _buttonPadding}),
pauseBtn = new DDMUG.PauseButton({x: this.position.x + _buttonSize, y: this.position.y + 0, width: _buttonSize, height: _buttonSize, buttonMargin: _buttonMargin, buttonPadding: _buttonPadding}),
stopBtn = new DDMUG.StopButton({x: this.position.x + _buttonSize*2, y: this.position.y + 0, width: _buttonSize, height: _buttonSize, buttonMargin: _buttonMargin, buttonPadding: _buttonPadding}),
exportBtn = new DDMUG.ExportButton({x: this.position.x + _buttonSize*3, y: this.position.y + 0, width: _buttonSize, height: _buttonSize, buttonMargin: _buttonMargin, buttonPadding: _buttonPadding});
var timeScale = new DDMUG.TimeScale({x: this.position.x + 0, y: this.position.y + this.size.height - _timeScrollHeight, width: _trackLabelWidth, height: _timeScrollHeight});
this.timeLine = new DDMUG.TimeLine({x: this.position.x + _trackLabelWidth, y: this.position.y + 0, width: _timeLineWidth, height: _buttonSize, tickerHeight: this.size.height, timeScale: timeScale, timelineEnd: this.audio.duration});
this.timeScrollBar = new DDMUG.TimeScrollBar({
x: this.position.x + _trackLabelWidth,
y: this.position.y + this.size.height - _timeScrollHeight,
width: _timeLineWidth,
height: _timeScrollHeight, timeLine: this.timeLine});
var trackHeight = 30;
var trackL1 = new DDMUG.Track({x: this.position.x + 0, y: this.position.y + _buttonSize + 0*trackHeight, width: this.size.width, height: trackHeight, name: "track L1", key: DDMUG.Keys.L1, trackLabelWidth: _trackLabelWidth, timeLine: this.timeLine}),
trackL2 = new DDMUG.Track({x: this.position.x + 0, y: this.position.y + _buttonSize + 1*trackHeight, width: this.size.width, height: trackHeight, name: "track L2", key: DDMUG.Keys.L2, trackLabelWidth: _trackLabelWidth, timeLine: this.timeLine}),
trackR1 = new DDMUG.Track({x: this.position.x + 0, y: this.position.y + _buttonSize + 2*trackHeight, width: this.size.width, height: trackHeight, name: "track R1", key: DDMUG.Keys.R1, trackLabelWidth: _trackLabelWidth, timeLine: this.timeLine}),
trackR2 = new DDMUG.Track({x: this.position.x + 0, y: this.position.y + _buttonSize + 3*trackHeight, width: this.size.width, height: trackHeight, name: "track R2", key: DDMUG.Keys.R2, trackLabelWidth: _trackLabelWidth, timeLine: this.timeLine});
this.tracks = [trackL1, trackL2, trackR1, trackR2];
this.setBPM = function(bpm){//设置bpm
this.timeLine.bpm = bpm;
return this.timeLine.bpm;
}
this.update = function(){
if(!this.audio.audio.paused && !pref) {//播放时更新时间并自动拖动时间栏
this.timeLine.updateTime(this.audio.audio.currentTime);
if(this.timeLine.tickerX -_trackLabelWidth > this.timeLine.size.width){
var dragX = Math.min(this.timeLine.position.x+this.timeLine.timeToX(this.timeLine.time), this.timeLine.size.width);
this.timeScrollBar.calcThumbPosX(dragX*this.timeScrollBar.timeScrollRatio, this); //时间栏拖动
}
};
};
this.moveTracks = function(x){//当timeScrollBar移动时触发,移动所有的tracks
for(var i = 0; i<this.tracks.length; i++){
this.tracks[i].move(x);
}
}
this.drawElements = function(ctx){
//ctx.fillStyle = "#DDDDDD";
playBtn.draw(ctx);
pauseBtn.draw(ctx);
stopBtn.draw(ctx);
exportBtn.draw(ctx);
timeScale.draw(ctx);
//trackL1.draw(ctx);
for(var i = 0; i<this.tracks.length; i++){
this.tracks[i].draw(ctx);
}
this.timeLine.draw(ctx);
this.timeScrollBar.draw(ctx);
//header borders
drawLine(ctx, this.position.x, this.position.y, this.position.x+this.size.width, this.position.y, "#000000");//上边框
drawLine(ctx, this.position.x, this.position.y+_headerHeight, this.position.x+this.size.width, this.position.y+_headerHeight, "#000000");//控制栏边框
drawLine(ctx, this.position.x, this.position.y+this.size.height - _timeScrollHeight, this.position.x+_trackLabelWidth, this.position.y+this.size.height - _timeScrollHeight, "#000000");//时间缩放上边框
drawLine(ctx, this.position.x+_trackLabelWidth, this.position.y, this.position.x+_trackLabelWidth, this.position.y+this.size.height, "#000000");//左右分隔栏
drawLine(ctx, this.position.x, this.position.y+this.size.height, this.position.x+this.size.width, this.position.y+this.size.height, "#000000"); //下边框
}
function drawLine(ctx, x1, y1, x2, y2, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1+0.5, y1+0.5);
ctx.lineTo(x2+0.5, y2+0.5);
ctx.stroke();
}
this.draw = function(ctx){
this.update();
ctx.save();
ctx.clearRect(this.position.x, this.position.y, this.size.width, this.size.height);
this.drawElements(ctx);
ctx.restore();
};
function downloadFile(fileName, content){
var aLink = document.createElement('a');
var blob = new Blob([content]);
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", false, false);
aLink.download = fileName;
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
}
this.export = function(){//导出track
console.log('export track: ')
var musicMap = [];
for(var i = 0; i<this.tracks.length; i++){
musicMap.push(this.tracks[i].export());
}
console.log(JSON.stringify(musicMap));
downloadFile("ddmug.map.txt", JSON.stringify(musicMap));
}
this.importMusicMap = function(musicMapString){//导入
var musicMap = JSON.parse(musicMapString);
for(var i = 0; i<musicMap.length; i++){//每个track
var trackNodes = musicMap[i];
for(var j = 0; j<trackNodes.length; j++){
this.tracks[i].keyFrames.push(new DDMUG.KeyFrame({
x: this.tracks[i].position.x+_trackLabelWidth+this.timeLine.timeToX(trackNodes[j].time),
y: this.tracks[i].position.y+this.tracks[i].size.height/2-10/2,
width: 10, height: 10,
time: trackNodes[j].time, key: this.tracks[i].key,
timeLine: this.timeLine, track: this.tracks[i], trackLabelWidth: _trackLabelWidth}));
}
}
console.log('import track done ')
}
this.analysis = function(url){
var analyzer = new DDMUG.Analyzer();
var that = this;
analyzer.spawnHit = function(i, beatband, time) {
time = time - time%(1.0/that.timeLine.bpm);
that.tracks[i].keyFrames.push(new DDMUG.KeyFrame({
x: that.tracks[i].position.x+_trackLabelWidth+that.timeLine.timeToX(time),
y: that.tracks[i].position.y+that.tracks[i].size.height/2-10/2,
width: 10, height: 10,
time: time, key: that.tracks[i].key,
timeLine: that.timeLine, track: that.tracks[i], trackLabelWidth: _trackLabelWidth}));
}
analyzer.analysis(url);
}
this.onClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//console.log('pannel');
playBtn.onClick(x, y, this);
pauseBtn.onClick(x, y, this);
stopBtn.onClick(x, y, this);
exportBtn.onClick(x, y, this);
this.timeLine.onClick(x, y, this);
timeScale.onClick(x, y, this);
}
}
this.onDblClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//trackL1.onDblClick(x, y, this);
for(var i = 0; i<this.tracks.length; i++){
this.tracks[i].onDblClick(x, y, this);
}
}
}
this.onMouseDown = function(x, y, context){
if(this.checkCollosion(x, y)){
this.dragging = true;
timeScale.onMouseDown(x, y, this);
this.timeLine.onMouseDown(x, y, this);
this.timeScrollBar.onMouseDown(x, y, this);
}
}
this.onMouseUp = function(x, y, context){
if(this.dragging){
this.dragging = false;
timeScale.onMouseUp(x, y, this);
this.timeLine.onMouseUp(x, y, this);
this.timeScrollBar.onMouseUp(x, y, this);
}
}
this.onMouseMove = function(x, y, context){
if(this.dragging){
timeScale.onMouseMove(x, y, this);
this.timeLine.onMouseMove(x, y, this);
this.timeScrollBar.onMouseMove(x, y, this);
}
}
var pref ;
this.onKeyDown = function(keyCode, context){
//console.log(keyCode)
if(keyCode==32){//空格 播放/暂停
if(this.audio.audio.paused)
this.audio.play();
else
this.audio.stop();
}
for(var i = 0; i<this.tracks.length; i++){
if(keyCode == this.tracks[i].key){
this.tracks[i].onDblClick(this.tracks[i].position.x+_trackLabelWidth+this.timeLine.timeToX(this.timeLine.time), this.tracks[i].position.y+this.tracks[i].size.height/2-10/2, this);
}
}
if(keyCode==17){//左Ctrl 按住时开启音频预览模式,像AE一样
pref = true;
this.audio.audio.currentTime = this.timeLine.time;
this.audio.play();
}
}
this.onKeyUp = function(keyCode, context){
if(keyCode==17){//左Ctrl
pref = false;
this.audio.stop();
}
}
}
DDMUG.Pannel.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.Pannel.prototype.constructor = DDMUG.EventElement;
DDMUG.PlayButton = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _buttonMargin = parameters.buttonMargin !== undefined ? parameters.buttonMargin : 5,//对外
_buttonPadding = parameters.buttonPadding !== undefined ? parameters.buttonPadding : 5.5,//对内
_buttonSize = {
width: this.size.width - 2*_buttonMargin,
height: this.size.height - 2*_buttonMargin
};
this.onClick = function(x, y, pannel){
if(this.checkCollosion(x, y)){
console.log('play btn');
//console.log(this);
//console.log(pannel.timeLine.time);
pannel.audio.play();
}
}
this.draw = function(ctx){
ctx.save();
ctx.fillStyle = "#DDDDDD";
var x = this.position.x + _buttonMargin,
y = this.position.y + _buttonMargin;
//play
ctx.fillRect(x, y, _buttonSize.width, _buttonSize.height);
ctx.strokeStyle = "#777777";
ctx.beginPath();
ctx.moveTo(x + _buttonPadding, y + _buttonPadding);
ctx.lineTo(x + _buttonSize.height - _buttonPadding,y + _buttonSize.height/2);
ctx.lineTo(x + _buttonPadding, y + _buttonSize.height - _buttonPadding);
ctx.lineTo(x + _buttonPadding, y + _buttonPadding);
ctx.stroke();
ctx.restore();
};
}
DDMUG.PlayButton.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.PlayButton.prototype.constructor = DDMUG.EventElement;
DDMUG.PauseButton = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _buttonMargin = parameters.buttonMargin !== undefined ? parameters.buttonMargin : 5,//对外
_buttonPadding = parameters.buttonPadding !== undefined ? parameters.buttonPadding : 5.5,//对内
_buttonSize = {
width: this.size.width - 2*_buttonMargin,
height: this.size.height - 2*_buttonMargin
};
this.onClick = function(x, y, pannel){
if(this.checkCollosion(x, y)){
console.log('pause btn');
//console.log(this);
//console.log(context);
pannel.audio.stop();
}
}
this.draw = function(ctx){
ctx.save();
ctx.fillStyle = "#DDDDDD";
var x = this.position.x + _buttonMargin,
y = this.position.y + _buttonMargin;
//pause
ctx.fillRect(x, y, _buttonSize.width, _buttonSize.height);
ctx.strokeRect(x + _buttonPadding, y + _buttonPadding, (_buttonSize.width- _buttonPadding*2)/3, _buttonSize.height - _buttonPadding*2);
ctx.strokeRect(x + _buttonPadding + (_buttonSize.width- _buttonPadding*2)*2/3, y + _buttonPadding, (_buttonSize.width- _buttonPadding*2)/3, _buttonSize.height - _buttonPadding*2);
ctx.restore();
};
}
DDMUG.PauseButton.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.PauseButton.prototype.constructor = DDMUG.EventElement;
DDMUG.StopButton = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _buttonMargin = parameters.buttonMargin !== undefined ? parameters.buttonMargin : 5,//对外
_buttonPadding = parameters.buttonPadding !== undefined ? parameters.buttonPadding : 5.5,//对内
_buttonSize = {
width: this.size.width - 2*_buttonMargin,
height: this.size.height - 2*_buttonMargin
};
this.onClick = function(x, y, pannel){
if(this.checkCollosion(x, y)){
console.log('stop btn');
//console.log(this);
//console.log(context);
pannel.audio.seek(0);
pannel.audio.stop();
}
}
this.draw = function(ctx){
ctx.save();
ctx.fillStyle = "#DDDDDD";
var x = this.position.x + _buttonMargin,
y = this.position.y + _buttonMargin;
//stop
ctx.fillRect(x, y, _buttonSize.width, _buttonSize.height);
ctx.strokeRect(x + _buttonPadding, y + _buttonPadding, _buttonSize.width - _buttonPadding*2, _buttonSize.height-_buttonPadding*2);
ctx.restore();
};
}
DDMUG.StopButton.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.StopButton.prototype.constructor = DDMUG.EventElement;
DDMUG.ExportButton = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _buttonMargin = parameters.buttonMargin !== undefined ? parameters.buttonMargin : 5,//对外
_buttonPadding = parameters.buttonPadding !== undefined ? parameters.buttonPadding : 5.5,//对内
_buttonSize = {
width: this.size.width - 2*_buttonMargin,
height: this.size.height - 2*_buttonMargin
};
this.onClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//console.log('export btn');
context.export();
}
}
this.draw = function(ctx){
ctx.save();
ctx.fillStyle = "#DDDDDD";
var x = this.position.x + _buttonMargin,
y = this.position.y + _buttonMargin;
//export
var lineHeight = 0.5;//画中间的线时减掉一个线高,防止变虚
ctx.fillRect(x, y, _buttonSize.width, _buttonSize.height);
ctx.beginPath();
ctx.moveTo(x + _buttonPadding, y + _buttonPadding);
ctx.lineTo(x + _buttonPadding + (_buttonSize.width- _buttonPadding*2) - 0, y + _buttonPadding);
ctx.moveTo(x + _buttonPadding, y + _buttonPadding + (_buttonSize.height- _buttonPadding*2)/2 - lineHeight);
ctx.lineTo(x + _buttonPadding + (_buttonSize.width- _buttonPadding*2) - _buttonSize.width/6, y + _buttonPadding + (_buttonSize.height- _buttonPadding*2)/2 - lineHeight);
ctx.moveTo(x + _buttonPadding, y + _buttonPadding + (_buttonSize.height- _buttonPadding*2) - lineHeight*2);
ctx.lineTo(x + _buttonPadding + (_buttonSize.width- _buttonPadding*2) - _buttonSize.width/3, y + _buttonPadding + (_buttonSize.height- _buttonPadding*2) - lineHeight*2);
ctx.stroke();
ctx.restore();
};
}
DDMUG.ExportButton.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.ExportButton.prototype.constructor = DDMUG.EventElement;
DDMUG.TimeLine = function(parameters){
DDMUG.EventElement.call(this, parameters);
var _headerHeight = this.size.height,
_tickerHeight = parameters.tickerHeight !== undefined ? parameters.tickerHeight : 0;
this.timeScaleObj = parameters.timeScale;//timeScale对象的引用
var originalX = this.position.x;//未被移动前的x坐标,用于绘制clip path
this.bpm = 110;
this.time = 0;//当前时间
this.timelineStart = parameters.timelineStart !== undefined ? parameters.timelineStart : 0;//开始时间 s
this.timelineEnd = parameters.timelineEnd !== undefined ? parameters.timelineEnd : 60; //结束时间 s
/*矩形碰撞检测*/
this.checkCollosion = function(x, y) {
// perform hit test between bounding box and mouse coordinates
if (originalX <= x &&
originalX + this.size.width > x &&
this.position.y <= y &&
this.position.y + this.size.height > y) {
return true;
}
return false;
};
this.onClick = function(x, y, context){
if(this.checkCollosion(x, y)){
console.log('timeline');
}
}
this.updateTime = function(time){//外部通过此方法来更新time
this.time = time - time % (1.0/this.bpm);//让时间线吸附在小刻度上
}
this.draw = function(ctx){
this.update();
ctx.save();
//timeline clipping path;超出区域外的都不会显示,否则会挡住左侧的tracklabel
ctx.beginPath();
ctx.moveTo(originalX, this.position.y);
ctx.lineTo(originalX+this.size.width, this.position.y);
ctx.lineTo(originalX+this.size.width, this.position.y+_tickerHeight);
ctx.lineTo(originalX, this.position.y+_tickerHeight);
ctx.clip();
ctx.fillStyle = "#DDDDDD";
ctx.fillRect(originalX, this.position.y, this.size.width, this.size.height);
//timeline
var lastTimeLabelX = 0;
ctx.fillStyle = "#666666";
for(var sec = this.timelineStart; sec < this.timelineEnd; sec++) {
var x = this.timeToX(sec);
//绘制秒见的小间隔
var labelWidth = (this.timeToX(1)-this.timeToX(0))/this.bpm;
if(labelWidth >= 5)
for(var i=0; i<this.bpm; i++){
var offset = i*labelWidth;
drawLine(ctx, this.position.x+x+offset, this.position.y, this.position.x+x+offset, i%10==0?this.position.y+_headerHeight*0.25:this.position.y+_headerHeight*0.15, "#999999");
}
//绘制秒
drawLine(ctx, this.position.x+x, this.position.y, this.position.x+x, this.position.y+_headerHeight*0.4, "#666666");
//绘制文字label
if (x - lastTimeLabelX > 30 || lastTimeLabelX == 0) {
var minutes = Math.floor(sec / 60);
var seconds = sec % 60;
var time = minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;
var textWidth = ctx.measureText(time).width;
ctx.fillText(time, this.position.x+x - textWidth/2, this.position.y+_headerHeight*0.8);
lastTimeLabelX = x;
}
}
//time ticker
this.tickerX = this.position.x + this.timeToX(this.time);
this.tickerY = this.position.y + _tickerHeight;
drawLine(ctx, this.tickerX, this.position.y, this.tickerX, this.tickerY, "#FF0000");
ctx.restore();
};
function drawLine(ctx, x1, y1, x2, y2, color) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1+0.5, y1+0.5);
ctx.lineTo(x2+0.5, y2+0.5);
ctx.stroke();
}
this.onMouseMove = function(x, y, context){
if(this.dragging){
var t = this.xToTime(x-this.position.x);
t = Math.max(this.timelineStart, Math.min(t, this.timelineEnd));
this.updateTime(t);
//音频播放seek
context.audio.seek(this.time);
}
}
var offset = 15, mapping = 1600;
this.timeToX = function(time) {
var timeScale = this.timeScaleObj.timeScale;
return offset + time * (timeScale * mapping);//scale=1的情况下,1s代表1000px
}
this.xToTime = function(x) {
var timeScale = this.timeScaleObj.timeScale;
return (x - offset) / (timeScale * mapping);
}
}
DDMUG.TimeLine.prototype = Object.create( DDMUG.EventElement.prototype );
DDMUG.TimeLine.prototype.constructor = DDMUG.EventElement;
DDMUG.TimeScale = function(parameters){
DDMUG.EventElement.call(this, parameters);
var padding = 3;
this.timeScale = 1;//时间缩放比例0-1
this.onClick = function(x, y, context){
if(this.checkCollosion(x, y)){
//console.log('timeScale');
//this.timeScale = 0.5;
//console.log(this);
//console.log(context);
}
}
this.onMouseMove = function(x, y, pannel){
if(this.dragging){
//还原之前的scrollbar位移
var dragX = pannel.timeScrollBar.timeScrollThumbPosX;
pannel.timeScrollBar.calcThumbPosX(-dragX, pannel);
//更新timeScale
var percent = 1 - (x-this.position.x-padding) / (this.size.width - 2*padding);
this.timeScale = Math.max(0.018, Math.min(percent, 1));
//重新设置scrollbar的位移
pannel.timeScrollBar.calcThumbPosX(dragX, pannel);
}
}
this.draw = function(ctx){
ctx.save();
ctx.fillStyle = "#DDDDDD";
ctx.fillRect(this.position.x, this.position.y, this.size.width, this.size.height);
//time scale
for(var i=1; i<20; i++) { //竖线
var f = 1.0 - (i*i)/361;//疏密变化的function
var x = this.position.x + padding + f*(this.size.width - 2*padding);
drawLine(ctx, x, this.position.y + padding, x, this.position.y + this.size.height - 2*padding, "#009999");
}
ctx.fillStyle = "#666666";
ctx.beginPath();
var y = this.position.y + this.size.height - padding;
ctx.moveTo(4 + (1.0-this.timeScale)*(this.size.width - 2*padding), y - 6);
ctx.lineTo(8 + (1.0-this.timeScale)*(this.size.width - 2*padding), y);
ctx.lineTo(0 + (1.0-this.timeScale)*(this.size.width - 2*padding), y);
ctx.fill();//三角形