Skip to content

Instantly share code, notes, and snippets.

@polatengin
Created October 17, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polatengin/2b26204152753feef99f60979efeb93c to your computer and use it in GitHub Desktop.
Save polatengin/2b26204152753feef99f60979efeb93c to your computer and use it in GitHub Desktop.
HTML5 sayfasında canvas ve javascript kullanarak çizim alanı oluşturma
<!doctype html>
<html>
<head>
<title>Mouse ile serbest çizim alanı</title>
<script>
var canvas, ctx, painting = false, previousMousePosition;
function getMousePosition(event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX – rect.left,
y: event.clientY – rect.top
};
}
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
function handleMouseMove(e) {
var mousePos = getMousePosition(e);
if (painting) {
drawLine(previousMousePosition.x, previousMousePosition.y, mousePos.x, mousePos.y);
previousMousePosition = mousePos;
}
}
window.onload = function () {
canvas = document.getElementById('cizimAlani');
ctx = canvas.getContext('2d');
canvas.addEventListener('mousemove', handleMouseMove, false);
canvas.addEventListener('mousedown', function (e) {
previousMousePosition = getMousePosition(e);
painting = true;
});
canvas.addEventListener('mouseup', function () {
painting = false;
});
};
</script>
</head>
<body>
<canvas id="cizimAlani" width="400" height="400"></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment