胡斌

simple draw using ext_input

正在显示 1 个修改的文件 包含 146 行增加107 行删除
@@ -339,6 +339,133 @@ @@ -339,6 +339,133 @@
339 wsState = "idle"; 339 wsState = "idle";
340 } 340 }
341 341
  342 +
  343 + var ctx, color = "#000";
  344 + var voffset = 100;
  345 + var canvas_width,canvas_height;
  346 + var ext_input_panel_width,ext_input_panel_height;
  347 + var ratio_x,ratio_y;
  348 + var ext_input_open = false;
  349 + // function to setup a new canvas for drawing
  350 + function newCanvas() {
  351 + //define and resize canvas
  352 + canvas_width = window.innerWidth;
  353 + canvas_height = window.innerHeight - 90;
  354 + document.getElementById("content").style.height = canvas_height;
  355 + var canvas = '<canvas id="canvas" width="' + window.innerWidth + '" height="' + canvas_height + '"></canvas>';
  356 + document.getElementById("content").innerHTML = canvas;
  357 +
  358 + // setup canvas
  359 + ctx = document.getElementById("canvas").getContext("2d");
  360 + ctx.strokeStyle = color;
  361 + ctx.lineWidth = 1;
  362 + ctx.translate(0.5, 0.5);
  363 +
  364 + // setup to trigger drawing on mouse or touch
  365 + drawTouch();
  366 + drawPointer();
  367 + drawMouse();
  368 + }
  369 +
  370 + var last_ext_press = 0;
  371 + function draw_ext_input(px,py,press) {
  372 + x = px * ratio_x;
  373 + y = py * ratio_y;
  374 +
  375 + //y = canvas_height - py * ratio_y;
  376 +
  377 + if(last_ext_press == 0 && press == 1){
  378 + ctx.beginPath();
  379 +
  380 + ctx.moveTo(x, y);
  381 + last_ext_press = 1;
  382 + }
  383 + else if(press == 1) {
  384 + ctx.lineTo(x, y);
  385 + ctx.stroke();
  386 + }
  387 + last_ext_press = press;
  388 +
  389 + }
  390 +
  391 + function selectColor(el) {
  392 + for (var i = 0; i < document.getElementsByClassName("palette").length; i++) {
  393 + document.getElementsByClassName("palette")[i].style.borderColor = "#777";
  394 + document.getElementsByClassName("palette")[i].style.borderStyle = "solid";
  395 + }
  396 + el.style.borderColor = "#fff";
  397 + el.style.borderStyle = "dashed";
  398 + color = window.getComputedStyle(el).backgroundColor;
  399 + ctx.beginPath();
  400 + ctx.strokeStyle = color;
  401 + }
  402 +
  403 + // prototype to start drawing on touch using canvas moveTo and lineTo
  404 + var drawTouch = function () {
  405 + var start = function (e) {
  406 + ctx.beginPath();
  407 + x = e.changedTouches[0].pageX;
  408 + y = e.changedTouches[0].pageY - voffset;
  409 + ctx.moveTo(x, y);
  410 + };
  411 + var move = function (e) {
  412 + e.preventDefault();
  413 + x = e.changedTouches[0].pageX;
  414 + y = e.changedTouches[0].pageY -voffset;
  415 + ctx.lineTo(x, y);
  416 + ctx.stroke();
  417 + };
  418 + document.getElementById("canvas").addEventListener("touchstart", start, false);
  419 + document.getElementById("canvas").addEventListener("touchmove", move, false);
  420 + };
  421 +
  422 + // prototype to start drawing on pointer(microsoft ie) using canvas moveTo and lineTo
  423 + var drawPointer = function () {
  424 + var start = function (e) {
  425 + e = e.originalEvent;
  426 + ctx.beginPath();
  427 + x = e.pageX;
  428 + y = e.pageY - voffset;
  429 + ctx.moveTo(x, y);
  430 + };
  431 + var move = function (e) {
  432 + e.preventDefault();
  433 + e = e.originalEvent;
  434 + x = e.pageX;
  435 + y = e.pageY - voffset;
  436 + ctx.lineTo(x, y);
  437 + ctx.stroke();
  438 + };
  439 + document.getElementById("canvas").addEventListener("MSPointerDown", start, false);
  440 + document.getElementById("canvas").addEventListener("MSPointerMove", move, false);
  441 + };
  442 +
  443 + // prototype to start drawing on mouse using canvas moveTo and lineTo
  444 + var drawMouse = function () {
  445 + var clicked = 0;
  446 + var start = function (e) {
  447 + clicked = 1;
  448 + ctx.beginPath();
  449 + x = e.pageX;
  450 + y = e.pageY - voffset;
  451 + ctx.moveTo(x, y);
  452 + };
  453 + var move = function (e) {
  454 + if (clicked) {
  455 + x = e.pageX;
  456 + y = e.pageY - voffset;
  457 + ctx.lineTo(x, y);
  458 + ctx.stroke();
  459 + }
  460 + };
  461 + var stop = function (e) {
  462 + clicked = 0;
  463 + };
  464 + document.getElementById("canvas").addEventListener("mousedown", start, false);
  465 + document.getElementById("canvas").addEventListener("mousemove", move, false);
  466 + document.addEventListener("mouseup", stop, false);
  467 + };
  468 +
342 function playRTMPStream(url) { 469 function playRTMPStream(url) {
343 document.getElementById('inputURL').value = url; 470 document.getElementById('inputURL').value = url;
344 queryParameters['source'] = url; 471 queryParameters['source'] = url;
@@ -479,22 +606,23 @@ @@ -479,22 +606,23 @@
479 606
480 function onMessage(evt) { 607 function onMessage(evt) {
481 if (typeof (evt.data) == "string") { 608 if (typeof (evt.data) == "string") {
482 - writeToScreen('<span style="color: green;">receive string</span>'); 609 + // writeToScreen('<span style="color: green;">receive string</span>');
483 } 610 }
484 else { 611 else {
485 - writeToScreen('<span style="color: green;">receive binary</span>'); 612 + // writeToScreen('<span style="color: green;">receive binary</span>');
486 var reader = new FileReader(); 613 var reader = new FileReader();
487 reader.onload = function (evt) { 614 reader.onload = function (evt) {
488 if (evt.target.readyState == FileReader.DONE) { 615 if (evt.target.readyState == FileReader.DONE) {
489 var dv = new DataView(evt.target.result); 616 var dv = new DataView(evt.target.result);
490 - writeToScreen('<span style="color: green;">receive string ' + dv.byteLength + '</span>');  
491 var result = dv.getUint16(0, true); 617 var result = dv.getUint16(0, true);
492 var byte_order = dv.getUint8(2, true); 618 var byte_order = dv.getUint8(2, true);
493 var data_len = dv.getUint32(4, true); 619 var data_len = dv.getUint32(4, true);
494 620
495 - if (result >= 0 && result < wsResult.length) { 621 + if (result >= 0 && result < wsResult.length ) {
496 var resultString = wsResult[result]; 622 var resultString = wsResult[result];
497 - writeToScreen('<span style="color: green;">result: ' + resultString + ' </span>'); 623 + if(!ext_input_open) {
  624 + writeToScreen('<span style="color: green;">result: ' + resultString + ' </span>');
  625 + }
498 } 626 }
499 else { 627 else {
500 writeToScreen('<span style="color: green;">result: unknown result </span>'); 628 writeToScreen('<span style="color: green;">result: unknown result </span>');
@@ -627,15 +755,22 @@ @@ -627,15 +755,22 @@
627 else if (119 == result) { 755 else if (119 == result) {
628 var type = dv.getUint8(8, true); 756 var type = dv.getUint8(8, true);
629 if( 1 == type ){ 757 if( 1 == type ){
630 - var width = dv.getUint16(9, true);  
631 - var height = dv.getUint16(11, true); 758 + ext_input_panel_width = dv.getUint16(9, true);
  759 + ext_input_panel_height = dv.getUint16(11, true);
  760 + ratio_x = (canvas_width + 0.0) / ext_input_panel_width;
  761 + ratio_y = (canvas_height + 0.0) / ext_input_panel_height;
  762 + ext_input_open = true;
632 writeToScreen('<span style="color: green;">panel resolution: ' + width + ',' + height + '</span>'); 763 writeToScreen('<span style="color: green;">panel resolution: ' + width + ',' + height + '</span>');
633 } 764 }
634 else if(2 == type ){ 765 else if(2 == type ){
635 var nPress = dv.getUint16(9, true); 766 var nPress = dv.getUint16(9, true);
636 var x = dv.getUint16(11, true); 767 var x = dv.getUint16(11, true);
637 var y = dv.getUint16(13, true); 768 var y = dv.getUint16(13, true);
638 - writeToScreen('<span style="color: green;">panel resolution: ' + x + ',' + y + ',' + nPress + '</span>'); 769 + draw_ext_input(x,y,nPress);
  770 + // writeToScreen('<span style="color: green;">pen position: ' + x + ',' + y + ',' + nPress + '</span>');
  771 + }
  772 + else if( 3 == type ) {
  773 + ext_input_open = false;
639 } 774 }
640 } 775 }
641 else { 776 else {
@@ -1167,104 +1302,6 @@ @@ -1167,104 +1302,6 @@
1167 newCanvas(); 1302 newCanvas();
1168 }); 1303 });
1169 1304
1170 - var ctx, color = "#000";  
1171 - var voffset = 100;  
1172 - // function to setup a new canvas for drawing  
1173 - function newCanvas() {  
1174 - //define and resize canvas  
1175 - document.getElementById("content").style.height = window.innerHeight - 90;  
1176 - var canvas = '<canvas id="canvas" width="' + window.innerWidth + '" height="' + (window.innerHeight - 90) + '"></canvas>';  
1177 - document.getElementById("content").innerHTML = canvas;  
1178 -  
1179 - // setup canvas  
1180 - ctx = document.getElementById("canvas").getContext("2d");  
1181 - ctx.strokeStyle = color;  
1182 - ctx.lineWidth = 1;  
1183 - ctx.translate(0.5, 0.5);  
1184 -  
1185 - // setup to trigger drawing on mouse or touch  
1186 - drawTouch();  
1187 - drawPointer();  
1188 - drawMouse();  
1189 - }  
1190 -  
1191 - function selectColor(el) {  
1192 - for (var i = 0; i < document.getElementsByClassName("palette").length; i++) {  
1193 - document.getElementsByClassName("palette")[i].style.borderColor = "#777";  
1194 - document.getElementsByClassName("palette")[i].style.borderStyle = "solid";  
1195 - }  
1196 - el.style.borderColor = "#fff";  
1197 - el.style.borderStyle = "dashed";  
1198 - color = window.getComputedStyle(el).backgroundColor;  
1199 - ctx.beginPath();  
1200 - ctx.strokeStyle = color;  
1201 - }  
1202 -  
1203 - // prototype to start drawing on touch using canvas moveTo and lineTo  
1204 - var drawTouch = function () {  
1205 - var start = function (e) {  
1206 - ctx.beginPath();  
1207 - x = e.changedTouches[0].pageX;  
1208 - y = e.changedTouches[0].pageY - voffset;  
1209 - ctx.moveTo(x, y);  
1210 - };  
1211 - var move = function (e) {  
1212 - e.preventDefault();  
1213 - x = e.changedTouches[0].pageX;  
1214 - y = e.changedTouches[0].pageY -voffset;  
1215 - ctx.lineTo(x, y);  
1216 - ctx.stroke();  
1217 - };  
1218 - document.getElementById("canvas").addEventListener("touchstart", start, false);  
1219 - document.getElementById("canvas").addEventListener("touchmove", move, false);  
1220 - };  
1221 -  
1222 - // prototype to start drawing on pointer(microsoft ie) using canvas moveTo and lineTo  
1223 - var drawPointer = function () {  
1224 - var start = function (e) {  
1225 - e = e.originalEvent;  
1226 - ctx.beginPath();  
1227 - x = e.pageX;  
1228 - y = e.pageY - voffset;  
1229 - ctx.moveTo(x, y);  
1230 - };  
1231 - var move = function (e) {  
1232 - e.preventDefault();  
1233 - e = e.originalEvent;  
1234 - x = e.pageX;  
1235 - y = e.pageY - voffset;  
1236 - ctx.lineTo(x, y);  
1237 - ctx.stroke();  
1238 - };  
1239 - document.getElementById("canvas").addEventListener("MSPointerDown", start, false);  
1240 - document.getElementById("canvas").addEventListener("MSPointerMove", move, false);  
1241 - };  
1242 -  
1243 - // prototype to start drawing on mouse using canvas moveTo and lineTo  
1244 - var drawMouse = function () {  
1245 - var clicked = 0;  
1246 - var start = function (e) {  
1247 - clicked = 1;  
1248 - ctx.beginPath();  
1249 - x = e.pageX;  
1250 - y = e.pageY - voffset;  
1251 - ctx.moveTo(x, y);  
1252 - };  
1253 - var move = function (e) {  
1254 - if (clicked) {  
1255 - x = e.pageX;  
1256 - y = e.pageY - voffset;  
1257 - ctx.lineTo(x, y);  
1258 - ctx.stroke();  
1259 - }  
1260 - };  
1261 - var stop = function (e) {  
1262 - clicked = 0;  
1263 - };  
1264 - document.getElementById("canvas").addEventListener("mousedown", start, false);  
1265 - document.getElementById("canvas").addEventListener("mousemove", move, false);  
1266 - document.addEventListener("mouseup", stop, false);  
1267 - };  
1268 </script> 1305 </script>
1269 </head> 1306 </head>
1270 1307
@@ -1514,7 +1551,9 @@ @@ -1514,7 +1551,9 @@
1514 <td align="left">&nbsp;</td> 1551 <td align="left">&nbsp;</td>
1515 <td width="27%" align="left"><a id="disconnectBtn" onclick="javascript:disconnectLiveServ();"><img src="images/disconnect.gif" width="104" height="25" /> 1552 <td width="27%" align="left"><a id="disconnectBtn" onclick="javascript:disconnectLiveServ();"><img src="images/disconnect.gif" width="104" height="25" />
1516 <td><img src="images/spacer.gif" width="50" height="10" /></td> 1553 <td><img src="images/spacer.gif" width="50" height="10" /></td>
1517 - <td width="27%" align="left"><a id="clearBtn" onclick="javascript:clearOutput();"><img src="images/clear.gif" width="104" height="25" /></a></td> 1554 + <td width="27%" align="left"><a id="clearBtn" onclick="javascript:clearOutput();"><img src="images/clear.gif" width="104" height="25" /></a></td>
  1555 + <td colspan="2" align="center"><button type="button" id="open_ext_input" onclick="onTestClick(this)">open_ext_input</button></td>
  1556 + <td colspan="2" align="center"><button type="button" id="close_ext_input" onclick="onTestClick(this)">close_ext_input</button></td>
1518 </tr> 1557 </tr>
1519 </table> 1558 </table>
1520 </td> 1559 </td>