胡斌

add some code about PendoTech pen

正在显示 1 个修改的文件 包含 211 行增加6 行删除
@@ -347,6 +347,7 @@ @@ -347,6 +347,7 @@
347 var ext_input_panel_width,ext_input_panel_height; 347 var ext_input_panel_width,ext_input_panel_height;
348 var ratio_x,ratio_y; 348 var ratio_x,ratio_y;
349 var ext_input_open = false; 349 var ext_input_open = false;
  350 + var cur_pen_type = 0;
350 // function to setup a new canvas for drawing 351 // function to setup a new canvas for drawing
351 function newCanvas() { 352 function newCanvas() {
352 //define and resize canvas 353 //define and resize canvas
@@ -368,7 +369,8 @@ @@ -368,7 +369,8 @@
368 drawMouse(); 369 drawMouse();
369 } 370 }
370 371
371 - var last_ext_press = 0; 372 + var last_ext_press = 0;
  373 + /*
372 function draw_ext_input(px,py,press) { 374 function draw_ext_input(px,py,press) {
373 //x = canvas_width - px * ratio_x; 375 //x = canvas_width - px * ratio_x;
374 //y = canvas_height - py * ratio_y; 376 //y = canvas_height - py * ratio_y;
@@ -387,9 +389,193 @@ @@ -387,9 +389,193 @@
387 ctx.stroke(); 389 ctx.stroke();
388 } 390 }
389 last_ext_press = press; 391 last_ext_press = press;
  392 + }*/
390 393
  394 + function draw_ext_input(px,py,press) {
  395 + //x = canvas_width - px * ratio_x;
  396 + //y = canvas_height - py * ratio_y;
  397 +
  398 + x = px * ratio_x;
  399 + y = py * ratio_y;
  400 +
  401 + if(last_ext_press == 0 && press != 0){
  402 + onTouchDownEvent(x, y, press);
  403 + }
  404 + else if(press != 0) {
  405 + onTouchMoveEvent(x, y, press);
  406 + }
  407 + else if(last_ext_press !=0) {
  408 + onTouchUpEvent(x, y, press);
  409 + }
  410 + last_ext_press = press;
391 } 411 }
392 412
  413 + function Point(x , y, time) {
  414 + this.x = x;
  415 + this.y = y;
  416 + this.time = time;
  417 + }
  418 +
  419 +
  420 + var previousPoint, startPoint, currentPoint;
  421 + var MIN_PEN_SIZE = 1.0;
  422 + var MIN_INCREMENT = 0.01;
  423 + var INCREMENT_CONSTANT = 0.0005;
  424 + var DRAWING_CONSTANT = 0.0085;
  425 + var MAX_VELOCITY_BOUND = 15;
  426 + var MIN_VELOCITY_BOUND = 1.6;
  427 + var STROKE_DES_VELOCITY = 1.0;
  428 + var VELOCITY_FILTER_WEIGHT = 0.2;
  429 + var lastVelocity, lastWidth;
  430 + //------begin
  431 + var P_LOW = 200;
  432 + var P_MIDDLE = 500;
  433 + var P_HIGN = 800;
  434 + var kWIDTH_MIN = 1.0;
  435 + var kWIDTH_MAX = 3.0;
  436 + //current width
  437 + var strokeWidth = 3.0;
  438 +
  439 +
  440 + function setStrokeWidth(strokeWidth) {
  441 + strokeWidth = strokeWidth;
  442 + kWIDTH_MAX = strokeWidth;
  443 + kWIDTH_MIN = strokeWidth / 3;
  444 + }
  445 +
  446 + function changeWidth(P) {
  447 +
  448 + if (P > P_LOW && P < P_MIDDLE) {
  449 + strokeWidth -= 0.01;
  450 +
  451 + if (strokeWidth < kWIDTH_MIN) {
  452 + strokeWidth = kWIDTH_MIN;
  453 + }
  454 +
  455 + } else if (P >= P_MIDDLE && P < P_HIGN) {
  456 + strokeWidth += 0.005;
  457 + if (strokeWidth > kWIDTH_MAX) {
  458 + strokeWidth = kWIDTH_MAX;
  459 + }
  460 +
  461 +
  462 + } else if (P >= P_HIGN) {
  463 + strokeWidth += 0.01;
  464 +
  465 + if (strokeWidth > kWIDTH_MAX) {
  466 + strokeWidth = kWIDTH_MAX;
  467 + }
  468 +
  469 +
  470 + } else {
  471 + strokeWidth -= 0.003;
  472 +
  473 + if (strokeWidth < kWIDTH_MIN) {
  474 + strokeWidth = kWIDTH_MIN;
  475 + }
  476 +
  477 + }
  478 +
  479 + }
  480 +
  481 + function velocityFrom(a,b) {
  482 + var dx = a.x - b.x;
  483 + var dy = a.y - b.y;
  484 + var dist = Math.sqrt(a*a + b*b);
  485 + var v = dist / (a.time - b.time)
  486 + return v;
  487 + }
  488 +
  489 + function onTouchDownEvent(x, y, p) {
  490 + previousPoint = null;
  491 + startPoint = null;
  492 + currentPoint = null;
  493 + lastVelocity = 0;
  494 + //lastWidth = penSize;
  495 + var d = new Date();
  496 + currentPoint = new Point(x, y, d.getTime());
  497 + previousPoint = currentPoint;
  498 + startPoint = previousPoint;
  499 + }
  500 +
  501 + function onTouchMoveEvent(x, y, p) {
  502 +
  503 +
  504 + if (previousPoint == null) {
  505 + return;
  506 + }
  507 + startPoint = previousPoint;
  508 + previousPoint = currentPoint;
  509 + var d = new Date();
  510 + currentPoint = new Point(x, y, d.getTime());
  511 +
  512 + var velocity = velocityFrom(currentPoint , previousPoint);
  513 + drawLine(strokeWidth, velocity, p);
  514 + lastVelocity = velocity;
  515 +
  516 + }
  517 +
  518 + function onTouchUpEvent(x, y, p) {
  519 + if (previousPoint == null) {
  520 + return;
  521 + }
  522 + startPoint = previousPoint;
  523 + previousPoint = currentPoint;
  524 + var d = new Date();
  525 + currentPoint = new Point(x, y, d.getTime());
  526 +
  527 + drawLine(0, lastVelocity, p);
  528 +
  529 + }
  530 +
  531 + function getStrokeWidth(velocity) {
  532 + return penSize - (velocity * STROKE_DES_VELOCITY);
  533 + }
  534 +
  535 +
  536 + function drawLine(currentWidth, velocity, Presure) {
  537 + var mid1 = midPoint(previousPoint, startPoint);
  538 + var mid2 = midPoint(currentPoint, previousPoint);
  539 +
  540 + draw(mid1, previousPoint, mid2,
  541 + currentWidth, velocity, Presure);
  542 + }
  543 +
  544 + function getPt(n1, n2, perc) {
  545 + var diff = n2 - n1;
  546 + return n1 + (diff * perc);
  547 + }
  548 +
  549 + function draw(p0, p1, p2, currentWidth, velocity, p) {
  550 + var xa, xb, ya, yb, x, y;
  551 + var increment;
  552 + if (velocity > MIN_VELOCITY_BOUND && velocity < MAX_VELOCITY_BOUND) {
  553 + increment = DRAWING_CONSTANT - (velocity * INCREMENT_CONSTANT);
  554 + } else {
  555 + increment = MIN_INCREMENT;
  556 + }
  557 +
  558 + for (var i = 0.0; i < 1.0; i += increment) {
  559 + xa = getPt(p0.x, p1.x, i);
  560 + ya = getPt(p0.y, p1.y, i);
  561 + xb = getPt(p1.x, p2.x, i);
  562 + yb = getPt(p1.y, p2.y, i);
  563 +
  564 + x = getPt(xa, xb, i);
  565 + y = getPt(ya, yb, i);
  566 + changeWidth(p);
  567 + ctx.lineWidth = strokeWidth;
  568 + ctx.beginPath(); //Start path
  569 + ctx.arc(x, y, strokeWidth, 0, Math.PI * 2, true); // Draw a point using the arc function of the canvas with a point structure.
  570 + ctx.fill();
  571 + }
  572 + }
  573 +
  574 + function midPoint(p1, p2) {
  575 + return new Point((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2, (p1.time + p2.time) / 2);
  576 + }
  577 + //------end
  578 +
393 function selectColor(el) { 579 function selectColor(el) {
394 for (var i = 0; i < document.getElementsByClassName("palette").length; i++) { 580 for (var i = 0; i < document.getElementsByClassName("palette").length; i++) {
395 document.getElementsByClassName("palette")[i].style.borderColor = "#777"; 581 document.getElementsByClassName("palette")[i].style.borderColor = "#777";
@@ -782,7 +968,7 @@ @@ -782,7 +968,7 @@
782 writeToScreen('<span style="color: green;">panel resolution: ' + ext_input_panel_width + ',' + ext_input_panel_height + '</span>'); 968 writeToScreen('<span style="color: green;">panel resolution: ' + ext_input_panel_width + ',' + ext_input_panel_height + '</span>');
783 ext_input_open = true; 969 ext_input_open = true;
784 } 970 }
785 - else if(2 == type ){ 971 + else if(2 == type || 14 == type){
786 var nPress = dv.getUint16(9, true); 972 var nPress = dv.getUint16(9, true);
787 var x = dv.getUint16(11, true); 973 var x = dv.getUint16(11, true);
788 var y = dv.getUint16(13, true); 974 var y = dv.getUint16(13, true);
@@ -922,7 +1108,16 @@ @@ -922,7 +1108,16 @@
922 } 1108 }
923 1109
924 var ext_input_connecting = false; 1110 var ext_input_connecting = false;
  1111 +
925 function onConnectClick(obj) { 1112 function onConnectClick(obj) {
  1113 + var cur_pen_type = document.getElementById("pen_type").selectedIndex;
  1114 + if(cur_pen_type == 0) {
  1115 + ext_input_panel_width = 21000;
  1116 + ext_input_panel_height = 29800;
  1117 + ratio_x = (canvas_width + 0.0) / ext_input_panel_width;
  1118 + ratio_y = (canvas_height + 0.0) / ext_input_panel_height;
  1119 + }
  1120 +
926 if( !ext_input_connecting ) { 1121 if( !ext_input_connecting ) {
927 doCmd("open_ext_input"); 1122 doCmd("open_ext_input");
928 } 1123 }
@@ -1170,7 +1365,12 @@ @@ -1170,7 +1365,12 @@
1170 cmd.data_len = 1; 1365 cmd.data_len = 1;
1171 var buffer = new ArrayBuffer(cmd.data_len); 1366 var buffer = new ArrayBuffer(cmd.data_len);
1172 cmd.data = new Uint8Array(buffer, 0, cmd.data_len); 1367 cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
1173 - cmd.data[0] = 0; 1368 + if(pen_type == 0) {
  1369 + cmd.data[0] = 10;
  1370 + }
  1371 + else {
  1372 + cmd.data[0] = 0;
  1373 + }
1174 1374
1175 } 1375 }
1176 else if(cmdName == "close_ext_input") { 1376 else if(cmdName == "close_ext_input") {
@@ -1178,7 +1378,12 @@ @@ -1178,7 +1378,12 @@
1178 cmd.data_len = 1; 1378 cmd.data_len = 1;
1179 var buffer = new ArrayBuffer(cmd.data_len); 1379 var buffer = new ArrayBuffer(cmd.data_len);
1180 cmd.data = new Uint8Array(buffer, 0, cmd.data_len); 1380 cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
1181 - cmd.data[0] = 1; 1381 + if(pen_type == 0) {
  1382 + cmd.data[0] = 11;
  1383 + }
  1384 + else {
  1385 + cmd.data[0] = 1;
  1386 + }
1182 } 1387 }
1183 else if(cmdName == "rotate_ext_input") { 1388 else if(cmdName == "rotate_ext_input") {
1184 cmd.type = 57; 1389 cmd.type = 57;
@@ -1505,7 +1710,7 @@ @@ -1505,7 +1710,7 @@
1505 <div id='jqxTabs' width="100%"> 1710 <div id='jqxTabs' width="100%">
1506 <ul style='margin-left: 20px;'> 1711 <ul style='margin-left: 20px;'>
1507 <li>live streaming</li> 1712 <li>live streaming</li>
1508 - <li>robot pen input</li> 1713 + <li>Pen input</li>
1509 <li>Agora Screen Share</li> 1714 <li>Agora Screen Share</li>
1510 </ul> 1715 </ul>
1511 1716
@@ -1768,6 +1973,7 @@ @@ -1768,6 +1973,7 @@
1768 <div> 1973 <div>
1769 <div id="page"> 1974 <div id="page">
1770 <div class="header"> 1975 <div class="header">
  1976 + <select id="pen_type" name="pen_type"><option value ="PendoTech">PendoTech PH-1820-A</option><option value ="robot">Nebula T7</option></select>
1771 <a id="new" class="navbtn" onclick="newCanvas()">New</a> 1977 <a id="new" class="navbtn" onclick="newCanvas()">New</a>
1772 <a id="open_ext_input" class="navbtn" onclick="onTestClick(this)">open_ext_input</a> 1978 <a id="open_ext_input" class="navbtn" onclick="onTestClick(this)">open_ext_input</a>
1773 <a id="close_ext_input" class="navbtn" onclick="onTestClick(this)">close_ext_input</a> 1979 <a id="close_ext_input" class="navbtn" onclick="onTestClick(this)">close_ext_input</a>
@@ -1798,7 +2004,6 @@ @@ -1798,7 +2004,6 @@
1798 </div> 2004 </div>
1799 </div> 2005 </div>
1800 </div> 2006 </div>
1801 -  
1802 <div> 2007 <div>
1803 <table> 2008 <table>
1804 <tr> 2009 <tr>