Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. let sketchRNN;
  2. let currentStroke;
  3. let x, y;
  4. let nextPen = 'down';
  5. let seedPath = [];
  6. let seedPoints = [];
  7. let personDrawing = false;
  8.  
  9. function preload() {
  10. sketchRNN = ml5.sketchRNN('catpig');
  11. }
  12.  
  13. function startDrawing() {
  14. personDrawing = true;
  15. x = innerWidth / 2;
  16. y = innerHeight / 2;
  17.  
  18. }
  19.  
  20. function sketchRNNStart() {
  21. personDrawing = false;
  22.  
  23. // Perform RDP Line Simplication
  24. const rdpPoints = [];
  25. const total = seedPoints.length;
  26. const start = seedPoints[0];
  27. const end = seedPoints[total - 1];
  28. rdpPoints.push(start);
  29. rdp(0, total - 1, seedPoints, rdpPoints);
  30. rdpPoints.push(end);
  31.  
  32. // Drawing simplified path
  33. background(255);
  34. stroke(0);
  35. strokeWeight(4);
  36. beginShape();
  37. noFill();
  38. for (let v of rdpPoints) {
  39. vertex(v.x, v.y);
  40. }
  41. endShape();
  42.  
  43. x = rdpPoints[rdpPoints.length-1].x;
  44. y = rdpPoints[rdpPoints.length-1].y;
  45.  
  46.  
  47. seedPath = [];
  48. // Converting to SketchRNN states
  49. for (let i = 1; i < rdpPoints.length; i++) {
  50. let strokePath = {
  51. dx: rdpPoints[i].x - rdpPoints[i-1].x,
  52. dy: rdpPoints[i].y - rdpPoints[i-1].y,
  53. pen: 'down'
  54. }
  55. //line(x, y, x + strokePath.dx, y + strokePath.dy);
  56. //x += strokePath.dx;
  57. //y += strokePath.dy;
  58. seedPath.push(strokePath);
  59. }
  60.  
  61.  
  62.  
  63.  
  64. sketchRNN.generate(seedPath, gotStrokePath);
  65. }
  66.  
  67. function setup() {
  68. let canvas = createCanvas(innerWidth, innerHeight);
  69. canvas.mousePressed(startDrawing);
  70. canvas.mouseReleased(sketchRNNStart);
  71. // x = width / 2;
  72. // y = height / 2;
  73. background(255);
  74. //sketchRNN.generate(gotStrokePath);
  75. console.log('model loaded');
  76. }
  77.  
  78.  
  79. function gotStrokePath(error, strokePath) {
  80. //console.error(error);
  81. //console.log(strokePath);
  82. currentStroke = strokePath;
  83. }
  84.  
  85. function draw() {
  86. translate(innerWidth / 2, innerHeight / 2);
  87. stroke(0);
  88. strokeWeight(4);
  89.  
  90.  
  91. if (personDrawing) {
  92. // let strokePath = {
  93. // dx: mouseX - pmouseX,
  94. // dy: mouseY - pmouseY,
  95. // pen: 'down'
  96. // }
  97. // line(x, y, x + strokePath.dx, y + strokePath.dy);
  98. // x += strokePath.dx;
  99. // y += strokePath.dy;
  100. // seedPath.push(strokePath);
  101.  
  102. line(innerWidth / 2, innerHeight / 2, innerWidth / 2, innerHeight / 2);
  103. seedPoints.push(createVector(mouseX, mouseY));
  104. }
  105.  
  106. if (currentStroke) {
  107.  
  108. if (nextPen == 'end') {
  109. sketchRNN.reset();
  110. sketchRNNStart();
  111. currentStroke = null;
  112. nextPen = 'down';
  113. return;
  114. }
  115.  
  116. if (nextPen == 'down') {
  117. line(x, y, x + currentStroke.dx, y + currentStroke.dy);
  118. }
  119. x += currentStroke.dx;
  120. y += currentStroke.dy;
  121. nextPen = currentStroke.pen;
  122. currentStroke = null;
  123. sketchRNN.generate(gotStrokePath);
  124.  
  125. }
  126.  
  127.  
  128. }
  129.  
  130. setTimeout(()=>startDrawing(), 125);
  131. setTimeout(()=>
  132. sketchRNNStart(),
  133. 5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement