xxxxxxxxxx
101
let sketchRNN;
let currentStroke;
let x, y;
let nextPen = "down";
let seedPath = [];
let seedPoints = [];
let personDrawing = false;
function preload() {
sketchRNN = ml5.sketchRNN("angel");
}
function startDrawing() {
personDrawing = true;
x = mouseX;
y = mouseY;
}
function sketchRNNStart() {
personDrawing = false;
// RDP Line Simplification
const rdpPoints = [];
const total = seedPoints.length;
const start = seedPoints[0];
const end = seedPoints[total - 1];
rdpPoints.push(start);
rdp(0, total-1, seedPoints, rdpPoints);
rdpPoints.push(end);
// Drawing simplified path
//background(220);
stroke(0);
beginShape();
noFill();
for (let v of rdpPoints) {
vertex(v.x, v.y);
}
endShape();
// Starting machine drawing from last human point
let len = rdpPoints.length;
x = rdpPoints[len-1].x;
y = rdpPoints[len-1].y;
seedPath = [];
// Converting to SketchRNN states
for(let i = 1; i < len; i++) {
let strokePath ={
dx: rdpPoints[i].x - rdpPoints[i-1].x,
dy: rdpPoints[i].y - rdpPoints[i-1].y,
pen: "down"
};
seedPath.push(strokePath);
}
sketchRNN.generate(seedPath, gotStrokePath);
}
function setup() {
let canvas = createCanvas(400, 400);
canvas.mousePressed(startDrawing);
canvas.mouseReleased(sketchRNNStart);
background(220);
console.log("model loaded");
}
function gotStrokePath(error, strokePath) {
currentStroke = strokePath;
}
function draw() {
stroke(0);
strokeWeight(4);
if (personDrawing) {
line(mouseX, mouseY, pmouseX, pmouseY);
seedPoints.push(createVector(mouseX, mouseY));
}
if (currentStroke) {
if (nextPen == "end") {
sketchRNN.reset();
sketchRNNStart();
currentStroke = null;
nextPen = "down";
return;
}
if (nextPen == "down") {
line(x, y, x + currentStroke.dx, y+ currentStroke.dy);
}
x += currentStroke.dx;
y += currentStroke.dy;
nextPen = currentStroke.pen;
currentStroke = null;
sketchRNN.generate(gotStrokePath);
}
}