Skip to content

Instantly share code, notes, and snippets.

@nefanov
Created November 20, 2016 14:30
Show Gist options
  • Save nefanov/aef444f26ba20bb3bfaff33b7287dee8 to your computer and use it in GitHub Desktop.
Save nefanov/aef444f26ba20bb3bfaff33b7287dee8 to your computer and use it in GitHub Desktop.
рисование
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