2019-09-11 18:16:33 +02: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 21:00:06 +02:00
|
|
|
#sample, svg{
|
|
|
|
position:absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
bottom: 0;
|
|
|
|
right:0;
|
|
|
|
width:100%;
|
|
|
|
height:100%;
|
2019-09-11 18:16:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
path {
|
|
|
|
fill: none;
|
|
|
|
stroke: red;
|
|
|
|
stroke-width: 2px;
|
|
|
|
}
|
2019-09-11 21:00:06 +02:00
|
|
|
|
|
|
|
body.submitted path{
|
|
|
|
stroke:darkgray;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.submitted .buttons{
|
|
|
|
display:none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#wrapper {
|
|
|
|
height: 600px;
|
|
|
|
width: 600px;
|
|
|
|
position: relative;
|
2019-09-12 14:01:10 +02:00
|
|
|
background:#ccc;
|
2019-10-23 22:33:37 +02:00
|
|
|
cursor: url(cursor.png) 6 6, auto;
|
2019-09-11 21:00:06 +02:00
|
|
|
}
|
2019-10-15 12:42:59 +02:00
|
|
|
html, body{
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
}
|
2019-09-11 18:16:33 +02:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-09-12 14:01:10 +02: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 18:16:33 +02:00
|
|
|
<div id='interface'>
|
2019-09-11 21:00:06 +02:00
|
|
|
<div id='wrapper'>
|
2019-10-23 22:33:37 +02:00
|
|
|
<img src="{IMAGE_URL}" id='sample'>
|
2019-09-11 18:16:33 +02:00
|
|
|
<svg id="canvas">
|
2019-09-11 21:00:06 +02:00
|
|
|
<path d="" id="stroke" />
|
2019-09-11 18:16:33 +02:00
|
|
|
</svg>
|
2019-09-11 21:00:06 +02:00
|
|
|
</div>
|
|
|
|
<div class='buttons'>
|
|
|
|
<button id='submit'>Submit</button>
|
|
|
|
<button id='reset'>Reset</button>
|
|
|
|
</div>
|
|
|
|
<div id='message'></div>
|
2019-09-11 18:16:33 +02:00
|
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
2019-10-23 22:33:37 +02:00
|
|
|
let url = window.location.origin.replace('http', 'ws') +'/ws?' + window.location.search.substring(1);
|
2019-09-11 18:16:33 +02:00
|
|
|
let svgEl = document.getElementById("canvas");
|
|
|
|
let strokeEl = document.getElementById('stroke');
|
|
|
|
let submitEl = document.getElementById('submit');
|
2019-09-11 21:00:06 +02:00
|
|
|
let resetEl = document.getElementById('reset');
|
|
|
|
let messageEl = document.getElementById('message');
|
|
|
|
|
2019-10-15 12:42:59 +02:00
|
|
|
|
2019-09-11 18:16:33 +02:00
|
|
|
let strokes = [];
|
|
|
|
let isDrawing = false;
|
2019-10-15 12:42:59 +02:00
|
|
|
let currentPoint = null;
|
|
|
|
|
2019-09-11 18:16:33 +02:00
|
|
|
let draw = function(e) {
|
|
|
|
let pos = svgEl.getBoundingClientRect()
|
|
|
|
let x = e.x - pos['left'];
|
|
|
|
let y = e.y - pos['top'];
|
2019-10-23 22:33:37 +02:00
|
|
|
|
|
|
|
if(isDrawing) {
|
|
|
|
strokes.push([x, y, 0]);
|
|
|
|
let d = strokes2D(strokes);
|
|
|
|
strokeEl.setAttribute('d', d);
|
|
|
|
}
|
2019-09-11 18:16:33 +02:00
|
|
|
|
2019-10-15 12:42:59 +02:00
|
|
|
currentPoint = {
|
2019-09-11 18:16:33 +02:00
|
|
|
'action': 'move',
|
2019-10-15 12:42:59 +02:00
|
|
|
'direction': [e.x/window.innerWidth, e.y/window.innerHeight],
|
|
|
|
'mouse': isDrawing,
|
|
|
|
};
|
|
|
|
console.log([x,y], isDrawing);
|
2019-09-11 18:16:33 +02:00
|
|
|
};
|
2019-10-15 12:42:59 +02:00
|
|
|
|
|
|
|
let interval = window.setInterval(function(){
|
|
|
|
if(currentPoint === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify(currentPoint));
|
|
|
|
}, 200);
|
|
|
|
|
2019-09-11 18:16:33 +02:00
|
|
|
let penup = function(e) {
|
|
|
|
if(!isDrawing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isDrawing = false;
|
|
|
|
|
2019-10-15 12:42:59 +02:00
|
|
|
//document.body.removeEventListener('mousemove', draw);
|
2019-09-11 21:00:06 +02:00
|
|
|
|
|
|
|
let pos = svgEl.getBoundingClientRect()
|
|
|
|
let x = e.x - pos['left'];
|
|
|
|
let y = e.y - pos['top'];
|
2019-09-11 18:16:33 +02: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 21:00:06 +02:00
|
|
|
'direction': [x, y]
|
2019-09-11 18:16:33 +02: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 21:00:06 +02:00
|
|
|
|
|
|
|
let startDrawing = function(e){
|
|
|
|
isDrawing = true;
|
|
|
|
// start drawing
|
2019-10-15 12:42:59 +02:00
|
|
|
//document.body.addEventListener('mousemove', draw);
|
2019-09-11 21:00:06 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
let reset = function() {
|
|
|
|
strokes = [];
|
|
|
|
strokeEl.setAttribute('d', "");
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'reset',
|
|
|
|
}));
|
|
|
|
}
|
2019-09-11 18:16:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
let socket = new WebSocket(url);
|
2019-10-15 12:42:59 +02:00
|
|
|
document.body.addEventListener('mousemove', draw);
|
|
|
|
document.body.addEventListener('mouseup', penup);
|
|
|
|
document.body.addEventListener('mousedown', startDrawing);
|
2019-09-11 18:16:33 +02:00
|
|
|
|
|
|
|
socket.addEventListener('message', function(e){
|
2019-09-11 21:00:06 +02: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 18:16:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-15 12:42:59 +02:00
|
|
|
//svgEl.addEventListener('mousedown', startDrawing);
|
2019-09-11 21:00:06 +02:00
|
|
|
resetEl.addEventListener('click', reset);
|
2019-09-11 18:16:33 +02:00
|
|
|
submitEl.addEventListener('click', function(e){
|
|
|
|
if(!strokes.length){
|
|
|
|
alert('please draw before submitting');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'action': 'submit'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|