-1
Hello. I need some function to check if my canvas was designed by the user. The code is as follows::
<canvas id="canvas">
Este navegador não suporta esta função.
</canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
var context = canvas.getContext('2d');
var start = function(coors) {
context.beginPath();
context.moveTo(coors.x, coors.y);
this.isDrawing = true;
};
var move = function(coors) {
if (this.isDrawing) {
context.strokeStyle = "#000";
context.lineJoin = "round";
context.lineWidth = 3;
context.lineTo(coors.x, coors.y);
context.stroke();
}
};
var stop = function(coors) {
if (this.isDrawing) {
this.touchmove(coors);
this.isDrawing = false;
}
};
var drawer = {
isDrawing: false,
mousedown: start,
mousemove: move,
mouseup: stop,
touchstart: start,
touchmove: move,
touchend: stop
};
var draw = function(e) {
var coors = {
x: e.clientX || e.targetTouches[0].pageX,
y: e.clientY || e.targetTouches[0].pageY
};
drawer[e.type](coors);
}
canvas.addEventListener('mousedown', draw, false);
canvas.addEventListener('mousemove', draw, false);
canvas.addEventListener('mouseup', draw, false);
canvas.addEventListener('touchstart', draw, false);
canvas.addEventListener('touchmove', draw, false);
canvas.addEventListener('touchend', draw, false);
var go = function(e) {
draw(e);
}
};
document.getElementById('divCanvas').addEventListener('mousedown', go, false);
document.getElementById('divCanvas').addEventListener('touchstart', go, false);
// prevent elastic scrolling
document.body.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);
// end body:touchmove
window.onresize = function(e) {
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
};
$('.aviso2').click(function(){
context.clearRect(0, 0, canvas.width, canvas.height);
});
</script>
The user presses the mouse or touch and draws following his command. I need some function to check if the user really drew on the canvas. I need to see if the canvas is not blank.
I’ve tried using a slight gambit for that. When the user clicks the mouse or touch, I increment a variable with the time it is holding the click/mouse. So at the end, I do an if to see if this time was less than 5 seconds for example. If it is, it’s because he didn’t draw or draw incorrectly (just scribbled something fast).
There’s like?
What have you tried to do? Could you add to the question?
– Boneco Sinforoso
Sure, I’ll add
– Vítor Souza