guest_worker/www/index.html

197 lines
6.3 KiB
HTML
Raw Normal View History

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">
#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;
}
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-10-23 20:33:37 +00:00
cursor: url(cursor.png) 6 6, auto;
}
html, body{
height: 100%;
width: 100%;
}
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'>
<div id='wrapper'>
2019-10-23 20:33:37 +00:00
<img src="{IMAGE_URL}" id='sample'>
2019-09-11 16:16:33 +00:00
<svg id="canvas">
<path d="" id="stroke" />
2019-09-11 16:16:33 +00:00
</svg>
</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">
2019-10-23 20:33:37 +00:00
let url = window.location.origin.replace('http', 'ws') +'/ws?' + window.location.search.substring(1);
2019-09-11 16:16:33 +00:00
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 16:16:33 +00:00
let strokes = [];
let isDrawing = false;
let currentPoint = null;
2019-09-11 16:16:33 +00:00
let draw = function(e) {
let pos = svgEl.getBoundingClientRect()
let x = e.x - pos['left'];
let y = e.y - pos['top'];
2019-10-23 20:33:37 +00:00
if(isDrawing) {
strokes.push([x, y, 0]);
let d = strokes2D(strokes);
strokeEl.setAttribute('d', d);
}
2019-09-11 16:16:33 +00:00
currentPoint = {
2019-09-11 16:16:33 +00:00
'action': 'move',
'direction': [e.x/window.innerWidth, e.y/window.innerHeight],
'mouse': isDrawing,
};
console.log([x,y], isDrawing);
2019-09-11 16:16:33 +00:00
};
let interval = window.setInterval(function(){
if(currentPoint === null) {
return
}
socket.send(JSON.stringify(currentPoint));
}, 200);
2019-09-11 16:16:33 +00: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 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',
'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;
}
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);
document.body.addEventListener('mousemove', draw);
document.body.addEventListener('mouseup', penup);
document.body.addEventListener('mousedown', startDrawing);
2019-09-11 16:16:33 +00: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 16:16:33 +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>