0. template 코드
1. index.html 에 src 추가하기
<html>
<head>
<meta charset="UTF-8">
<script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
</head>
<body></body>
</html>
2. QuickStart
- 모델을 preload하기 때문에 callback 파라미터는 필요 없다.
- models 변수 대신에 특정 스트링 배열로 어떤 모델을 호출할지 정한다.
let sketchRNN;
function preload(){
sketchRNN = ml5.sketchRNN('cat');
}
function setup() {
createCanvas(400, 400);
console.log('model loaded');
}
function draw() {
background(220);
}
3. gotStrokePath() 메소드
function gotStrokePath(error, strokePath) {
console.log(strokePath);
}
Object {dx: -7.009424007547129, dy: -2.5481305925730413, pen: "down"}
dx: -7.009424007547129
dy: -2.5481305925730413
pen: "down"
4. initial stroke, many strokes
let sketchRNN;
let currentStroke;
let x, y;
function setup() {
createCanvas(400, 400);
sketchRNN.generate(gotStrokePath);
x = width/2;
y = height/2;
console.log('model loaded');
}
function draw() {
background(220);
if (currentStroke) {
stroke(0);
strokeWeight(4);
line(x, y, x+ currentStroke.dx, y + currentStroke.dy);
x += currentStroke.dx;
y += currentStroke.dy;
currentStroke=null;
sketchRNN.generate(gotStrokePath);
}
}
5. pen 의 상태
let nextPen = 'down';
function draw(){
if (nextPen == 'end') {
noLoop();
return;
}
if (nextPen == 'down') {
line(x, y, x+ currentStroke.dx, y + currentStroke.dy);
}
x += currentStroke.dx;
y += currentStroke.dy;
nextPen = currentStroke.pen;
}
6. 내 스케치와 SketchRNN 구분하기
let seedPath = [];
let personDrawing = false;
function startDrawing() {
console.log("person is drawing");
personDrawing = true;
x = mouseX;
y = mouseY;
}
function sketchRNNStart(){
console.log("model is drawing");
personDrawing = false;
sketchRNN.generate(gotStrokePath);
}
function setup(){
let canvas = createCanvas(400, 400);
canvas.mousePressed(startDrawing);
canvas.mouseReleased(sketchRNNStart);
}
function draw(){
stroke(0);
strokeWeight(4);
if (personDrawing) {
let strokePath = {
dx: mouseX - pmouseX,
dy: mouseY - pmouseY,
pen: 'down'
}
line(x, y, x + strokePath.dx, y + strokePath.dy);
x += strokePath.dx;
y += strokePath.dy;
seedPath.push(strokePath);
}
}
7. 내가 그린 것을 읽고, 그 이후의 stroke 를 그리도록 하기
- seedPath 데이터를 알려줘야 함
- sketchRNN.generate(gotStrokePath);
- generate 함수는 기존의 stroke 데이터를 변수로 받을 수 있다.
function sketchRNNStart() {
console.log("model is drawing");
personDrawing = false;
sketchRNN.generate(seedPath, gotStrokePath);
}
8. RDP Line Simplification 알고리즘과 결합