2019-09-11 16:16:33 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en" dir="ltr">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>MT Request: draw over the image</title>
|
|
|
|
<style media="screen">
|
2019-09-11 19:00:06 +00:00
|
|
|
#sample, svg{
|
|
|
|
position:absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
bottom: 0;
|
|
|
|
right:0;
|
|
|
|
width:100%;
|
|
|
|
height:100%;
|
2019-09-11 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path {
|
|
|
|
fill: none;
|
|
|
|
stroke: red;
|
|
|
|
stroke-width: 2px;
|
|
|
|
}
|
2019-09-11 19:00:06 +00:00
|
|
|
|
|
|
|
body.submitted path{
|
|
|
|
stroke:darkgray;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.submitted .buttons{
|
|
|
|
display:none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#wrapper {
|
|
|
|
height: 600px;
|
|
|
|
width: 600px;
|
|
|
|
position: relative;
|
2019-09-12 12:01:10 +00:00
|
|
|
background:#ccc;
|
2019-09-11 19:00:06 +00:00
|
|
|
|
|
|
|
}
|
2019-09-11 16:16:33 +00:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-09-12 12:01:10 +00:00
|
|
|
<ul>
|
|
|
|
<li>Drag the mouse to trace the line drawing</li>
|
|
|
|
<li>Follow the lines as precise as possible</li>
|
|
|
|
<li>Press submit when you're done.</li>
|
|
|
|
<li>You'll receive a submission token, to fill in at Mechanical Turk</li>
|
|
|
|
</ul>
|
2019-09-11 16:16:33 +00:00
|
|
|
<div id='interface'>
|
2019-09-11 19:00:06 +00:00
|
|
|
<div id='wrapper'>
|
|
|
|
<img src="/latest.svg" id='sample'>
|
2019-09-11 16:16:33 +00:00
|
|
|
<svg id="canvas">
|
2019-09-11 19:00:06 +00:00
|
|
|
<path d="" id="stroke" />
|
2019-09-11 16:16:33 +00:00
|
|
|
</svg>
|
2019-09-11 19:00:06 +00:00
|
|
|
</div>
|
|
|
|
<div class='buttons'>
|
|
|
|
<button id='submit'>Submit</button>
|
|
|
|
<button id='reset'>Reset</button>
|
|
|
|
</div>
|
|
|
|
<div id='message'></div>
|
2019-09-11 16:16:33 +00:00
|
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
let url = window.location.origin.replace('http', 'ws') +'/ws';
|
|
|
|
let svgEl = document.getElementById("canvas");
|
|
|
|
let strokeEl = document.getElementById('stroke');
|
|
|
|
let submitEl = document.getElementById('submit');
|
2019-09-11 19:00:06 +00:00
|
|
|
let resetEl = document.getElementById('reset');
|
|
|
|
let messageEl = document.getElementById('message');
|
|
|
|
|
2019-09-11 16:16:33 +00:00
|
|
|
let strokes = [];
|
|
|
|
let isDrawing = false;
|
|
|
|
let draw = function(e) {
|
|
|
|
let pos = svgEl.getBoundingClientRect()
|
|
|
|
let x = e.x - pos['left'];
|
|
|
|
let y = e.y - pos['top'];
|
|
|
|
strokes.push([x, y, 0]);
|
|
|
|
|
|
|
|
let d = strokes2D(strokes);
|
|
|
|
console.log(d);
|
|
|
|
strokeEl.setAttribute('d', d);
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'move',
|
|
|
|
'direction': [x, y]
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
let penup = function(e) {
|
|
|
|
if(!isDrawing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isDrawing = false;
|
|
|
|
|
|
|
|
document.body.removeEventListener('mousemove', draw);
|
2019-09-11 19:00:06 +00:00
|
|
|
|
|
|
|
let pos = svgEl.getBoundingClientRect()
|
|
|
|
let x = e.x - pos['left'];
|
|
|
|
let y = e.y - pos['top'];
|
2019-09-11 16:16:33 +00:00
|
|
|
|
|
|
|
if(strokes.length > 0){
|
|
|
|
// mark point as last of stroke
|
|
|
|
strokes[strokes.length - 1][2] = 1;
|
|
|
|
}
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'up',
|
2019-09-11 19:00:06 +00:00
|
|
|
'direction': [x, y]
|
2019-09-11 16:16:33 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
};
|
|
|
|
let strokes2D = function(strokes) {
|
|
|
|
// strokes to a d attribute for a path
|
|
|
|
let d = "";
|
|
|
|
let last_stroke = undefined;
|
|
|
|
let cmd = "";
|
|
|
|
for (let stroke of strokes) {
|
|
|
|
if(!last_stroke) {
|
|
|
|
d += `M${stroke[0]},${stroke[1]} `;
|
|
|
|
cmd = 'M';
|
|
|
|
} else {
|
|
|
|
if (last_stroke[2] == 1) {
|
|
|
|
d += " m";
|
|
|
|
cmd = 'm';
|
|
|
|
} else if (cmd != 'l') {
|
|
|
|
d+=' l ';
|
|
|
|
cmd = 'l';
|
|
|
|
}
|
|
|
|
let rel_stroke = [stroke[0] - last_stroke[0], stroke[1] - last_stroke[1]];
|
|
|
|
d += `${rel_stroke[0]},${rel_stroke[1]} `;
|
|
|
|
}
|
|
|
|
last_stroke = stroke;
|
|
|
|
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
2019-09-11 19:00:06 +00:00
|
|
|
|
|
|
|
let startDrawing = function(e){
|
|
|
|
isDrawing = true;
|
|
|
|
// start drawing
|
|
|
|
document.body.addEventListener('mousemove', draw);
|
|
|
|
|
|
|
|
};
|
|
|
|
let reset = function() {
|
|
|
|
strokes = [];
|
|
|
|
strokeEl.setAttribute('d', "");
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'reset',
|
|
|
|
}));
|
|
|
|
}
|
2019-09-11 16:16:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
let socket = new WebSocket(url);
|
|
|
|
|
|
|
|
socket.addEventListener('message', function(e){
|
2019-09-11 19:00:06 +00:00
|
|
|
let msg = JSON.parse(e.data);
|
|
|
|
console.log('receive', msg);
|
|
|
|
if(msg['action'] == 'submitted') {
|
|
|
|
document.body.classList.add('submitted');
|
|
|
|
messageEl.innerHTML = msg['msg'];
|
|
|
|
svgEl.removeEventListener('mousedown', startDrawing);
|
2019-09-11 16:16:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.addEventListener('mouseup', penup);
|
2019-09-11 19:00:06 +00:00
|
|
|
svgEl.addEventListener('mousedown', startDrawing);
|
|
|
|
resetEl.addEventListener('click', reset);
|
2019-09-11 16:16:33 +00:00
|
|
|
submitEl.addEventListener('click', function(e){
|
|
|
|
if(!strokes.length){
|
|
|
|
alert('please draw before submitting');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'submit'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|