Check-in [b454ce1c79]
Not logged in
Overview
Comment:adjusted sizes of center and axis squares.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b454ce1c7923771bfe316f4e19b5da5b6a8f8e55
User & Date: Derek on 2012-12-24 19:03:36
Other Links: manifest | tags
Context
2012-12-27
20:24
started working on some changes for rotating and scaling selected points. check-in: 29f4bb3e29 user: Derek tags: trunk
2012-12-24
19:03
adjusted sizes of center and axis squares. check-in: b454ce1c79 user: Derek tags: trunk
17:44
improved axes prespective, changed motion speed base. check-in: fb2f9f8895 user: Derek tags: trunk
Changes

Modified draw.html from [936850004e] to [a746033bd0].

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
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







-
-

-

-
-

-

-
-



-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






-
-
-
-





-
+
-

+

+
+







<html>
<script src='matrix_vector_lib.js' ></script>

<script>

//  TODO rotate selected points
//  TODO control with zoom rotate or pan means that action is only applied to the camera not the selected points.


//  TODO clicking and dragging on a line allows you to place a point somewhere on that line.  Perhaps with the modifier key it would break up the existing line into line segments.

//  TODO when lines are deleted, readd the solo points to the solo_points list
//  

//  TODO z for undo shift + z for redo.
//
//  TODO 
//

//  TODO drop down minimalist menu in top right of canvas.


//  drag to connect points

//
//
//  Global variables
//
//  use longer more descriptive names, especially for obscure variables, to avoid conflict in the global space,
//  even if certain properties are factored into objects, if it is a large object, they work similar to globals in that exist in a large namespace accessible from many functions.
//
//  All caps indicates a constant
//  Some lower case variables may also be constants
//  Capitalized constants indicate that we expect that variable to remain a constant
//  even in future code changes, ie statically configured with an initial.
//
//



var canvas;
var origin = [0, 0, 0];
var frame_rate = 20;
var saveTimeout = 10*1000;

var forward_axis_size = 0.1;
var vertical_axis_size = 0.05;      




// to prevent camera from rotating beyond straight vertical
var vertical_camera_rotation = 0;
var zoom_dist = 1;

var zoom_factor = 0.83;
var zoom_scale = 2.667;  // scalar zoom property.
var zoom_dist = 1;
var last_zoom_dist = 1;
var zoom_factor = 0.83;



// camera animation
var max_move_delta = 0.01;
var max_angle_delta = Math.PI;

var delta_horizontal_angle = 0;
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){