Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class Path
  2. {
  3. constructor(context)
  4. {
  5. if(!(context instanceof CanvasRenderingContext2D)) throw new Error("Invalid Rendering Context");
  6.  
  7. this.context= context;
  8. let nodes = [];
  9. let closed = false;
  10.  
  11. this.getNodes = function ()
  12. {
  13. return nodes;
  14. }
  15.  
  16. this.getPathClose = function ()
  17. {
  18. return closed;
  19. }
  20.  
  21. this.define = function (n, c)
  22. {
  23. if(!(n instanceof Array)) throw new Error("Nodes are provided in arrays.");
  24.  
  25. nodes = n;
  26. closed = c && true;
  27.  
  28. return this;
  29. }
  30.  
  31. this.addNode = function(n)
  32. {
  33. if(!(n instanceof Array)) throw new Error("Invalid Node");
  34. if(!(typeof n[0] == "number" && typeof n[1] == "number" ) ) throw new Error("Invalid Node Coordination");
  35.  
  36. this.getNodes().push(n);
  37. return this;
  38. }
  39.  
  40. return this;
  41. }
  42.  
  43. stroke()
  44. {
  45. var a = this.getNodes(),
  46. i = 0;
  47.  
  48. this.context.beginPath();
  49. this.context.moveTo( a[0][0], a[0][1] );
  50.  
  51. while ( ++i < a.length ) this.context.lineTo( a[i][0], a[i][1] );
  52.  
  53. if( this.getPathClose() ) this.context.closePath();
  54. this.context.stroke();
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement