瀏覽代碼

Update the pencil tool to work with latest browsers

dev_h
Ophir LOJKINE 7 年之前
父節點
當前提交
2786fa4641
共有 4 個文件被更改,包括 1155 次插入263 次删除
  1. 1
    3
      client-data/board.html
  2. 0
    238
      client-data/js/classList.js
  3. 1119
    0
      client-data/js/path-data-polyfill.js
  4. 35
    22
      client-data/tools/pencil/pencil.js

+ 1
- 3
client-data/board.html 查看文件

@@ -41,9 +41,7 @@
41 41
 </div>
42 42
 
43 43
 
44
-<!--[if lt IE 10]>
45
-<script type="text/javascript" src="js/classList.js"></script>
46
-<![endif]-->
44
+<script type="text/javascript" src="js/path-data-polyfill.js"></script>
47 45
 <script type="text/javascript" src="js/minitpl.js"></script>
48 46
 <script type="text/javascript" src="js/board.js"></script>
49 47
 <script type="text/javascript" src="tools/pencil/pencil.js"></script>

+ 0
- 238
client-data/js/classList.js 查看文件

@@ -1,238 +0,0 @@
1
-/*
2
- * classList.js: Cross-browser full element.classList implementation.
3
- * 1.1.20150312
4
- *
5
- * By Eli Grey, http://eligrey.com
6
- * License: Dedicated to the public domain.
7
- *   See https://github.com/eligrey/classList.js/blob/master/LICENSE.md
8
- */
9
-
10
-/*global self, document, DOMException */
11
-
12
-/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
13
-
14
-if ("document" in self) {
15
-
16
-// Full polyfill for browsers with no classList support
17
-if (!("classList" in document.createElement("_"))) {
18
-
19
-(function (view) {
20
-
21
-"use strict";
22
-
23
-if (!('Element' in view)) return;
24
-
25
-var
26
-	  classListProp = "classList"
27
-	, protoProp = "prototype"
28
-	, elemCtrProto = view.Element[protoProp]
29
-	, objCtr = Object
30
-	, strTrim = String[protoProp].trim || function () {
31
-		return this.replace(/^\s+|\s+$/g, "");
32
-	}
33
-	, arrIndexOf = Array[protoProp].indexOf || function (item) {
34
-		var
35
-			  i = 0
36
-			, len = this.length
37
-		;
38
-		for (; i < len; i++) {
39
-			if (i in this && this[i] === item) {
40
-				return i;
41
-			}
42
-		}
43
-		return -1;
44
-	}
45
-	// Vendors: please allow content code to instantiate DOMExceptions
46
-	, DOMEx = function (type, message) {
47
-		this.name = type;
48
-		this.code = DOMException[type];
49
-		this.message = message;
50
-	}
51
-	, checkTokenAndGetIndex = function (classList, token) {
52
-		if (token === "") {
53
-			throw new DOMEx(
54
-				  "SYNTAX_ERR"
55
-				, "An invalid or illegal string was specified"
56
-			);
57
-		}
58
-		if (/\s/.test(token)) {
59
-			throw new DOMEx(
60
-				  "INVALID_CHARACTER_ERR"
61
-				, "String contains an invalid character"
62
-			);
63
-		}
64
-		return arrIndexOf.call(classList, token);
65
-	}
66
-	, ClassList = function (elem) {
67
-		var
68
-			  trimmedClasses = strTrim.call(elem.getAttribute("class") || "")
69
-			, classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
70
-			, i = 0
71
-			, len = classes.length
72
-		;
73
-		for (; i < len; i++) {
74
-			this.push(classes[i]);
75
-		}
76
-		this._updateClassName = function () {
77
-			elem.setAttribute("class", this.toString());
78
-		};
79
-	}
80
-	, classListProto = ClassList[protoProp] = []
81
-	, classListGetter = function () {
82
-		return new ClassList(this);
83
-	}
84
-;
85
-// Most DOMException implementations don't allow calling DOMException's toString()
86
-// on non-DOMExceptions. Error's toString() is sufficient here.
87
-DOMEx[protoProp] = Error[protoProp];
88
-classListProto.item = function (i) {
89
-	return this[i] || null;
90
-};
91
-classListProto.contains = function (token) {
92
-	token += "";
93
-	return checkTokenAndGetIndex(this, token) !== -1;
94
-};
95
-classListProto.add = function () {
96
-	var
97
-		  tokens = arguments
98
-		, i = 0
99
-		, l = tokens.length
100
-		, token
101
-		, updated = false
102
-	;
103
-	do {
104
-		token = tokens[i] + "";
105
-		if (checkTokenAndGetIndex(this, token) === -1) {
106
-			this.push(token);
107
-			updated = true;
108
-		}
109
-	}
110
-	while (++i < l);
111
-
112
-	if (updated) {
113
-		this._updateClassName();
114
-	}
115
-};
116
-classListProto.remove = function () {
117
-	var
118
-		  tokens = arguments
119
-		, i = 0
120
-		, l = tokens.length
121
-		, token
122
-		, updated = false
123
-		, index
124
-	;
125
-	do {
126
-		token = tokens[i] + "";
127
-		index = checkTokenAndGetIndex(this, token);
128
-		while (index !== -1) {
129
-			this.splice(index, 1);
130
-			updated = true;
131
-			index = checkTokenAndGetIndex(this, token);
132
-		}
133
-	}
134
-	while (++i < l);
135
-
136
-	if (updated) {
137
-		this._updateClassName();
138
-	}
139
-};
140
-classListProto.toggle = function (token, force) {
141
-	token += "";
142
-
143
-	var
144
-		  result = this.contains(token)
145
-		, method = result ?
146
-			force !== true && "remove"
147
-		:
148
-			force !== false && "add"
149
-	;
150
-
151
-	if (method) {
152
-		this[method](token);
153
-	}
154
-
155
-	if (force === true || force === false) {
156
-		return force;
157
-	} else {
158
-		return !result;
159
-	}
160
-};
161
-classListProto.toString = function () {
162
-	return this.join(" ");
163
-};
164
-
165
-if (objCtr.defineProperty) {
166
-	var classListPropDesc = {
167
-		  get: classListGetter
168
-		, enumerable: true
169
-		, configurable: true
170
-	};
171
-	try {
172
-		objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
173
-	} catch (ex) { // IE 8 doesn't support enumerable:true
174
-		if (ex.number === -0x7FF5EC54) {
175
-			classListPropDesc.enumerable = false;
176
-			objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
177
-		}
178
-	}
179
-} else if (objCtr[protoProp].__defineGetter__) {
180
-	elemCtrProto.__defineGetter__(classListProp, classListGetter);
181
-}
182
-
183
-}(self));
184
-
185
-} else {
186
-// There is full or partial native classList support, so just check if we need
187
-// to normalize the add/remove and toggle APIs.
188
-
189
-(function () {
190
-	"use strict";
191
-
192
-	var testElement = document.createElement("_");
193
-
194
-	testElement.classList.add("c1", "c2");
195
-
196
-	// Polyfill for IE 10/11 and Firefox <26, where classList.add and
197
-	// classList.remove exist but support only one argument at a time.
198
-	if (!testElement.classList.contains("c2")) {
199
-		var createMethod = function(method) {
200
-			var original = DOMTokenList.prototype[method];
201
-
202
-			DOMTokenList.prototype[method] = function(token) {
203
-				var i, len = arguments.length;
204
-
205
-				for (i = 0; i < len; i++) {
206
-					token = arguments[i];
207
-					original.call(this, token);
208
-				}
209
-			};
210
-		};
211
-		createMethod('add');
212
-		createMethod('remove');
213
-	}
214
-
215
-	testElement.classList.toggle("c3", false);
216
-
217
-	// Polyfill for IE 10 and Firefox <24, where classList.toggle does not
218
-	// support the second argument.
219
-	if (testElement.classList.contains("c3")) {
220
-		var _toggle = DOMTokenList.prototype.toggle;
221
-
222
-		DOMTokenList.prototype.toggle = function(token, force) {
223
-			if (1 in arguments && !this.contains(token) === !force) {
224
-				return force;
225
-			} else {
226
-				return _toggle.call(this, token);
227
-			}
228
-		};
229
-
230
-	}
231
-
232
-	testElement = null;
233
-}());
234
-
235
-}
236
-
237
-}
238
-

+ 1119
- 0
client-data/js/path-data-polyfill.js
文件差異過大導致無法顯示
查看文件


+ 35
- 22
client-data/tools/pencil/pencil.js 查看文件

@@ -104,46 +104,59 @@
104 104
 
105 105
 	var svg = Tools.svg;
106 106
 	function addPoint (line, x,y) {
107
-		var nbr = line.pathSegList.numberOfItems, //The number of points already in the line
108
-			pts = line.pathSegList, //The points that are already in the line as a SVGPathSegList
109
-			npoint;
107
+		var pts = line.getPathData(), //The points that are already in the line as a PathData
108
+			nbr = pts.length; //The number of points already in the line
110 109
 		switch (nbr) {
111 110
 			case 0: //The first point in the line
112 111
 				//If there is no point, we have to start the line with a moveTo statement
113
-				npoint = line.createSVGPathSegMovetoAbs(x,y);
112
+				npoint = {type:"M", values:[x,y]};
114 113
 				break;
115 114
 			case 1: //There is only one point.
116 115
 				//Draw a curve that is segment between the old point and the new one
117
-				npoint = line.createSVGPathSegCurvetoCubicAbs(
118
-							x,y, pts.getItem(0).x,pts.getItem(0).y, x,y);
116
+				npoint = {type:"C", values:[
117
+					pts[0].values[0], pts[0].values[1],
118
+					x,y,
119
+					x,y,
120
+				]};
119 121
 				break;
120 122
 			default: //There are at least two points in the line
121 123
 				//We add the new point, and smoothen the line
122 124
 				var ANGULARITY = 3; //The lower this number, the smoother the line
123
-				var prev = [pts.getItem(nbr-2), pts.getItem(nbr-1)]; //The last two points that are already in the line
125
+				var prev_values = pts[nbr-1].values; // Previous point
126
+				var ante_values = pts[nbr-2].values; // Point before the previous one
127
+				var prev_x = prev_values[prev_values.length - 2];
128
+				var prev_y = prev_values[prev_values.length - 1];
129
+				var ante_x = ante_values[ante_values.length - 2];
130
+				var ante_y = ante_values[ante_values.length - 1];
131
+
124 132
 
125 133
 				//We don't want to add the same point twice consecutively
126
-				if ((prev[1].x==x && prev[1].y==y)
127
-					|| (prev[0].x==x && prev[0].y==y) ) return;
134
+				if ((prev_x==x && prev_y==y)
135
+					|| (ante_x==x && ante_y==y) ) return;
128 136
 
129
-				var vectx = x-prev[0].x,
130
-					vecty = y-prev[0].y;
137
+				var vectx = x-ante_x,
138
+					vecty = y-ante_y;
131 139
 				var norm = Math.hypot(vectx,vecty);
132
-				var dist1 = dist(prev[0].x,prev[0].y,prev[1].x,prev[1].y)/norm,
133
-					dist2 = dist(x,y,prev[1].x,prev[1].y)/norm;
140
+				var dist1 = dist(ante_x,ante_y,prev_x,prev_y)/norm,
141
+					dist2 = dist(x,y,prev_x,prev_y)/norm;
134 142
 				vectx /= ANGULARITY;
135 143
 				vecty /= ANGULARITY;
136 144
 				//Create 2 control points around the last point
137
-				var cx1 = prev[1].x - dist1*vectx,
138
-					cy1 = prev[1].y - dist1*vecty, //First control point
139
-					cx2 = prev[1].x + dist2*vectx,
140
-					cy2 = prev[1].y + dist2*vecty; //Second control point
141
-				prev[1].x2 = cx1;
142
-				prev[1].y2 = cy1;
143
-
144
-				npoint = line.createSVGPathSegCurvetoCubicAbs(x,y,cx2,cy2,x,y);
145
+				var cx1 = prev_x - dist1*vectx,
146
+					cy1 = prev_y - dist1*vecty, //First control point
147
+					cx2 = prev_x + dist2*vectx,
148
+					cy2 = prev_y + dist2*vecty; //Second control point
149
+				prev_values[2] = cx1;
150
+				prev_values[3] = cy1;
151
+
152
+				npoint = {type:"C", values:[
153
+					cx2,cy2,
154
+					x,y,
155
+					x,y,
156
+				]};
145 157
 		}
146
-		line.pathSegList.appendItem(npoint);
158
+		pts.push(npoint);
159
+		line.setPathData(pts);
147 160
 	}
148 161
 
149 162
 	function createLine(lineData) {

Loading…
取消
儲存