胡斌

use linedda to interpolate point and pressure

正在显示 1 个修改的文件 包含 113 行增加140 行删除
@@ -372,12 +372,8 @@ @@ -372,12 +372,8 @@
372 var last_ext_press = 0; 372 var last_ext_press = 0;
373 /* 373 /*
374 function draw_ext_input(px,py,press) { 374 function draw_ext_input(px,py,press) {
375 - //x = canvas_width - px * ratio_x;  
376 - //y = canvas_height - py * ratio_y;  
377 -  
378 x = px * ratio_x; 375 x = px * ratio_x;
379 y = py * ratio_y; 376 y = py * ratio_y;
380 -  
381 if(last_ext_press == 0 && press != 0){ 377 if(last_ext_press == 0 && press != 0){
382 ctx.beginPath(); 378 ctx.beginPath();
383 379
@@ -385,16 +381,14 @@ @@ -385,16 +381,14 @@
385 last_ext_press = 1; 381 last_ext_press = 1;
386 } 382 }
387 else if(press != 0) { 383 else if(press != 0) {
  384 + var line_width = (kWIDTH_MAX - kWIDTH_MIN)/pressure_level * p + kWIDTH_MIN;
388 ctx.lineTo(x, y); 385 ctx.lineTo(x, y);
389 ctx.stroke(); 386 ctx.stroke();
390 } 387 }
391 last_ext_press = press; 388 last_ext_press = press;
392 }*/ 389 }*/
393 390
394 - function draw_ext_input(px,py,press) {  
395 - //x = canvas_width - px * ratio_x;  
396 - //y = canvas_height - py * ratio_y;  
397 - 391 + function draw_ext_input(px, py, press) {
398 var x = px * ratio_x; 392 var x = px * ratio_x;
399 var y = py * ratio_y; 393 var y = py * ratio_y;
400 394
@@ -410,32 +404,20 @@ @@ -410,32 +404,20 @@
410 last_ext_press = press; 404 last_ext_press = press;
411 } 405 }
412 406
413 - function Point(x , y, time) { 407 + function Point(x , y, p) {
414 this.x = x; 408 this.x = x;
415 this.y = y; 409 this.y = y;
416 - this.time = time; 410 + this.p = p;
417 } 411 }
418 412
419 413
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_MIN = 1.0; 414 + var previousPoint, currentPoint;
  415 + var kWIDTH_MIN = 0.2;
436 var kWIDTH_MAX = 3.0; 416 var kWIDTH_MAX = 3.0;
437 //current width 417 //current width
438 var strokeWidth = 3.0; 418 var strokeWidth = 3.0;
  419 + var rotate_degree = 0;
  420 + var pressure_level = 2048.0;
439 421
440 422
441 function setStrokeWidth(strokeWidth) { 423 function setStrokeWidth(strokeWidth) {
@@ -444,137 +426,89 @@ @@ -444,137 +426,89 @@
444 kWIDTH_MIN = strokeWidth / 3; 426 kWIDTH_MIN = strokeWidth / 3;
445 } 427 }
446 428
447 - function changeWidth(P) {  
448 -  
449 - if (P > P_LOW && P < P_MIDDLE) {  
450 - strokeWidth -= 0.01;  
451 -  
452 - if (strokeWidth < kWIDTH_MIN) {  
453 - strokeWidth = kWIDTH_MIN;  
454 - }  
455 -  
456 - } else if (P >= P_MIDDLE && P < P_HIGN) {  
457 - strokeWidth += 0.005;  
458 - if (strokeWidth > kWIDTH_MAX) {  
459 - strokeWidth = kWIDTH_MAX; 429 + function draw_line(pt) {
  430 + var x = previousPoint.x;
  431 + var y = previousPoint.y;
  432 + var p = previousPoint.p;
  433 + var stepx, stepy ,stepp;
  434 + var times = 1;
  435 +
  436 + var dy = pt.y - previousPoint.y;
  437 + var dx = pt.x - previousPoint.x;
  438 + var dp = pt.p - previousPoint.p;
  439 + var abs_dy = Math.abs(dy);
  440 + var abs_dx = Math.abs(dx);
  441 + if(abs_dy < 1 && abs_dx <1) {
  442 + if(pt.p > p) {
  443 + p = pt.p;
460 } 444 }
461 -  
462 -  
463 - } else if (P >= P_HIGN) {  
464 - strokeWidth += 0.01;  
465 -  
466 - if (strokeWidth > kWIDTH_MAX) {  
467 - strokeWidth = kWIDTH_MAX;  
468 - }  
469 -  
470 -  
471 - } else {  
472 - strokeWidth -= 0.003;  
473 -  
474 - if (strokeWidth < kWIDTH_MIN) {  
475 - strokeWidth = kWIDTH_MIN;  
476 - }  
477 - 445 + var line_width = (kWIDTH_MAX - kWIDTH_MIN)/pressure_level * p + kWIDTH_MIN;
  446 + ctx.beginPath(); //Start path
  447 + ctx.arc(x, y, line_width, 0, Math.PI * 2, true); // Draw a point using the arc function of the canvas with a point structure.
  448 + ctx.fill();
  449 + return;
478 } 450 }
  451 + if (abs_dy >= abs_dx) {
  452 + times += abs_dy;
  453 + stepx = dx * 1.0 / abs_dy;
  454 + stepy = 1.0;
  455 + stepp = dp * 1.0 / abs_dy;
  456 + if (previousPoint.y > pt.y) {
  457 + stepy *= -1;
  458 + }
  459 + }
  460 + else
  461 + {
  462 + times += abs_dx;
  463 + stepx = 1.0;
  464 + stepy = dy * 1.0 / abs_dx;
  465 + stepp = dp * 1.0 / abs_dx;
  466 + if (previousPoint.x > pt.x) {
  467 + stepx *= -1;
  468 + }
  469 + }
  470 +
  471 + while (times > 0) {
  472 + times = times - 1;
  473 + var line_width = (kWIDTH_MAX - kWIDTH_MIN)/pressure_level * p + kWIDTH_MIN;
  474 + ctx.lineWidth = line_width;
  475 + ctx.beginPath(); //Start path
  476 + ctx.arc(x, y, line_width, 0, Math.PI * 2, true); // Draw a point using the arc function of the canvas with a point structure.
  477 + ctx.fill();
  478 + x += stepx;
  479 + y += stepy;
  480 + p += stepp;
  481 + }
  482 + }
479 483
480 - }  
481 -  
482 - function velocityFrom(a,b) {  
483 - var dx = a.x - b.x;  
484 - var dy = a.y - b.y;  
485 - var dist = Math.sqrt(a*a + b*b);  
486 - var v = dist / (a.time - b.time)  
487 - return v;  
488 - }  
489 484
490 function onTouchDownEvent(x, y, p) { 485 function onTouchDownEvent(x, y, p) {
491 previousPoint = null; 486 previousPoint = null;
492 - startPoint = null;  
493 currentPoint = null; 487 currentPoint = null;
494 - lastVelocity = 0;  
495 - //lastWidth = penSize;  
496 - var d = new Date();  
497 - currentPoint = new Point(x, y, d.getTime()); 488 + currentPoint = new Point(x, y, p);
498 previousPoint = currentPoint; 489 previousPoint = currentPoint;
499 - startPoint = previousPoint;  
500 } 490 }
501 491
502 function onTouchMoveEvent(x, y, p) { 492 function onTouchMoveEvent(x, y, p) {
503 -  
504 -  
505 if (previousPoint == null) { 493 if (previousPoint == null) {
506 return; 494 return;
507 } 495 }
508 - startPoint = previousPoint;  
509 - previousPoint = currentPoint;  
510 - var d = new Date();  
511 - currentPoint = new Point(x, y, d.getTime());  
512 -  
513 - var velocity = velocityFrom(currentPoint , previousPoint);  
514 - drawLine(strokeWidth, velocity, p);  
515 - lastVelocity = velocity;  
516 496
  497 + previousPoint = currentPoint;
  498 + currentPoint = new Point(x, y, p);
  499 + draw_line(currentPoint);
517 } 500 }
518 501
519 function onTouchUpEvent(x, y, p) { 502 function onTouchUpEvent(x, y, p) {
520 if (previousPoint == null) { 503 if (previousPoint == null) {
521 return; 504 return;
522 } 505 }
523 - startPoint = previousPoint;  
524 previousPoint = currentPoint; 506 previousPoint = currentPoint;
525 - var d = new Date();  
526 - currentPoint = new Point(x, y, d.getTime());  
527 -  
528 - drawLine(0, lastVelocity, p); 507 + currentPoint = new Point(x, y, 0);
529 508
  509 + draw_line(currentPoint);
530 } 510 }
531 511
532 - function getStrokeWidth(velocity) {  
533 - return penSize - (velocity * STROKE_DES_VELOCITY);  
534 - }  
535 -  
536 -  
537 - function drawLine(currentWidth, velocity, Presure) {  
538 - var mid1 = midPoint(previousPoint, startPoint);  
539 - var mid2 = midPoint(currentPoint, previousPoint);  
540 -  
541 - draw(mid1, previousPoint, mid2,  
542 - currentWidth, velocity, Presure);  
543 - }  
544 -  
545 - function getPt(n1, n2, perc) {  
546 - var diff = n2 - n1;  
547 - return n1 + (diff * perc);  
548 - }  
549 -  
550 - function draw(p0, p1, p2, currentWidth, velocity, p) {  
551 - var xa, xb, ya, yb, x, y;  
552 - var increment;  
553 - if (velocity > MIN_VELOCITY_BOUND && velocity < MAX_VELOCITY_BOUND) {  
554 - increment = DRAWING_CONSTANT - (velocity * INCREMENT_CONSTANT);  
555 - } else {  
556 - increment = MIN_INCREMENT;  
557 - }  
558 -  
559 - for (var i = 0.0; i < 1.0; i += increment) {  
560 - xa = getPt(p0.x, p1.x, i);  
561 - ya = getPt(p0.y, p1.y, i);  
562 - xb = getPt(p1.x, p2.x, i);  
563 - yb = getPt(p1.y, p2.y, i);  
564 -  
565 - x = getPt(xa, xb, i);  
566 - y = getPt(ya, yb, i);  
567 - changeWidth(p);  
568 - ctx.lineWidth = strokeWidth;  
569 - ctx.beginPath(); //Start path  
570 - ctx.arc(x, y, strokeWidth, 0, Math.PI * 2, true); // Draw a point using the arc function of the canvas with a point structure.  
571 - ctx.fill();  
572 - }  
573 - }  
574 -  
575 - function midPoint(p1, p2) {  
576 - return new Point((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2, (p1.time + p2.time) / 2);  
577 - }  
578 //------end 512 //------end
579 513
580 function selectColor(el) { 514 function selectColor(el) {
@@ -973,7 +907,23 @@ @@ -973,7 +907,23 @@
973 var nPress = dv.getUint16(9, true); 907 var nPress = dv.getUint16(9, true);
974 var x = dv.getUint16(11, true); 908 var x = dv.getUint16(11, true);
975 var y = dv.getUint16(13, true); 909 var y = dv.getUint16(13, true);
976 - draw_ext_input(x,y,nPress); 910 + if(14 == type) {
  911 + if(rotate_degree == 0) {
  912 + draw_ext_input(x,y,nPress);
  913 + }
  914 + else if(rotate_degree == 90) {
  915 + draw_ext_input(y, ext_input_panel_width - x ,nPress);
  916 + }
  917 + else if(rotate_degree == 180) {
  918 + draw_ext_input(x, ext_input_panel_height - y);
  919 + }
  920 + else {
  921 + draw_ext_input(ext_input_panel_height - y, ext_input_panel_width -x);
  922 + }
  923 + }
  924 + else {
  925 + draw_ext_input(x,y,nPress);
  926 + }
977 // writeToScreen('<span style="color: green;">pen position: ' + x + ',' + y + ',' + nPress + '</span>'); 927 // writeToScreen('<span style="color: green;">pen position: ' + x + ',' + y + ',' + nPress + '</span>');
978 } 928 }
979 else if( 3 == type ) { 929 else if( 3 == type ) {
@@ -1345,20 +1295,18 @@ @@ -1345,20 +1295,18 @@
1345 if(cur_pen_type == 0) { 1295 if(cur_pen_type == 0) {
1346 ext_input_panel_width = 21000; 1296 ext_input_panel_width = 21000;
1347 ext_input_panel_height = 29800; 1297 ext_input_panel_height = 29800;
  1298 + pressure_level = 2048.0;
1348 var w = ext_input_panel_width * 1.0; 1299 var w = ext_input_panel_width * 1.0;
1349 var h = ext_input_panel_height * 1.0; 1300 var h = ext_input_panel_height * 1.0;
1350 var canvasw = w / h * canvas_height; 1301 var canvasw = w / h * canvas_height;
1351 ratio_x = (canvasw + 0.0) / ext_input_panel_width; 1302 ratio_x = (canvasw + 0.0) / ext_input_panel_width;
1352 ratio_y = (canvas_height + 0.0) / ext_input_panel_height; 1303 ratio_y = (canvas_height + 0.0) / ext_input_panel_height;
1353 - P_LOW = 300;  
1354 - P_MIDDLE = 1000;  
1355 - P_HIGN = 1800; 1304 + rotate_degree = 0;
1356 } 1305 }
1357 else { 1306 else {
1358 - P_LOW = 200;  
1359 - P_MIDDLE = 500;  
1360 - P_HIGN = 800;  
1361 - } 1307 + pressure_level = 1024.0;
  1308 + }
  1309 +
1362 1310
1363 cmd.type = 57; 1311 cmd.type = 57;
1364 cmd.data_len = 1; 1312 cmd.data_len = 1;
@@ -1385,11 +1333,36 @@ @@ -1385,11 +1333,36 @@
1385 } 1333 }
1386 } 1334 }
1387 else if(cmdName == "rotate_ext_input") { 1335 else if(cmdName == "rotate_ext_input") {
  1336 + if(cur_pen_type == 0) {
  1337 + rotate_degree = rotate_degree + 90;
  1338 + if( rotate_degree == 360) {
  1339 + rotate_degree = 0;
  1340 + }
  1341 +
  1342 + if (rotate_degree == 0 || rotate_gree == 180) {
  1343 + var w = ext_input_panel_height * 1.0;
  1344 + var h = ext_input_panel_width * 1.0;
  1345 + var canvasw = w / h * canvas_height;
  1346 + ratio_x = (canvasw + 0.0) / w;
  1347 + ratio_y = (canvas_height + 0.0) / h;
  1348 + }
  1349 + else {
  1350 + var w = ext_input_panel_width * 1.0;
  1351 + var h = ext_input_panel_height * 1.0;
  1352 + var canvasw = w / h * canvas_height;
  1353 + ratio_x = (canvasw + 0.0) / ext_input_panel_width;
  1354 + ratio_y = (canvas_height + 0.0) / ext_input_panel_height;
  1355 + }
  1356 +
  1357 + return;
  1358 + }
  1359 + else {
1388 cmd.type = 57; 1360 cmd.type = 57;
1389 cmd.data_len = 1; 1361 cmd.data_len = 1;
1390 var buffer = new ArrayBuffer(cmd.data_len); 1362 var buffer = new ArrayBuffer(cmd.data_len);
1391 cmd.data = new Uint8Array(buffer, 0, cmd.data_len); 1363 cmd.data = new Uint8Array(buffer, 0, cmd.data_len);
1392 cmd.data[0] = 2; 1364 cmd.data[0] = 2;
  1365 + }
1393 } 1366 }
1394 else if(cmdName == "enter_channel"){ 1367 else if(cmdName == "enter_channel"){
1395 cmd.type = 58; 1368 cmd.type = 58;