您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

pencil.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (function(){ //Code isolation
  2. //Indicates the id of the line the user is currently drawing or an empty string while the user is not drawing
  3. var curLineId = "",
  4. lastTime = performance.now(); //The time at which the last point was drawn
  5. function startLine (x,y) {
  6. curLineId = Tools.generateUID("l"); //"l" for line
  7. Tools.drawAndSend({
  8. 'type' : 'line',
  9. 'id' : curLineId
  10. });
  11. //Immediatly add a point to the line
  12. continueLine(x,y);
  13. }
  14. function continueLine (x,y){
  15. /*Wait 50ms before adding any point to the currently drawing line.
  16. This allows the animation to be smother*/
  17. if (curLineId !== "" &&
  18. performance.now() - lastTime > 50) {
  19. Tools.drawAndSend({
  20. 'type' : 'point',
  21. 'line' : curLineId,
  22. 'x' : x,
  23. 'y' : y
  24. });
  25. lastTime = performance.now();
  26. }
  27. }
  28. function stopLine (x,y){
  29. //Add a last point to the line
  30. continueLine(x,y);
  31. curLineId = "";
  32. }
  33. function draw(data) {
  34. switch(data.type) {
  35. case "line":
  36. renderingLine = createLine(data.id);
  37. break;
  38. case "point":
  39. var line = (renderingLine.id == data.line) ? renderingLine : svg.getElementById(data.line);
  40. if (!line) {
  41. console.log("Pencil: Hmmm... I received a point of a line I don't know...");
  42. }
  43. addPoint(line, data.x, data.y);
  44. break;
  45. case "endline":
  46. //TODO?
  47. break;
  48. default:
  49. console.log("Pencil: Draw instruction with unknown type. ", data);
  50. break;
  51. }
  52. }
  53. var svg = Tools.svg;
  54. function addPoint (line, x,y) {
  55. var point = svg.createSVGPoint();
  56. point.x = x; point.y = y;
  57. line.points.appendItem(point);
  58. }
  59. function createLine(id) {
  60. var line = document.createElementNS(svg.namespaceURI, "polyline");
  61. line.id = id;
  62. svg.appendChild(line);
  63. return line;
  64. }
  65. Tools.add({ //The new tool
  66. "name" : "Pencil",
  67. "listeners" : {
  68. "press" : startLine,
  69. "move" : continueLine,
  70. "release" : stopLine,
  71. },
  72. "draw" : draw
  73. });
  74. })(); //End of code isolation