소스 검색

End of the first tool (pencil). Now, let's debug all that...

dev_h
Ophir LOJKINE 11 년 전
부모
커밋
eaea72d442
1개의 변경된 파일47개의 추가작업 그리고 43개의 파일을 삭제
  1. 47
    43
      client-data/tools/pencil.js

+ 47
- 43
client-data/tools/pencil.js 파일 보기

@@ -1,27 +1,55 @@
1 1
 (function(){ //Code isolation
2
-	var pencil = {}; //The new tool that will be added with Tools.add
3
-	pencil.name = "Pencil";
2
+	//Indicates the id of the line the user is currently drawing or an empty string while the user is not drawing
3
+	var curLineId = "",
4
+		lastTime = performance.now(); //The time at which the last point was drawn
4 5
 
6
+	function startLine (x,y) { 
7
+		curLineId = Tools.generateUID("l"); //"l" for line
5 8
 
6
-	var svg = Tools.svg;
9
+		Tools.drawAndSend({
10
+			'type' : 'line',
11
+			'id' : curLineId
12
+		});
13
+		
14
+		//Immediatly add a point to the line
15
+		continueLine(x,y);
16
+	}
17
+
18
+	function continueLine (x,y){
19
+		/*Wait 50ms before adding any point to the currently drawing line.
20
+		This allows the animation to be smother*/
21
+		if (curLineId !== "" &&
22
+			performance.now() - lastTime > 50) {
23
+			Tools.drawAndSend({
24
+				'type' : 'point',
25
+				'line' : curLineId,
26
+				'x' : x,
27
+				'y' : y
28
+			});
29
+			lastTime = performance.now();
30
+		}
31
+	}
32
+
33
+	function stopLine (x,y){
34
+		//Add a last point to the line
35
+		continueLine(x,y);
36
+		curLineId = "";
37
+	}
7 38
 
8
-	var curLine = {},
9
-		lastTime = performance.now(),
10
-		drawing = false; //Indicates if a line is currently being draxn
11
- 
12 39
 	function draw(data) {
13 40
 		switch(data.type) {
14 41
 			case "line":
15
-				curLine = createLine(data.id);
42
+				renderingLine = createLine(data.id);
16 43
 				break;
17 44
 			case "point":
18
-				var line = (data.line===curLine.id) ? curLine : svg.getElementById(data.line);
45
+				var line = (renderingLine.id == data.line) ? renderingLine : svg.getElementById(data.line);
19 46
 				if (!line) {
20 47
 					console.log("Pencil: Hmmm... I received a point of a line I don't know...");
21 48
 				}
22 49
 				addPoint(line, data.x, data.y);
23 50
 				break;
24 51
 			case "endline":
52
+				//TODO?
25 53
 				break;
26 54
 			default:
27 55
 				console.log("Pencil: Draw instruction with unknown type. ", data);
@@ -29,41 +57,8 @@
29 57
 		}
30 58
 	}
31 59
 
32
-	function pressHandler (ev) {
33
-		drawing = true;
34
-		Tools.drawAndSend({
35
-			'type' : 'line',
36
-			'id' : Tools.generateUID("l"); //"l" for line
37
-		});
38
-	}
39
-
40
-	function moveHandler (ev){
41
-		if (curLine !== null &&
42
-			performance.now() - lastTime > 50) {
43
-			var x = ev.clientX + window.scrollX,
44
-				y = ev.clientY + window.scrollY;
45
-			socket.emit('broadcast', {
46
-				'type' : 'point',
47
-				'line' : curLine.id,
48
-				'x' : x,
49
-				'y' : y
50
-			});
51
-			addPoint(curLine, x,y);
52
-			lastTime = performance.now();
53
-		}
54
-		ev.preventDefault();
55
-		return false;
56
-	}
57
-
58
-	function releaseHandler (){
59
-		drawing = false;
60
-	}
61
-svg.addEventListener("mouseup", releaseHandler);
62
-svg.addEventListener("mouseleave", releaseHandler);
63
-//svg.addEventListener("touchend", releaseHandler);
64
-
65
-
66 60
 
61
+	var svg = Tools.svg;
67 62
 	function addPoint (line, x,y) {
68 63
 		var point = svg.createSVGPoint();
69 64
 		point.x = x; point.y = y;
@@ -77,4 +72,13 @@ svg.addEventListener("mouseleave", releaseHandler);
77 72
 		return line;
78 73
 	}
79 74
 
75
+	Tools.add({ //The new tool
76
+	 	"name" : "Pencil",
77
+	 	"listeners" : {
78
+	 		"press" : startLine,
79
+	 		"move" : continueLine,
80
+	  		"release" : stopLine,
81
+	 	},
82
+	 	"draw" : draw
83
+	});
80 84
 })(); //End of code isolation

Loading…
취소
저장