Pārlūkot izejas kodu

New tool: rectangle

dev_h
Ophir LOJKINE 9 gadus atpakaļ
vecāks
revīzija
78bff7c1b9

+ 1
- 0
client-data/board.html Parādīt failu

51
 <script type="text/javascript" src="tools/eraser/eraser.js"></script>
51
 <script type="text/javascript" src="tools/eraser/eraser.js"></script>
52
 <script type="text/javascript" src="tools/hand/hand.js"></script>
52
 <script type="text/javascript" src="tools/hand/hand.js"></script>
53
 <script type="text/javascript" src="tools/line/line.js"></script>
53
 <script type="text/javascript" src="tools/line/line.js"></script>
54
+<script type="text/javascript" src="tools/rect/rect.js"></script>
54
 <script type="text/javascript" src="/js/canvascolor/canvascolor.min.js"></script>
55
 <script type="text/javascript" src="/js/canvascolor/canvascolor.min.js"></script>
55
 </body>
56
 </body>
56
 </html>
57
 </html>

+ 3
- 0
client-data/tools/rect/rect.css Parādīt failu

1
+rect {
2
+	fill: none;
3
+}

+ 140
- 0
client-data/tools/rect/rect.js Parādīt failu

1
+/**
2
+ *                        WHITEBOPHIR
3
+ *********************************************************
4
+ * @licstart  The following is the entire license notice for the
5
+ *  JavaScript code in this page.
6
+ *
7
+ * Copyright (C) 2013  Ophir LOJKINE
8
+ *
9
+ *
10
+ * The JavaScript code in this page is free software: you can
11
+ * redistribute it and/or modify it under the terms of the GNU
12
+ * General Public License (GNU GPL) as published by the Free Software
13
+ * Foundation, either version 3 of the License, or (at your option)
14
+ * any later version.  The code is distributed WITHOUT ANY WARRANTY;
15
+ * without even the implied warranty of MERCHANTABILITY or FITNESS
16
+ * FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
17
+ *
18
+ * As additional permission under GNU GPL version 3 section 7, you
19
+ * may distribute non-source (e.g., minimized or compacted) forms of
20
+ * that code without the copy of the GNU GPL normally required by
21
+ * section 4, provided you include this license notice and a URL
22
+ * through which recipients can access the Corresponding Source.
23
+ *
24
+ * @licend
25
+ */
26
+
27
+(function(){ //Code isolation
28
+	//Indicates the id of the shape the user is currently drawing or an empty string while the user is not drawing
29
+	var curId = "",
30
+		curUpdate = { //The data of the message that will be sent for every new point
31
+				'type' : 'update',
32
+				'id' : "",
33
+        'x'  : 0,
34
+				'y'  : 0,
35
+				'x2' : 0,
36
+				'y2' : 0
37
+		},
38
+		lastTime = performance.now(); //The time at which the last point was drawn
39
+
40
+	function start (x,y, evt) {
41
+
42
+		//Prevent the press from being interpreted by the browser
43
+		evt.preventDefault();
44
+
45
+		curId = Tools.generateUID("r"); //"r" for rectangle
46
+
47
+		Tools.drawAndSend({
48
+			'type' : 'rect',
49
+			'id' : curId,
50
+			'color' : Tools.getColor(),
51
+			'size' : Tools.getSize(),
52
+      'x' : x,
53
+      'y' : y,
54
+      'x2': x,
55
+      'y2': y
56
+		});
57
+
58
+    curUpdate.id = curId;
59
+    curUpdate.x = x;
60
+    curUpdate.y = y;
61
+	}
62
+
63
+	function move (x,y, evt){
64
+		/*Wait 70ms before adding any point to the currently drawing shape.
65
+		This allows the animation to be smother*/
66
+		if (curId !== "") {
67
+			curUpdate['x2'] = x; curUpdate['y2'] = y;
68
+			if (performance.now() - lastTime > 70) {
69
+				Tools.drawAndSend(curUpdate);
70
+				lastTime = performance.now();
71
+			} else {
72
+				draw(curUpdate);
73
+			}
74
+		}
75
+		if (evt) evt.preventDefault();
76
+	}
77
+
78
+	function stop (x,y){
79
+		//Add a last point to the shape
80
+		move(x,y);
81
+		curId = "";
82
+	}
83
+
84
+	function draw(data) {
85
+		switch(data.type) {
86
+			case "rect":
87
+				createShape(data);
88
+				break;
89
+			case "update":
90
+				var shape = svg.getElementById(data['id']);
91
+				if (!shape) {
92
+					console.error("Straight shape: Hmmm... I received a point of a rect that has not been created (%s).", data['id']);
93
+					createShape({ //create a new shape in order not to loose the points
94
+            "id": data['id'],
95
+            "x": data['x2'],
96
+            "y": data['y2']
97
+          });
98
+				}
99
+        updateShape(shape, data);
100
+				break;
101
+			default:
102
+				console.error("Straight shape: Draw instruction with unknown type. ", data);
103
+				break;
104
+		}
105
+	}
106
+
107
+	var svg = Tools.svg;
108
+	function createShape(data) {
109
+		//Creates a new shape on the canvas, or update a shape that already exists with new information
110
+		var shape = svg.getElementById(data.id) || Tools.createSVGElement("rect");
111
+		shape.id = data.id;
112
+    updateShape(shape, data);
113
+		//If some data is not provided, choose default value. The shape may be updated later
114
+		shape.setAttribute("stroke", data.color || "black" );
115
+		shape.setAttribute("stroke-width", data.size || 10 );
116
+		svg.appendChild(shape);
117
+		return shape;
118
+	}
119
+
120
+  function updateShape(shape, data) {
121
+    shape.x.baseVal.value = Math.min(data['x2'], data['x']);
122
+    shape.y.baseVal.value = Math.min(data['y2'], data['y']);
123
+    shape.width.baseVal.value  = Math.abs(data['x2'] - data['x']);
124
+    shape.height.baseVal.value = Math.abs(data['y2'] - data['y']);
125
+  }
126
+
127
+	Tools.add({ //The new tool
128
+	 	"name" : "Rectangle",
129
+	 	"icon" : "▢",
130
+	 	"listeners" : {
131
+	 		"press" : start,
132
+	 		"move" : move,
133
+      "release" : stop,
134
+	 	},
135
+	 	"draw" : draw,
136
+	 	"mouseCursor" : "crosshair",
137
+    "stylesheet" : "tools/rect/rect.css"
138
+	});
139
+
140
+})(); //End of code isolation

Notiek ielāde…
Atcelt
Saglabāt