forked from jeruick/widget-axes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.js
2018 lines (1846 loc) · 86.7 KB
/
widget.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
/* global cprequire_test macro chilipeppr THREE $ requirejs cprequire cpdefine chilipeppr */
// Test this element. This code is auto-removed by the chilipeppr.load()
cprequire_test(["inline:com-chilipeppr-widget-xyz"], function (xyz) {
console.log("test running of " + xyz.id);
//sp.init("192.168.1.7");
//xyz.initAs3dPrinting();
xyz.init();
xyz.showBody("com-chilipeppr-widget-xyz");
xyz.currentUnits = 'mm'
// load the WCS widget to left
//$('body').css('margin-left', '80px');
var wrapDiv = $('<div class="testdiv" style="margin-left:80px;position:relative;">');
$('#com-chilipeppr-widget-xyz').wrap(wrapDiv);
// test out planner resume/pause
var testPauseResume = function () {
setTimeout(function () {
chilipeppr.publish('/com-chilipeppr-interface-cnccontroller/plannerpause', "");
}, 5000);
setTimeout(function () {
chilipeppr.publish('/com-chilipeppr-interface-cnccontroller/plannerresume', "");
}, 10000);
setTimeout(function () {
chilipeppr.publish('/com-chilipeppr-interface-cnccontroller/plannerpause', "");
}, 15000);
setTimeout(function () {
chilipeppr.publish('/com-chilipeppr-interface-cnccontroller/plannerresume', "");
}, 20000);
};
var testAxesValUpdates = function () {
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: 0.005,
y: 10.46,
z: 0.304,
a: 0,
type: "work",
mpo: {
x: 0.005,
y: 10.46,
z: 0.304,
a: 0,
type: "machine"
}
});
}, 1000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: 192,
y: 0.028,
z: 0.01,
a: 0,
type: "work"
});
}, 2000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: -3288.73,
y: -0,
z: 1.12,
a: null,
type: "work"
});
}, 3000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: 940.5744,
y: null,
z: 1.12,
a: null,
type: "work"
});
}, 4000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: 0.574,
y: 32.424,
z: -3.424,
a: null,
type: "work"
});
}, 5000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/axes", {
x: null,
y: 132.424,
z: -3.424,
a: null,
type: "work"
});
}, 5000);
};
// test units change
var testUnitsChange = function () {
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/units", "inch");
}, 1000);
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/units", "mm");
}, 5000);
};
// test units change
var testCoordsChange = function () {
setTimeout(function () {
var c = {
coord: "G54",
coordNum: 54
};
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/coords", c);
}, 1000);
setTimeout(function () {
var c = {
coord: "G55",
coordNum: 55
};
chilipeppr.publish("/com-chilipeppr-interface-cnccontroller/coords", c);
}, 5000);
};
//testPauseResume();
testAxesValUpdates();
//testUnitsChange();
testCoordsChange();
/*
setTimeout(function() {
console.log("setting up alternate keystrokes");
$('#com-chilipeppr-widget-xyz-ftr').keydown(function(evt) {
var e = $.Event('keydown');
if (evt.which == 50) {
// the #2 key
// mimic down arrow being pressed
e.which= 40;
} else if (evt.which == 56) {
// the #8 key
// mimic up arrow being pressed
e.which= 38; // enter
}
console.log("artificial event:", e);
if (e.which > 0) {
evt.preventDefault();
// fire off fake event as if key was pressed down
$('#com-chilipeppr-widget-xyz-ftr').trigger(e);
return false;
}
});
}, 5000);
*/
$('body').css("padding", "20px");
} /*end_test*/ );
cpdefine("inline:com-chilipeppr-widget-xyz", ["chilipeppr_ready", "jquerycookie"], function () {
return {
id: "com-chilipeppr-widget-xyz",
url: "(auto fill by runme.js)", // The final URL of the working widget as a single HTML file with CSS and Javascript inlined. You can let runme.js auto fill this if you are using Cloud9.
fiddleurl: "(auto fill by runme.js)", // The edit URL. This can be auto-filled by runme.js in Cloud9 if you'd like, or just define it on your own to help people know where they can edit/fork your widget
githuburl: "(auto fill by runme.js)", // The backing github repo
testurl: "(auto fill by runme.js)", // The standalone working widget so can view it working by itself
name: "Widget / XYZ Axes v2",
desc: "The Axes widget shows the XYZA values of the axes of your CNC controller. It also enables you to jog, home, change units, and change Work Coordinate Systems.",
publish: {},
subscribe: {},
foreignPublish: {
'/com-chilipeppr-widget-serialport/send': "We publish to the serial port Gcode jog commands"
},
foreignSubscribe: {
"/com-chilipeppr-interface-cnccontroller/axes": "We want X,Y,Z,A,MX,MY,MZ,MA axis updates.",
"/com-chilipeppr-interface-cnccontroller/coords": "Track which is active: G54, G55, etc.",
"/com-chilipeppr-interface-cnccontroller/plannerpause": "We need to know when to pause sending jog cmds.",
"/com-chilipeppr-interface-cnccontroller/plannerresume": "We need to know when to resume jog cmds.",
"/com-chilipeppr-interface-cnccontroller/units": "Deprecated. Not listening to this anymore. See next.",
'/com-chilipeppr-widget-3dviewer/unitsChanged': "Listenting to see if the 3D Viewer is telling us that the user Gcode is in a specific coordinate and then just assuming we will only be sent axes coordinate updates in that unit. Not using /com-chilipeppr-interface-cnccontroller/units anymore."
},
init: function () {
// Do UI setup
this.initBody();
this.btnSetup();
this.menuSetup();
this.jogSetup();
this.pencilSetup();
this.forkSetup();
this.toolbarSetup();
// Subscribe to the signal published from the specific controller implementing the generic interface
// for CNC controllers that normalizes the XYZ Axis updates so we don't have to worry about
// the specific implementation
chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/axes", this, this.updateAxesFromStatus);
// Update units if we get a notification published to us
chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/units", this, this.updateUnitsFromStatus);
chilipeppr.subscribe('/com-chilipeppr-widget-3dviewer/unitsChanged', this, this.updateUnitsFromStatus);
// in case we didn't get loaded fast enough, thus we never got the /unitsChanged event from the 3d viewer, we better double check here and fire off a request just to cover all our bases
chilipeppr.subscribe('/com-chilipeppr-widget-3dviewer/recvUnits', this, this.updateUnitsFromStatus);
chilipeppr.publish('/com-chilipeppr-widget-3dviewer/requestUnits', "");
// Subscribe to the generic interface signals of plannerresume/plannerpause so we know when to slow
// down on our sending of gcode commands when jogging.
// If a different controller is implemented, they can send on these signals
// and abstract away the specific hardware details from us so this widget is reusable
chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/plannerpause", this, this.onPlannerPause);
chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/plannerresume", this, this.onPlannerResume);
// subscribe to the CNC controller broadcasting what
// layer system we're in
chilipeppr.subscribe('/com-chilipeppr-interface-cnccontroller/coords', this.onCoordsUpdate.bind(this));
// setup onconnect pubsub event
/*
chilipeppr.subscribe("/com-chilipeppr-widget-serialport/ws/onconnect", this, function (msg) {
console.log("got onconnect so will query for status");
});
*/
// setup recv pubsub event
// this is when we receive data in a per line format from the serial port
/*
chilipeppr.subscribe("/com-chilipeppr-widget-serialport/recvline", this, function (msg) {
this.onRecvCmd(msg);
});
*/
// setup DOM elements for the Axes in the UI
this.setupAxes();
// setup controller specific init items
//this.initControllerSpecific();
// setup cookie based UI settings
this.setupUiFromCookie();
this.setupTouchArea();
this.setupShowHideTouchBtn();
this.setupShowHideWcsBtn();
var that = this;
console.log(this.name + " done loading.");
},
pencilSetup: function() {
// add mouseover events to DRO numbers
//$('#com-chilipeppr-widget-xyz-x').mouseover(this.pencilOnMouseover.bind(this));
//$('#com-chilipeppr-widget-xyz-x').mouseout(this.pencilOnMouseout.bind(this));
$('#com-chilipeppr-widget-xyz-x').hover(this.pencilOnMouseover.bind(this), this.pencilOnMouseout.bind(this));
$('#com-chilipeppr-widget-xyz-y').hover(this.pencilOnMouseover.bind(this), this.pencilOnMouseout.bind(this));
$('#com-chilipeppr-widget-xyz-z').hover(this.pencilOnMouseover.bind(this), this.pencilOnMouseout.bind(this));
$('#com-chilipeppr-widget-xyz-a').hover(this.pencilOnMouseover.bind(this), this.pencilOnMouseout.bind(this));
},
pencilOnMouseover: function(evt) {
console.log("got pencilOnMouseover. evt:", evt);
var tgtEl = $(evt.currentTarget);
var btn = $('<button class="btn btn-xs btn-default xyz-pencil"><span class="glyphicon glyphicon-pencil"></span></button>');
btn.click(this.pencilClick.bind(this));
tgtEl.find('.com-chilipeppr-xyz-pos-well').prepend(btn);
// attach descriptive popoover
btn.popover({
animation: true,
delay: 500,
placement: "auto",
container: "body",
trigger: "hover",
title: "Enter a new coordinate",
content: "Move to a new coordinate in this axis by modifying the value and hitting the enter key."
});
},
pencilOnMouseout: function(evt) {
console.log("got pencilOnMouseout. evt:", evt);
var tgtEl = $(evt.currentTarget);
this.pencilHide(tgtEl);
},
pencilClick: function(evt) {
console.log("got pencilClick. evt:", evt);
var tgtEl = $(evt.currentTarget);
var txt = $('<input type="number" class="form-control xyz-number" placeholder="Enter New Coord">');
txt.keyup(this.pencilKeypress.bind(this));
var posEl = tgtEl.parents('.com-chilipeppr-xyz-pos-well');
console.log("lastCoords:", this.lastCoords, "lastVal:", this.lastVal);
var val = this.lastVal[posEl.data('axis')];
txt.val(val);
posEl.prepend(txt);
txt.focus();
// hide popover
posEl.find('button').popover('hide');
},
pencilCtr: 0,
pencilKeypress: function(evt) {
console.log("got pencilKeypress. evt:", evt);
var tgtEl = $(evt.currentTarget);
var posEl = tgtEl.parents('.com-chilipeppr-xyz-pos-well');
var axis = posEl.data('axis').toUpperCase();
console.log("axis:", axis);
// see if return key
if (evt.keyCode == 13) {
console.log("enter key hit");
// send gcode
var gcode = "G90 G0 " + axis + tgtEl.val();
console.log("about to send gcode:", gcode);
chilipeppr.publish('/com-chilipeppr-widget-serialport/jsonSend', {
D: gcode,
Id:"axes" + this.pencilCtr++
});
this.pencilHide(tgtEl.parents('.com-chilipeppr-xyz-pos-well'));
} else if (evt.keyCode == 27) {
console.log("ESC key hit");
this.pencilHide(tgtEl.parents('.com-chilipeppr-xyz-pos-well'));
}
},
pencilHide: function(tgtEl) {
console.log("pencilHide");
// hide popover
tgtEl.find('button').popover('hide');
//tgtEl.popover('hide');
tgtEl.find('.xyz-pencil').remove();
tgtEl.find('.xyz-number').remove();
},
initAs3dPrinting: function () {
// by default we'll show the A/B/C axes
$('#com-chilipeppr-widget-xyz-a').removeClass("hidden");
$('#com-chilipeppr-widget-xyz-b').removeClass("hidden");
// change labels
$('#com-chilipeppr-widget-xyz-a .com-chilipeppr-xyz-label').text("E0");
$('#com-chilipeppr-widget-xyz-b .com-chilipeppr-xyz-label').text("E1");
// change units
$('#com-chilipeppr-widget-xyz-a .com-chilipeppr-xyz-dim').text("mm");
$('#com-chilipeppr-widget-xyz-b .com-chilipeppr-xyz-dim').text("mm");
},
setupShowHideWcsBtn: function () {
var btnEl = $("#com-chilipeppr-widget-xyz .btnToggleShowWcs");
btnEl.click(this.toggleWcs.bind(this));
btnEl.popover();
chilipeppr.load(
"#com-chilipeppr-widgetholder-wcs",
//"http://fiddle.jshell.net/Danal/4ete4691/show/light/",
"http://raw.githubusercontent.com/chilipeppr/widget-wcs/master/auto-generated-widget.html",
function () {
cprequire(["inline:com-chilipeppr-widget-wcs"], function (wcs) {
console.log("test running of " + wcs.id);
wcs.init();
});
});
},
toggleWcs: function (evt) {
$("#com-chilipeppr-widget-xyz .btnToggleShowWcs").popover('hide');
var wcsEl = $('#com-chilipeppr-widgetholder-wcs');
if (wcsEl.hasClass("hidden")) {
wcsEl.removeClass("hidden");
$('#com-chilipeppr-widget-xyz .btnToggleShowWcs').addClass("active");
} else {
wcsEl.addClass("hidden");
$('#com-chilipeppr-widget-xyz .btnToggleShowWcs').removeClass("active");
}
},
setupShowHideTouchBtn: function () {
$("#com-chilipeppr-widget-xyz .btnToggleShowTouchJog").popover();
//$( window ).resize(this.showHideTouchBtn.bind(this));
this.showHideTouchBtn();
$(window).resize(this.showHideTouchBtn.bind(this));
},
showHideTouchBtn: function () {
//console.log("should we show or hide the touch btn");
var btnEl = $("#com-chilipeppr-widget-xyz .btnShowTouchJog");
var btnParentWidth = btnEl.parent().parent().width();
var widgetWidth = $("#com-chilipeppr-widget-xyz").width();
console.log("btnParentWidth:", btnParentWidth, "widgetWidth:", widgetWidth);
//console.log("btnEl:", btnEl, "parent:", btnEl.parent(), "btnEl.parent().width()", btnEl.parent().width(), "btnEl.parent().parent().width()", btnEl.parent().parent().width());
//console.log("btnEl.parent().parent().width()", btnEl.parent().parent().width(), "btnEl.parent().parent().width()", btnEl.parent().parent().parent().parent().width());
if (btnParentWidth > widgetWidth) {
console.log("it appears the btn is being clipped");
btnEl.css('visibility', 'hidden');
btnEl.addClass("hidden");
//apply class1
} else {
//apply class2
btnEl.css('visibility', 'visible');
btnEl.removeClass("hidden");
console.log("it appears the btn is NOT being clipped");
}
},
canvas: null,
el: null,
ctx: null,
setupTouchArea: function () {
this.canvas = $('#com-chilipeppr-widget-xyz .touchpad-overlay canvas');
var tpad = $('#com-chilipeppr-widget-xyz .touchpad-overlay');
console.log("tpad:", tpad);
/*
this.canvas.width(tpad.width());
this.canvas.height(tpad.height());
this.canvas.prop({
width: tpad.width(),
height: tpad.height()
});
*/
this.el = $('#com-chilipeppr-widget-xyz .touchpad-overlay canvas')[0];
this.canvasResize();
//this.ctx = $('#com-chilipeppr-widget-xyz .touchpad-overlay canvas')[0].getContext("2d");
var that = this;
tpad.bind("touchstart", function (e) {
//console.log("about to dish touchstart evt:", e);
that.handleStart(e);
});
tpad.bind("touchend", this.handleEnd.bind(this));
tpad.bind("touchcancel", this.handleCancel.bind(this));
tpad.bind("touchleave", this.handleEnd.bind(this));
tpad.bind("touchmove", this.handleMove.bind(this));
/*
tpad.bind('touchstart', function(e){
console.log("got touch/mouse evt:", e);
that.drawCircle(ctx, e);
});
tpad.bind('touchend', function(e){
console.log("got touch/mouse evt:", e);
that.drawCircle(ctx, e);
});
*/
// setup toggle buttons
$('#com-chilipeppr-widget-xyz .btnToggleShowTouchJog').click(this.toggleTouchJog.bind(this));
$(window).resize(this.canvasResize.bind(this));
//this.toggleTouchJog();
// scrolling
tpad.bind('mousewheel DOMMouseScroll', this.onScroll.bind(this));
// mouse movements
tpad.bind("mousedown", this.onMouseDown.bind(this));
tpad.bind("mousemove", this.onMouseMove.bind(this));
tpad.bind("mouseup", this.onMouseUp.bind(this));
this.log("touch area setup");
},
toggleTouchJog: function () {
$('#com-chilipeppr-widget-xyz .btnToggleShowTouchJog').popover('hide');
var tpad = $('#com-chilipeppr-widget-xyz .touchpad-overlay');
if (tpad.hasClass("hidden")) {
tpad.removeClass("hidden");
this.canvasResize();
$('#com-chilipeppr-widget-xyz .btnToggleShowTouchJog').addClass("active");
} else {
tpad.addClass("hidden");
$('#com-chilipeppr-widget-xyz .btnToggleShowTouchJog').removeClass("active");
}
},
canvasResize: function () {
console.log("touchpad resizing");
var tpad = $('#com-chilipeppr-widget-xyz .touchpad-overlay');
this.canvas.width(tpad.width());
this.canvas.height(tpad.height());
this.canvas.prop({
width: tpad.width(),
height: tpad.height()
});
//this.el = $('#com-chilipeppr-widget-xyz .touchpad-overlay canvas')[0];
this.ctx = $('#com-chilipeppr-widget-xyz .touchpad-overlay canvas')[0].getContext("2d");
this.drawText();
},
drawText: function () {
var x = this.el.width / 2;
var y = this.el.height / 2;
this.ctx.font = '14pt "Helvetica Neue",Helvetica,Arial,sans-serif';
this.ctx.textAlign = 'center';
this.ctx.fillStyle = 'silver';
this.ctx.fillText('Touch/Mouse/Scroll', x, y - 8);
this.ctx.fillText('Jog Area', x, y + 8);
},
isMouseDown: false,
mouseLastOffset: {
x: 0,
y: 0
},
onMouseDown: function (evt) {
console.log("onMouseDown:", evt);
this.isMouseDown = true;
//inside my mouse events handler:
var target = evt.target || evt.srcElement,
rect = target.getBoundingClientRect(),
offsetX = evt.clientX - rect.left,
offsetY = evt.clientY - rect.top;
anOffsetX = offsetX;
anOffsetY = offsetY;
console.log("mouse anOffsetX:", anOffsetX, "anOffsetY:", anOffsetY);
this.mouseLastOffset.x = anOffsetX; //evt.offsetX;
this.mouseLastOffset.y = anOffsetY; //evt.offsetY;
console.log("mouseLastOffset:", this.mouseLastOffset);
this.canvas.css('cursor', 'default');
var ctx = this.ctx;
ctx.beginPath();
ctx.arc(this.mouseLastOffset.x, this.mouseLastOffset.y, 10, 0, 2 * Math.PI, true); // a circle at the start
ctx.fillStyle = 'rgba(0,0,255,0.1)';
ctx.strokeStyle = 'rgba(0,0,0,0)';
ctx.lineWidth = 0;
ctx.fill();
ctx.stroke();
},
onMouseMove: function (evt) {
if (!this.isMouseDown) {
return;
}
console.log("onMouseMove:", evt);
var target = evt.target || evt.srcElement,
rect = target.getBoundingClientRect(),
offsetX = evt.clientX - rect.left,
offsetY = evt.clientY - rect.top;
anOffsetX = offsetX;
anOffsetY = offsetY;
console.log("mouse anOffsetX:", anOffsetX, "anOffsetY:", anOffsetY);
var deltax = anOffsetX - this.mouseLastOffset.x;
var deltay = (anOffsetY - this.mouseLastOffset.y) * -1;
console.log("deltax:", deltax, "deltay", deltay);
var newpos = {
x: anOffsetX,
y: anOffsetY
};
//this.sendMove(0, this.mouseLastOffset, {x:deltax, y:deltay});
this.sendMove(0, this.mouseLastOffset, newpos);
var ctx = this.ctx;
ctx.moveTo(this.mouseLastOffset.x, this.mouseLastOffset.y);
ctx.lineTo(newpos.x, newpos.y);
ctx.lineWidth = 8;
ctx.strokeStyle = 'rgba(0,0,255,0.1)';
ctx.stroke();
this.mouseLastOffset = newpos;
},
onMouseUp: function (evt) {
console.log("onMouseUp:", evt);
this.isMouseDown = false;
var target = evt.target || evt.srcElement,
rect = target.getBoundingClientRect(),
offsetX = evt.clientX - rect.left,
offsetY = evt.clientY - rect.top;
anOffsetX = offsetX;
anOffsetY = offsetY;
console.log("mouse anOffsetX:", anOffsetX, "anOffsetY:", anOffsetY);
var ctx = this.ctx;
ctx.lineWidth = 14;
ctx.fillStyle = 'rgba(0,0,255,0.2)';
ctx.beginPath();
ctx.moveTo(this.mouseLastOffset.x, this.mouseLastOffset.y);
ctx.lineTo(anOffsetX, anOffsetY);
ctx.fillRect(anOffsetX - 9, anOffsetY - 9, 18, 18); // and a square at the end
this.sendDone();
this.fadeCanvas();
},
scrollPrev: {
x: 0,
y: 0
},
scrollFadeTimer: null,
scrollLastPosDir: "up",
onScroll: function (evt) {
console.log("onScroll:", evt.originalEvent); //, evt.originalEvent.wheelDelta);
evt.preventDefault();
// fix for firefox. detect DOMMouseScroll
if ("type" in evt && evt.type.match(/^D/)) {
console.log("detected firefox.");
evt.originalEvent.wheelDelta = evt.originalEvent.detail * -10;
evt.originalEvent.wheelDeltaX = 0;
evt.originalEvent.wheelDeltaY = 0;
if (evt.originalEvent.axis == 2) {
// left/right scroll
evt.originalEvent.wheelDeltaY = evt.originalEvent.wheelDelta;
} else {
evt.originalEvent.wheelDeltaX = evt.originalEvent.wheelDelta;
}
}
// see if user changed positions. if so, cancel all moves.
//if (evt.originalEvent.wheelDelta /120 > 0) {
if (evt.originalEvent.wheelDelta > 0) {
console.log('scrolling up !');
if (this.scrollLastPosDir != "up") {
this.sendDone();
this.scrollLastPosDir = "up";
}
} else {
console.log('scrolling down !');
if (this.scrollLastPosDir != "dn") {
this.sendDone();
this.scrollLastPosDir = "dn";
}
}
var newpos = {
x: evt.originalEvent.wheelDeltaX,
y: evt.originalEvent.wheelDeltaY
};
// divide by 10 to slow down to sort of match touch/mouse
//newpos.x = newpos.x / 10;
//newpos.y = newpos.y / 10;
this.sendMove(0, {
x: 0,
y: 0
}, {
x: newpos.x / 10,
y: newpos.y / 10
});
var x = this.el.width / 2;
var y = this.el.height / 2;
var ctx = this.ctx;
ctx.beginPath();
var moveTo = {
x: this.scrollPrev.x + x,
y: this.scrollPrev.y + y
};
moveTo = {
x: x,
y: y
};
console.log("moveTo:", moveTo);
ctx.moveTo(moveTo.x, moveTo.y);
var lineTo = {
x: moveTo.x + newpos.x,
y: moveTo.y + newpos.y
};
// logarithmically adjust
//var xSign = -1 ? lineTo.x < 0 : 1;
//lineTo.x = Math.log(Math.abs(lineTo.x)) * xSign;
if (newpos.y !== 0) {
var ySign = newpos.y < 0 ? -1 : 1;
lineTo.y = (Math.log(Math.abs(newpos.y)) * ySign * 25) + y;
}
if (newpos.x !== 0) {
var xSign = newpos.x < 0 ? -1 : 1;
lineTo.x = (Math.log(Math.abs(newpos.x)) * xSign * 25) + x;
}
console.log("lineTo:", lineTo);
ctx.lineTo(lineTo.x, lineTo.y);
ctx.lineWidth = 32;
ctx.strokeStyle = 'rgba(0,0,0,0.025)';
ctx.stroke();
this.scrollPrev.x += newpos.x;
this.scrollPrev.y += newpos.y;
var that = this;
if (this.scrollFadeTimer) {
clearTimeout(this.scrollFadeTimer);
that.scrollPrev.x = 0;
that.scrollPrev.y = 0;
}
this.scrollFadeTimer = setTimeout(function () {
that.scrollPrev.x = 0;
that.scrollPrev.y = 0;
that.ctx.clearRect(0, 0, that.el.width, that.el.height);
that.drawText();
}, 100);
//this.fadeCanvas();
},
ongoingTouches: [], // new Array;
start: {
x: 0,
y: 0
},
inZMode: false,
handleStart: function (evt) {
console.log("touchstart. evt:", evt);
var el = this.el; //= document.getElementsByTagName("canvas")[0];
var ctx = this.ctx; // el.getContext("2d");
var touches = evt.originalEvent.changedTouches;
var offset = this.findPos(el);
for (var i = 0; i < touches.length; i++) {
if (touches[i].clientX - offset.x > 0 && touches[i].clientX - offset.x < parseFloat(el.width) && touches[i].clientY - offset.y > 0 && touches[i].clientY - offset.y < parseFloat(el.height)) {
evt.preventDefault();
this.log("touchstart:" + i + "...");
this.ongoingTouches.push(this.copyTouch(touches[i]));
var color = this.colorForTouch(touches[i]);
ctx.beginPath();
ctx.arc(touches[i].clientX - offset.x, touches[i].clientY - offset.y, 14, 0, 2 * Math.PI, false); // a circle at the start
ctx.fillStyle = color;
ctx.fill();
this.log("touchstart:" + i + ".");
}
}
},
//divider: 10,
sendCtr: 0,
sendMove: function (touchid, prevpos, newpos) {
var deltax = newpos.x - prevpos.x;
var deltay = newpos.y - prevpos.y;
if (deltax === 0 && deltay === 0) {
console.log("no move happened. returning.");
return;
}
var gcode = "G91 G0 ";
if (deltax !== 0) {
gcode += "X" + (deltax * this.accelBaseval).toFixed(3) + " ";
}
if (deltay !== 0) {
gcode += "Y" + (deltay * this.accelBaseval * -1).toFixed(3) + " ";
}
gcode += "\nG90\n";
//chilipeppr.publish("/com-chilipeppr-widget-serialport/send", gcode);
var jsonSend = {
D: gcode,
Id: "jog" + this.sendCtr
};
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", jsonSend);
this.sendCtr++;
if (this.sendCtr > 999999) this.sendCtr = 0;
},
sendDone: function () {
chilipeppr.publish("/com-chilipeppr-widget-serialport/send", "!\n%\n");
setTimeout(function () {
chilipeppr.publish("/com-chilipeppr-widget-serialport/send", "%\n");
}, 200);
},
sendMoveZ: function (touchid, prevpos, newpos) {
//var deltax = newpos.x - prevpos.x;
var deltaz = newpos.y - prevpos.y;
if (deltaz === 0) {
console.log("no z move happened. returning.");
return;
}
var gcode = "G91 G0 ";
gcode += "Z" + (deltaz * this.accelBaseval) + " ";
gcode += "\nG90\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/send", gcode);
},
handleMove: function (evt) {
var el = this.el; //document.getElementsByTagName("canvas")[0];
var ctx = this.ctx; //el.getContext("2d");
var touches = evt.originalEvent.changedTouches;
var offset = this.findPos(el);
for (var i = 0; i < touches.length; i++) {
if (touches[i].clientX - offset.x > 0 && touches[i].clientX - offset.x < parseFloat(el.width) && touches[i].clientY - offset.y > 0 && touches[i].clientY - offset.y < parseFloat(el.height)) {
evt.preventDefault();
var color = this.colorForTouch(touches[i]);
var idx = this.ongoingTouchIndexById(touches[i].identifier);
if (idx >= 0) {
//this.log("continuing touch " + idx);
ctx.beginPath();
//this.log("ctx.moveTo(" + this.ongoingTouches[idx].clientX + ", " + this.ongoingTouches[idx].clientY + ");");
var prevpos = {
x: this.ongoingTouches[idx].clientX - offset.x,
y: this.ongoingTouches[idx].clientY - offset.y
};
ctx.moveTo(prevpos.x, prevpos.y);
//ctx.moveTo(this.ongoingTouches[idx].clientX-offset.x, this.ongoingTouches[idx].clientY-offset.y);
//this.log("ctx.lineTo(" + touches[i].clientX + ", " + touches[i].clientY + ");");
var newpos = {
x: touches[i].clientX - offset.x,
y: touches[i].clientY - offset.y
};
ctx.lineTo(newpos.x, newpos.y);
//ctx.lineTo(touches[i].clientX-offset.x, touches[i].clientY-offset.y);
ctx.lineWidth = 12;
ctx.strokeStyle = color;
ctx.stroke();
if (idx === 0) this.sendMove(idx, prevpos, newpos);
//if (idx == 1) this.sendMoveZ(idx, prevpos, newpos);
this.ongoingTouches.splice(idx, 1, this.copyTouch(touches[i])); // swap in the new touch record
//this.log(".");
} else {
this.log("can't figure out which touch to continue");
}
}
}
},
handleEnd: function (evt) {
console.log("touchend/touchleave. evt:", evt);
var el = this.el; //document.getElementsByTagName("canvas")[0];
var ctx = this.ctx; //el.getContext("2d");
var touches = evt.originalEvent.changedTouches;
var offset = this.findPos(el);
for (var i = 0; i < touches.length; i++) {
if (touches[i].clientX - offset.x > 0 && touches[i].clientX - offset.x < parseFloat(el.width) && touches[i].clientY - offset.y > 0 && touches[i].clientY - offset.y < parseFloat(el.height)) {
evt.preventDefault();
var color = this.colorForTouch(touches[i]);
var idx = this.ongoingTouchIndexById(touches[i].identifier);
if (idx >= 0) {
ctx.lineWidth = 14;
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(this.ongoingTouches[idx].clientX - offset.x, this.ongoingTouches[idx].clientY - offset.y);
ctx.lineTo(touches[i].clientX - offset.x, touches[i].clientY - offset.y);
ctx.fillRect(touches[i].clientX - 14 - offset.x, touches[i].clientY - 14 - offset.y, 18, 18); // and a square at the end
this.ongoingTouches.splice(i, 1); // remove it; we're done
} else {
this.log("can't figure out which touch to end");
}
}
}
this.ongoingTouches = [];
this.sendDone();
this.fadeCanvas();
},
handleCancel: function (evt) {
evt.preventDefault();
this.log("touchcancel.");
var touches = evt.originalEvent.changedTouches;
for (var i = 0; i < touches.length; i++) {
this.ongoingTouches.splice(i, 1); // remove it; we're done
}
},
colorForTouch: function (touch) {
var r = touch.identifier % 16;
var g = Math.floor(touch.identifier / 3) % 16;
var b = Math.floor(touch.identifier / 7) % 16;
r = r.toString(16); // make it a hex digit
g = g.toString(16); // make it a hex digit
b = b.toString(16); // make it a hex digit
var color = "#" + r + g + b;
if (touch.identifier === 0) color = 'rgba(0,0,0,0.35)'; //"#dddddd";
if (touch.identifier === 1) color = 'rgba(200,200,200,0.25)'; //"#dddddd";
if (touch.identifier === 2) color = 'rgba(255,0,0,0.25)';
//this.log("color for touch with identifier " + touch.identifier + " = " + color);
return color;
},
copyTouch: function (touch) {
return {
identifier: touch.identifier,
clientX: touch.clientX,
clientY: touch.clientY
};
},
ongoingTouchIndexById: function (idToFind) {
for (var i = 0; i < this.ongoingTouches.length; i++) {
var id = this.ongoingTouches[i].identifier;
if (id == idToFind) {
return i;
}
}
return -1; // not found
},
log: function (msg) {
console.log(msg);
//var p = document.getElementById('log');
//p.innerHTML = msg + "\n" + p.innerHTML;
},
findPos: function (obj) {
var curleft = 0,
curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return {
x: curleft - document.body.scrollLeft,
y: curtop - document.body.scrollTop
};
}
},
lastImage: null,
fadeCanvas: function () {
//console.log("fadeCanvas. this.el:", this.el);
this.lastImage = this.ctx.getImageData(0, 0, this.el.width, this.el.height);
this.fadeCtr = 0;
this.fadeCanvasStep();
},
fadeCtr: 0,
fadeCanvasStep: function () {
//console.log("fadeCanvasStep");
var pixelData = this.lastImage.data;
var len = pixelData.length;
for (var i = 3; i < len; i += 4) {
pixelData[i] -= 50;
}
this.ctx.putImageData(this.lastImage, 0, 0);
//this.drawText();
this.fadeCtr++;
if (this.fadeCtr >= 255 / 50) {
this.drawText();
//console.log("done fading");
return;
}
setTimeout(this.fadeCanvasStep.bind(this), 50);
},
drawCircle: function (ctx, e) {
var x, y;
if (e.type.match(/touch/)) {
x = e.originalEvent.changedTouches[0].clientX;
y = e.originalEvent.changedTouches[0].clientY;
} else {
x = e.offsetX;
y = e.offsetY;
}
console.log("drawCircle. x:", x, "y:", y);
ctx.beginPath();
ctx.arc(x, y, 10, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
},
toolbarSetup: function () {
// config the css sizes to get more compact display
var config = localStorage.getItem("/" + this.id + "/size");
if (config == "small") this.bodyShowSmall();
var that = this;
$('#com-chilipeppr-widget-xyz .view-small').click(function () {
that.bodyShowSmall();
localStorage.setItem("/" + that.id + "/size", "small");
});
$('#com-chilipeppr-widget-xyz .view-large').click(function () {
that.bodyShowNormal();
localStorage.setItem("/" + that.id + "/size", "normal");
});
},
bodyShowSmall: function () {
$('#com-chilipeppr-widget-xyz').addClass("size-small");
$('#com-chilipeppr-widget-xyz .view-small').addClass("active");
$('#com-chilipeppr-widget-xyz .view-large').removeClass("active");
},
bodyShowNormal: function () {
$('#com-chilipeppr-widget-xyz').removeClass("size-small");
$('#com-chilipeppr-widget-xyz .view-small').removeClass("active");
$('#com-chilipeppr-widget-xyz .view-large').addClass("active");
},
options: null,
setupUiFromCookie: function () {
// read vals from cookies
var options = $.cookie('com-chilipeppr-widget-xyz-options');
if (true && options) {
options = $.parseJSON(options);
console.log("just evaled options: ", options);
} else {
options = {
showA: false,
moveBy: 0.01
};
}
this.options = options;
console.log("options:", options);
// hilite the correct button
var cls = ".jogincr1";
if (options.moveBy == "0.1") cls = ".jogincrpt1";
if (options.moveBy == "0.01") cls = ".jogincrpt01";
if (options.moveBy == "0.001") cls = ".jogincrpt001";
this.changeBaseVal({
data: {