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
|
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
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
var point_projections = [];
var line_midpoint_projections = [];
var highlight_object = null;
var mouse_dragging = false;
var last_mouse_down = null;
var mouse_loc = null;
var MIN_DRAG_DIST = 10;
var POINT_HIGHLIGHT_DIST = 10;
var LINE_HIGHLIGHT_DIST = 15;
var MOTION_SPEED_BASE = 2.8;
var selected_points = [];
var selected_lines = [];
var msg = //"3D drawing tool\n\n" +
"Select Left Click\n" +
"Rotate Right Click\n" +
"Move wasd, qe\n" +
"Copy Space\n" +
"Delete x\n" +
"\n" +
"Click the red dot to create a point\n" +
"Spam a motion key to increase speed\n";
var helpmsg = //"3D drawing tool\n\n" +
"Select Left Click\n" +
"Rotate Right Click\n" +
"Move wasd, qe\n" +
"Copy Space\n" +
"Delete x\n" +
"\n" +
"Click the red dot to create a point\n" +
"Drag between two points to connect them" +
"Spam a motion key to increase speed\n";
var msg = helpmsg;
var view_transform =
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]];
var key_state = {};
var key_state_augmentation_timeout = 150; // the permissible time in milliseconds since the last key event to still allow the key state to augment instead of clearing.
// constants
var MIN_DRAG_DIST = 10;
var POINT_HIGHLIGHT_DIST = 10;
var LINE_HIGHLIGHT_DIST = 15;
var MOTION_SPEED_BASE = 2.8;
var CENTER_POINT_COLOR = "rgba(250, 0, 0, 1.0)";
var AXIS_POINT_COLOR = "rgba(0, 0, 250, 1.0)";
var CENTER_POINT_RADIUS = 2.5;
var MIN_AXIS_POINT_RADIUS = 1;
var MAX_AXIS_POINT_RADIUS = 4;
var AXIS_LINE_WIDTH = 1.333;
var FORWARD_AXIS_LEN = 0.15;
var VERTICAL_AXIS_LEN = 0.075;
// The point, line, and fill colors are the same for a reason.
// Because all the colors are the same we don't have to worry about occlusion and z-order,
// because no matter which order colors are painted onto a pixel, the final result will be the same.
// Observe that this assertion holds even with a color with an alpha channel.
// This helps us use our 2d canvas happily and not have to worry about draw order or non-sensical visual effects from wrong order.
// With transparency we can still see each individual object and it almost mimics occlusion.
// This is meant to be a lightweight drawing tool, not a full featured one, so this decision is limiting but fits our goals perfectly.
var POINT_COLOR = "rgba(0, 0, 0, 0.9)";
var LINE_COLOR = "rgba(0, 0, 0, 0.9)";
var FILL_COLOR = "rgba(0, 0, 0, 0.9)";
var LINE_WIDTH = 1;
function addKeyListener(keycode, func){
if(!key_state[keycode]) key_state[keycode] = {};
var key_obj = key_state[keycode];
if(!key_obj.listeners) key_obj.listeners = [];
|
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
|
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
|
+
-
-
+
+
-
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
+
-
-
-
+
+
-
+
-
+
-
+
|
ctx.clearRect(0, 0, w, h);
ctx.strokeStyle = "rgba(0, 0, 0, 0.9)";
ctx.stroke();
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
for(var k in solo_points){
var pt = point_projections[k];
if(pt) ctx.fillRect(pt[0] - 2, pt[1] - 2, 4, 4); }
//
// The following drawing of axes is to help provide an perspective
// The nuances of this draw behavior are created to provide visual cues for interpreting perspective and depth.
//
// Because we use a 2d canvas library for the drawing the rest of the points/shapes after projection, these visual information nuances are important.
//
// axis lines and dots
var faxis_pt = project(canvas, vector_add(origin, [0, 0, forward_axis_size]), view_transform, origin);
var vaxis_pt = project(canvas, vector_add(origin, [0, -vertical_axis_size, 0]), view_transform, origin);
var faxis_pt = project(canvas, vector_add(origin, [0, 0, FORWARD_AXIS_LEN]), view_transform, origin);
var vaxis_pt = project(canvas, vector_add(origin, [0, -VERTICAL_AXIS_LEN, 0]), view_transform, origin);
ctx.fillStyle = "rgba(0,0,0,1.0)";
ctx.fillStyle = AXIS_POINT_COLOR;
var fdot = vector_dot(view_transform[2], [0, 0, 1]);
var a = MIN_AXIS_POINT_RADIUS; //random letters for intermediate computations
var b = MAX_AXIS_POINT_RADIUS;
var c = (a+b)/2;
var d = (b-a)/2;
var fr = 4 - 2.5 * fdot;
var fr = c - d * fdot;
var vdot = vector_dot(view_transform[2], [0, -1, 0]);
var vr = 4 - 2.5 * vdot;
var vr = c - d * vdot;
ctx.lineWidth = 1.333;
ctx.lineWidth = AXIS_LINE_WIDTH;
ctx.beginPath();
if(fdot >=0 && faxis_pt){ // occlude this axis, draw first
ctx.moveTo(w/2, h/2);
ctx.lineTo(faxis_pt[0], faxis_pt[1]);
ctx.fillRect(faxis_pt[0] - fr/2, faxis_pt[1] - fr/2, fr, fr); }
ctx.fillRect(faxis_pt[0] - fr, faxis_pt[1] - fr, 2*fr, 2*fr); }
if(vdot >=0 && vaxis_pt){ // occlude this axis, draw first
ctx.moveTo(w/2, h/2);
ctx.lineTo(vaxis_pt[0], vaxis_pt[1]);
ctx.fillRect(vaxis_pt[0] - vr/2, vaxis_pt[1] - vr/2, vr, vr); }
ctx.fillRect(vaxis_pt[0] - vr, vaxis_pt[1] - vr, 2*vr, 2*vr); }
ctx.stroke()
ctx.stroke();
// draw center red square
ctx.fillStyle = "rgba(250, 0, 0, 1.0)";
ctx.fillRect(w/2 - 3, h/2 - 3, 6, 6);
ctx.fillStyle = CENTER_POINT_COLOR;
var cr = CENTER_POINT_RADIUS;
ctx.fillRect(w/2 - cr, h/2 - cr, 2*cr, 2*cr);
ctx.fillStyle = "rgba(0,0,0,1.0)";
ctx.beginPath();
ctx.fillStyle = AXIS_POINT_COLOR;
if(fdot < 0 && faxis_pt){
ctx.beginPath();
ctx.moveTo(w/2, h/2);
ctx.lineTo(faxis_pt[0], faxis_pt[1]);
ctx.fillRect(faxis_pt[0] - fr/2, faxis_pt[1] - fr/2, fr, fr); }
ctx.lineTo(faxis_pt[0], faxis_pt[1]);
ctx.stroke();
ctx.fillRect(faxis_pt[0] - fr, faxis_pt[1] - fr, 2*fr, 2*fr); }
if(vdot < 0 && vaxis_pt){
ctx.beginPath();
ctx.moveTo(w/2, h/2);
ctx.lineTo(vaxis_pt[0], vaxis_pt[1]);
ctx.fillRect(vaxis_pt[0] - vr/2, vaxis_pt[1] - vr/2, vr, vr); }
ctx.stroke();
ctx.stroke();
ctx.fillRect(vaxis_pt[0] - vr, vaxis_pt[1] - vr, 2*vr, 2*vr); }
// reset line width
ctx.lineWidth = 1;
ctx.lineWidth = LINE_WIDTH;
for(var i = 0; i < selected_points.length; ++i){
var pt;
if(selected_points[i] == -1)
pt = [w/2, h/2];
else pt = point_projections[selected_points[i]];
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
ctx.fillStyle = POINT_COLOR;
if(pt) ctx.fillRect(pt[0] - 4, pt[1] - 4, 8, 8); }
if(highlight_object !== null){
if(highlight_object < point_projections.length){
var pt;
if(highlight_object == -1)
pt = [w/2, h/2];
else pt = point_projections[highlight_object];
ctx.fillStyle = "rgba(50, 50, 50, .6)";
ctx.fillRect(pt[0] - 4, pt[1] - 4, 8, 8);
ctx.fillRect(pt[0] - 4, pt[1] - 4, 8, 8); // TODO getting a error here that pt is null
document.body.style.cursor = "hand"; }
else if(highlight_object < point_projections.length + line_midpoint_projections.length){
// alert("drawing highlighted line");
var line = lines[highlight_object - point_projections.length];
if(line){
|