Files
uni/year4/semester1/CT404: Graphics & Image Processing/materials/week3/examples/canvasWithMouseExample.html

48 lines
1.5 KiB
HTML

<html>
<head>
<script>
var isMouseDown = false;
function attachEvents() {
document.onmousedown = function (event) {
isMouseDown = true;
draw(event.clientX, event.clientY);
}
document.onmouseup = function (event) {
isMouseDown = false;
}
document.onmousemove = function (event) {
if (isMouseDown) {
draw(event.clientX, event.clientY);
}
}
}
function draw(xoffset, yoffset) {
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
// remove previous translation if any
context.save();
// over-write previous content, with a grey rectangle
context.fillStyle = "#DDDDDD";
context.fillRect(0, 0, 600, 600);
// translate based on position of mouseclick
context.translate(xoffset, yoffset);
// purple rectangle
context.fillStyle = "#CC00FF";
context.fillRect(-25, -25, 50, 50); // centred on coord system
context.restore();
}
</script>
</head>
<body onload="attachEvents(); draw(0,0);">
hello<br>
<div id='canvasdiv' style='position:absolute; left:0px; top:0px;'><canvas id="canvas" width="600"
height="600"></canvas></div>
</body>
</html>