-
-
Save parzibyte/745009a221dce0be4efec3e2b76c3326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
____ _____ _ _ _ | |
| _ \ | __ \ (_) | | | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___ | |
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \ | |
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/ | |
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___| | |
__/ | __/ | | |
|___/ |___/ | |
____________________________________ | |
/ Si necesitas ayuda, contáctame en \ | |
\ https://parzibyte.me / | |
------------------------------------ | |
\ ^__^ | |
\ (oo)\_______ | |
(__)\ )\/\ | |
||----w | | |
|| || | |
Creado por Parzibyte (https://parzibyte.me). | |
------------------------------------------------------------------------------------------------ | |
| IMPORTANTE | | |
Si vas a borrar este encabezado, considera: | |
Seguirme: https://parzibyte.me/blog/sigueme/ | |
Y compartir mi blog con tus amigos | |
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1 | |
Twitter: https://twitter.com/parzibyte | |
Facebook: https://facebook.com/parzibyte.fanpage | |
Instagram: https://instagram.com/parzibyte | |
Hacer una donación vía PayPal: https://paypal.me/LuisCabreraBenito | |
------------------------------------------------------------------------------------------------ | |
*/ | |
const $canvas = document.querySelector("#canvas"); | |
const contexto = $canvas.getContext("2d"); | |
const COLOR = "black"; | |
const GROSOR = 2; | |
let xAnterior = 0, yAnterior = 0, xActual = 0, yActual = 0; | |
const obtenerXReal = (clientX) => clientX - $canvas.getBoundingClientRect().left; | |
const obtenerYReal = (clientY) => clientY - $canvas.getBoundingClientRect().top; | |
let haComenzadoDibujo = false; // Bandera que indica si el usuario está presionando el botón del mouse sin soltarlo | |
$canvas.addEventListener("mousedown", evento => { | |
// En este evento solo se ha iniciado el clic, así que dibujamos un punto | |
xAnterior = xActual; | |
yAnterior = yActual; | |
xActual = obtenerXReal(evento.clientX); | |
yActual = obtenerYReal(evento.clientY); | |
contexto.beginPath(); | |
contexto.fillStyle = COLOR; | |
contexto.fillRect(xActual, yActual, GROSOR, GROSOR); | |
contexto.closePath(); | |
// Y establecemos la bandera | |
haComenzadoDibujo = true; | |
}); | |
$canvas.addEventListener("mousemove", (evento) => { | |
if (!haComenzadoDibujo) { | |
return; | |
} | |
// El mouse se está moviendo y el usuario está presionando el botón, así que dibujamos todo | |
xAnterior = xActual; | |
yAnterior = yActual; | |
xActual = obtenerXReal(evento.clientX); | |
yActual = obtenerYReal(evento.clientY); | |
contexto.beginPath(); | |
contexto.moveTo(xAnterior, yAnterior); | |
contexto.lineTo(xActual, yActual); | |
contexto.strokeStyle = COLOR; | |
contexto.lineWidth = GROSOR; | |
contexto.stroke(); | |
contexto.closePath(); | |
}); | |
["mouseup", "mouseout"].forEach(nombreDeEvento => { | |
$canvas.addEventListener(nombreDeEvento, () => { | |
haComenzadoDibujo = false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment