-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.js
65 lines (55 loc) · 1.75 KB
/
mouse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var myCanvas = document.getElementById('area_de_dibujo');
var prevX = 0;
var prevY = 0;
var isDrawing = false;
var limpiar = document.getElementById('botoncito');
var colorButtons = document.getElementsByClassName('colorButton');
var selectColor = 'black';
myCanvas.addEventListener('mousedown', mouseDown);
myCanvas.addEventListener('mouseup', mouseUp);
myCanvas.addEventListener('mousemove', mouseMove);
function getCoords(event) {
const canvas = event.target
const x = event.clientX - canvas.offsetLeft; // le resta lo que esta por fuera del canvas (el margen)
const y = event.clientY - canvas.offsetTop; // y asi la esquina del canvas cuenta como coord 0,0
return { x, y }
}
function dibujarLinea(color, canvas, prevX, prevY, currX, currY) {
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.strokeStyle = color;
ctx.linewidth =3;
ctx.moveTo(prevX,prevY);
ctx.lineTo(currX,currY);
ctx.stroke();
ctx.closePath();
}
var colorcito = 'black'
function mouseDown(event) {
const {x, y} = getCoords(event);
console.log(event);
isDrawing = true;
prevX = x;
prevY = y;
}
function mouseUp(event) {
console.log(event);
isDrawing = false;
}
function mouseMove(event) {
if(!isDrawing) return;
const {x, y} = getCoords(event);
dibujarLinea(selectColor, event.target, prevX, prevY, x, y);
prevX = x;
prevY = y;
}
limpiar.addEventListener('click', function(){
const context = myCanvas.getContext('2d');
context.clearRect(0, 0, myCanvas.width, myCanvas.height);
})
for (let index = 0; index < colorButtons.length; index++) {
const colorButton = colorButtons[index];
colorButton.addEventListener('click', function(event){
selectColor = event.target.id;
});
}