Diff
Not logged in

Differences From Artifact [d50a5e8508]:

To Artifact [97f687849d]:


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





-
+









+
+













+
+
-
-
-
+
+
+

+
+
+
+
+
+
-
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+







<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


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;      
var no_rotate_area = 0.80;
            
            




// 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 last_zoom_dist = 1;
            


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

var delta_horizontal_angle = 0;
var delta_vertical_angle = 0;
var delta_position = [0,0,0];


//  Use to rotate selected points about a vector line
var selection_rotation_vector = null;
var rotation_center = origin.slice(0);





var points = []; //global
var solo_points = {};
var lines = [];

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 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 view_transform =
    [[1, 0, 0],
     [0, 1, 0],
     [0, 0, 1]];
    







            

var key_state = {};
var key_state_augmentation_delay = 150; // the permissible time in milliseconds since the last key event to still allow the key state to augment instead of clearing.
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.

function addKeyListener(keycode, func){
    if(!key_state[keycode]) key_state[keycode] = {};
    
    var key_obj = key_state[keycode];
    if(!key_obj.listeners) key_obj.listeners = [];
    
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
132
133
134
135
136
137
138

139
140
141
142
143
144
145
146







-
+







    var state = key_obj.state;
    if(state) return; //key is already pressed.
    
    state = 0;
    
    var latest = key_obj.latest;

    if(latest && time - latest < key_state_augmentation_delay)
    if(latest && time - latest < key_state_augmentation_timeout)
        state = key_obj.state = key_obj.lastState + 1;
    else
        state = key_obj.state = 1;
        
    key_obj.latest = time;
    
    key_obj.lastState = state;
146
147
148
149
150
151
152
153
154


155
156
157
158
159
160
161
162
218
219
220
221
222
223
224


225
226

227
228
229
230
231
232
233







-
-
+
+
-







    if(!positive) delta = -delta;
    
    var velocityChange = [0, 0, 0];
    velocityChange[direction] = delta;
    vector_add(delta_position, velocityChange, delta_position); }
    

//
//  This 
	

//
    
function leftPress(e, s, lasts){
    changeVelocity(0, false, s, lasts); }
    
function rightPress(e, s, lasts){
     changeVelocity(0, true, s, lasts); }
     
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
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







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


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







				
				if(minx < _x && _x < maxx
				 && miny < _y && _y < maxy){
				   selected_points.push(i); }}}                
        }});





// right click
addKeyListener(1002, function(e, s, lasts){
	if(s == 0){
// to prevent camera from rotating beyond straight vertical
var vertical_camera_rotation = 0;
var NAVIGATION_ZOOM_DIST = 0.01;
var zoom_dist = 1;
var zoom_factor = 0.83;
var zoom_scale = 2.667;  // scalar zoom property.
var last_zoom_dist = 1;


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

var delta_horizontal_angle = 0;
var delta_vertical_angle = 0;
		delta_horizontal_angle = 0;
		delta_vertical_angle = 0; }});
var delta_position = [0,0,0];



function writeMsg(canvas, msg){


var points = []; //global
var solo_points = {};
var lines = [];

	var lines = msg.split("\n");
	var ctx = canvas.getContext("2d");
var point_projections = [];
var line_midpoint_projections = [];

	ctx.font = "9pt sans-serif"
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;

	for(var i = 0; i < lines.length; ++i){
var selected_points = [];
var selected_lines = [];

		ctx.fillText(lines[i], 5, 10 + 15 *  i); }}
var msg = "3D drawing tool";


var view_transform =
    [[1, 0, 0],
     [0, 1, 0],
     [0, 0, 1]];
    

    
//  camera functions

function rotateView(norm, theta, cameracentric){

    // camera centric uses the camera axes to perform the rotation instead of the space axes.   
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
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







-
-
-
+
-
-
-
-
-

-
+


+





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


+







        var x1 = _pt1[0];
        var y1 = _pt1[1];
        
        ctx.moveTo(x0, y0);
        ctx.lineTo(x1, y1); }
    
    ctx.clearRect(0, 0, w, h);
    var hoffset = h * (1 - no_rotate_area)/ 2;
    var woffset = w * (1 - no_rotate_area)/ 2;
    
	
    ctx.fillStyle = "rgba(128, 128, 128, 0.05)";
    ctx.fillRect(0, 0, w, hoffset);
    ctx.fillRect(0, hoffset, woffset, h - 2 * hoffset);
    ctx.fillRect(w - woffset, hoffset, woffset, h - 2 * hoffset);
    ctx.fillRect(0, h - hoffset, w, hoffset);
    
    ctx.fillStyle = "rgb(0, 0, 128)";
	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); }
        
    
    
    

	// axis lines and dots
	ctx.beginPath();
    var faxis_pt = project(canvas, vector_add(origin, [0, 0, forward_axis_size, 0, 0]), view_transform, origin);
	var vaxis_pt = project(canvas, vector_add(origin, [0, -vertical_axis_size, 0]), view_transform, origin);
	
	ctx.fillStyle = "rgba(0,0,0,0.9)";
	
	if(faxis_pt){
		ctx.moveTo(w/2, h/2);
		ctx.lineTo(faxis_pt[0], faxis_pt[1]);
		ctx.fillRect(faxis_pt[0] - 2, faxis_pt[1] - 2, 4, 4); }
		
	if(vaxis_pt){
		ctx.moveTo(w/2, h/2);
		ctx.lineTo(vaxis_pt[0], vaxis_pt[1]);
		ctx.fillRect(vaxis_pt[0] - 2, vaxis_pt[1] - 2, 4, 4); }
		
	ctx.stroke();	
	
	

    // red center dot.
	ctx.fillStyle = "rgba(250, 0, 0, 0.9)";
	ctx.fillRect(w/2 - 2, h/2 - 2, 4, 4);
		
	
	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]];
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
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







-
+

-
-
-
-
-
+
+
+
+

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



-
+







		
	else{
		document.body.style.cursor = "crosshair"; }
	
	if(mouse_dragging){
	
		if(selected_points.length && (!key_state[16] || !key_state[16].state)){  // if shift is held, we are selecting more points.
			if(mouse_loc){
			if(mouse_loc && key_state[1000] && key_state[1000].state){
				ctx.beginPath();
				for(var i = 0; i < selected_points.length; ++i){
					var pt = point_projections[selected_points[i]];
					if(pt){
						ctx.moveTo(mouse_loc[0], mouse_loc[1]);
						ctx.lineTo(pt[0], pt[1]); }}
				var pt = point_projections[selected_points[0]];
				if(pt){
					ctx.moveTo(mouse_loc[0], mouse_loc[1]);
					ctx.lineTo(pt[0], pt[1]); }
				ctx.stroke(); }}
		
		else if(mouse_loc && last_mouse_down){  // select points inside square            
			var minx = Math.min(mouse_loc[0], last_mouse_down[0]);
			var maxx = Math.max(mouse_loc[0], last_mouse_down[0]);
			var miny = Math.min(mouse_loc[1], last_mouse_down[1]);
			var maxy = Math.max(mouse_loc[1], last_mouse_down[1]);
			ctx.beginPath();
			ctx.rect(minx, miny, maxx-minx, maxy-miny);
			ctx.stroke(); }
				
		else if(mouse_loc && last_mouse_down){  
		
		
		    if(key_state[1000] && key_state[1000].state){     // select points inside square
				var minx = Math.min(mouse_loc[0], last_mouse_down[0]);
				var maxx = Math.max(mouse_loc[0], last_mouse_down[0]);
				var miny = Math.min(mouse_loc[1], last_mouse_down[1]);
				var maxy = Math.max(mouse_loc[1], last_mouse_down[1]);
				ctx.beginPath();
				ctx.rect(minx, miny, maxx - minx, maxy - miny);
				ctx.stroke(); }}
	}
    
    ctx.fillStyle = "rgb(0,0,0)";
    ctx.fillText(msg, 5, 20); }
	writeMsg(canvas, msg); }

    
    
window.onload = function(){

    // load localStorage saved state.
    if(localStorage.points && localStorage.lines){
660
661
662
663
664
665
666
667

668
669
670
671
672
673
674
675
676
712
713
714
715
716
717
718

719
720

721
722
723
724
725
726
727







-
+

-







        canvas.width = window.innerWidth - 20;
        canvas.height = window.innerHeight - 20; }

    
    
    function getRotationAngle(x, size, max, center_size){

        if(!center_size) center_size = 0.25;  // the proportional size of the area in which no rotation is effected by the mouse movement
        if(!center_size) center_size = 0.05;  // the proportional size of the area in which no rotation is effected by the mouse movement
        if(!max) max = Math.PI/128;
        x -= size/2;
        
        //  center does nothing
        if(Math.abs(x) < size * center_size/2) x = 0;
        else if(x < 0) x += size * center_size/2;
        else           x -= size * center_size/2;
        
        return max * x / ((1 - center_size) * size/2); }
695
696
697
698
699
700
701

702
703
704




705
706
707
708
709
710
711
746
747
748
749
750
751
752
753



754
755
756
757
758
759
760
761
762
763
764







+
-
-
-
+
+
+
+







            var dist2 = _x*_x + _y*_y;
            
            if(dist2 > MIN_DRAG_DIST * MIN_DRAG_DIST){
                mouse_dragging = true; }}
                

				
	    if(key_state[1002] && key_state[1002].state && last_mouse_down){
		delta_horizontal_angle = getRotationAngle(x, w, max_angle_delta/frame_rate, no_rotate_area);
		delta_vertical_angle = -getRotationAngle(y, h, max_angle_delta/frame_rate, no_rotate_area);
		
			delta_horizontal_angle = getRotationAngle(x - last_mouse_down[0], w, max_angle_delta/frame_rate, 0);
			delta_vertical_angle = -getRotationAngle(y - last_mouse_down[1], h, max_angle_delta/frame_rate, 0); }
			
			
		var min_point_dist2 = LINE_HIGHLIGHT_DIST * LINE_HIGHLIGHT_DIST;
		highlight_object = null;
		
		var _x = canvas.width/2 - x;
		var _y = canvas.height/2 - y;
		var point_dist = _x*_x + _y*_y;