guest_worker/www/index.html

196 lines
6.2 KiB
HTML
Raw Normal View History

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">
#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;
}
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;
}
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'>
<div id='wrapper'>
<img src="/latest.svg" id='sample'>
2019-09-11 18:16:33 +02:00
<svg id="canvas">
<path d="" id="stroke" />
2019-09-11 18:16:33 +02:00
</svg>
</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">
let url = window.location.origin.replace('http', 'ws') +'/ws';
let svgEl = document.getElementById("canvas");
let strokeEl = document.getElementById('stroke');
let submitEl = document.getElementById('submit');
let resetEl = document.getElementById('reset');
let messageEl = document.getElementById('message');
2019-09-11 18:16:33 +02:00
let strokes = [];
let isDrawing = false;
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'];
strokes.push([x, y, 0]);
let d = strokes2D(strokes);
console.log(d);
strokeEl.setAttribute('d', d);
currentPoint = {
2019-09-11 18:16:33 +02:00
'action': 'move',
'direction': [e.x/window.innerWidth, e.y/window.innerHeight],
'mouse': isDrawing,
};
console.log([x,y], isDrawing);
2019-09-11 18:16:33 +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;
//document.body.removeEventListener('mousemove', draw);
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',
'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;
}
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 18:16:33 +02:00
let socket = new WebSocket(url);
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){
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
}
});
//svgEl.addEventListener('mousedown', startDrawing);
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>