forked from kynd/infinite_quickdraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
153 lines (130 loc) · 4.81 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<title>Infinite quick draw</title>
<link href="https://fonts.googleapis.com/css?family=Amatic+SC:400" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<canvas id="paper-canvas" resize></canvas>
<div id="link"><span class="active">Directional</span> | <a href="mutational.html">Mutational</a></div>
<div id="loading-text">Loading</div>
<div id="svg-export">Export SVG</div>
<footer><a href="https://twitter.com/kyndinfo">kynd.info</a> | data from <a href="https://quickdraw.withgoogle.com/data">Quick, Draw!</a></footer>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.11.3/paper-core.min.js"></script>
<script type="text/javascript">
let loadingText = document.getElementById("loading-text");
let canvas = document.getElementById("paper-canvas");
let drawingData, dictData;
let drawingIndex, lineIndex, penTipIndex, paths = [];
let offset, scale, prevPenTip;
let dataReq = new XMLHttpRequest();
dataReq.open("get", "./data/data.json", true);
dataReq.onload = (data)=>{
drawingData = JSON.parse(dataReq.responseText);
onLoadData();
}
dataReq.send(null);
let dictReq = new XMLHttpRequest();
dictReq.open("get", "./data/dict.json", true);
dictReq.onload = (data)=>{
dictData = JSON.parse(dictReq.responseText);
onLoadData();
}
dictReq.send(null);
function onLoadData() {
if (drawingData && dictData) { main(); }
}
function main() {
loadingText.setAttribute("class", "hidden");
let scope = new paper.PaperScope();
scope.setup(canvas);
window.addEventListener("resize", onResize);
onResize();
document.getElementById("svg-export").addEventListener("click", ()=>{
let fileName = "drawing.svg";
let url = "data:image/svg+xml;utf8," + encodeURIComponent(paper.project.exportSVG({asString:true}));
let link = document.createElement("a");
link.download = fileName;
link.href = url;
link.click();
})
scope.view.onMouseUp = ()=> {
clear();
}
scope.view.onFrame = (evt)=> {
if (paths.length == 0) {
paths.unshift(new paper.Path({strokeColor:'black', strokeWidth:0.5}));
scale = new paper.Size(0.1, 0.1);
offset = paper.view.viewSize.subtract(new paper.Size(256, 256).multiply(scale)).divide(new paper.Size(2));
prevPenTip = new paper.Point(paper.view.viewSize.divide(new paper.Size(2)));
}
let penTip = new paper.Point(
drawingData[drawingIndex][lineIndex][0][penTipIndex],
drawingData[drawingIndex][lineIndex][1][penTipIndex])
.multiply(scale).add(offset);
let path = paths[0];
path.strokeColor = 'black'
path.smooth();
path.strokeWidth = 0.5;
if (path.segments.length == 0) {
path.moveTo(penTip);
} else {
path.lineTo(penTip);
}
penTipIndex ++;
let jump = Math.random() < 0.04;
if (penTipIndex >= drawingData[drawingIndex][lineIndex][0].length ) {
penTipIndex = 0;
lineIndex ++;
if (lineIndex >= drawingData[drawingIndex].length) {
jump = true;
} else {
paths.unshift(new paper.Path({strokeColor:'black', strokeWidth:0.5}));
}
}
if (jump) {
let dir = penTip.subtract(prevPenTip);
let key = parseInt(Math.atan2(dir.y, dir.x) * 10);
if (dictData[key]) {
let i = Math.floor(Math.random() * dictData[key].length);
drawingIndex = dictData[key][i][0];
lineIndex = dictData[key][i][1];
penTipIndex = dictData[key][i][2];
sc = Math.random() + 0.02;
scale = new paper.Size(sc, sc);
let np = new paper.Point(
drawingData[drawingIndex][lineIndex][0][penTipIndex],
drawingData[drawingIndex][lineIndex][1][penTipIndex]).multiply(scale);
offset = penTip.subtract(np);
} else {
penTipIndex = lineIndex = 0;
drawingIndex = Math.floor(Math.random() * drawingData.length);
}
paths.unshift(new paper.Path({strokeColor:'black', strokeWidth:0.5}));
if (penTip.x < 0 || penTip.x > paper.view.viewSize.width || penTip.y < 0 || penTip.y > paper.view.viewSize.height) {
offset = new paper.Point(Math.random() * paper.view.viewSize.width, Math.random() * paper.view.viewSize.height).subtract(new paper.Size(128, 128).multiply(scale));
}
}
prevPenTip = penTip;
}
}
function clear() {
for (let i = 0; i < paths.length; i ++) { paths[i].remove();}
paths = [];
drawingIndex = Math.floor(Math.random() * drawingData.length);
lineIndex = penTipIndex = 0;
}
function onResize() {
let w = document.documentElement.clientWidth;
let h = document.documentElement.clientHeight;
canvas.setAttribute("width", w);
canvas.setAttribute("height", h);
paper.view.viewSize = new paper.Size(w, h);
clear();
}
</script>
</body>
</html>