Created
November 20, 2016 14:30
-
-
Save nefanov/aef444f26ba20bb3bfaff33b7287dee8 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
window.onload = function() { | |
canvas = document.getElementById("the-canvas"); | |
context = canvas.getContext("2d"); | |
// Подключаем требуемые для рисования события | |
canvas.onmousedown = startDrawing; | |
canvas.onmouseup = stopDrawing; | |
canvas.onmouseout = stopDrawing; | |
canvas.onmousemove = draw; | |
} | |
function startDrawing(e) { | |
// Начинаем рисовать | |
isDrawing = true; | |
// Создаем новый путь (с текущим цветом и толщиной линии) | |
context.beginPath(); | |
// Нажатием левой кнопки мыши помещаем "кисть" на холст | |
context.moveTo(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop); | |
} | |
function draw(e) { | |
if (isDrawing == true) | |
{ | |
// Определяем текущие координаты указателя мыши | |
var x = e.pageX - canvas.offsetLeft; | |
var y = e.pageY - canvas.offsetTop; | |
// Рисуем линию до новой координаты | |
context.lineTo(x, y); | |
context.stroke(); | |
} | |
} | |
function stopDrawing() { | |
isDrawing = false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment