Canvas: pointerevents to move drawing around
- draw a red and a blue square and connect corners of blue square to corners of canvas
- use pointerevents to drag blue square around (hold down mouse key )
- should work for mouse or touchscreen
- event =
- x, y =
- pressure = empty
Source /home/ch45859/web/wlkl.ch/public_html/inf/js/c02canvasPoi.php
<html>
<head>
<title> <?php echo basename(__file__, '.php'); ?> </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, user-scalable=yes"/>
<meta name="HandheldFriendly" content="true" />
<meta name="apple-mobile-web-app-capable" content="YES" />
<style>
canvas {
touch-action:none;
border: 1px solid black;
}
</style>
</head>
<body onload="draw();">
<h1>Canvas: pointerevents to move drawing around</h1>
<ol><li>draw a red and a blue square and connect corners of blue square to corners of canvas
</li><li>use pointerevents to drag blue square around (hold down mouse key )
</li><li>should work for mouse or touchscreen
</li></ol>
<div style="float: left; margin-right: 30px;"> <canvas id="tutorial" width="150" height="150"></canvas> </div>
<ul><li>event = <span id="ty"></span>
</li><li>x, y = <span id="xy"></span>
</li><li>pressure = <span id="press">empty</span>
</li></ul>
<h1 style="clear: both;">Source <?php echo __file__; ?> </h1>
<?php highlight_file(__file__) ?>
</body>
<script>
const can = document.getElementById("tutorial");
const ctx = can.getContext("2d");
var o1 = {x: 30, y:30, w:50, h:50, sel: false}
, cnt = 0;
function draw() {
//a = Object.assign({}, aEv);
ctx.clearRect(0, 0, 150, 150);
// ctx.setTransform(0, 1, -1, 0, 150, 0);
ctx.fillStyle = "rgb(200, 0, 0)";
ctx.fillRect(10, 10, 50, 50);
ctx.fillStyle = "rgba(" + (o1.sel ? "100, 100" : "0, 0") + ", 200, 0.5)";
ctx.fillRect(o1.x, o1.y, o1.w, o1.h);
ctx.beginPath();
ctx.lineWidth = 3;
ctx.strokeStyle = "rgba(0, 0, 200, 0.5)";
ctx.moveTo(o1.x, o1.y);
ctx.lineTo(0, 0);
ctx.moveTo(o1.x + o1.w, o1.y);
ctx.lineTo(150, 0);
ctx.stroke();
ctx.beginPath();
const gradient = ctx.createLinearGradient(0, 0, 150, 0);
// Add color stops
gradient.addColorStop(0, "green");
gradient.addColorStop(0.5, "cyan");
gradient.addColorStop(1, "green");
ctx.strokeStyle = gradient;
ctx.moveTo(o1.x, o1.y+o1.h);
ctx.lineTo(0, 150);
ctx.moveTo(o1.x+o1.w, o1.y+o1.h);
ctx.lineTo(150, 150);
ctx.stroke();
}
function pointerAna(e) {
can.aX = Math.round(e.offsetX);
can.aY = Math.round(e.offsetY);
document.getElementById("ty").innerHTML = e.type + ' ' + (cnt+=1);
document.getElementById("xy").innerHTML = can.aX + ", " + can.aY;
document.getElementById("press").innerHTML = e.pressure;
}
can.addEventListener("pointerdown", (e) => { // select blue square, if cursor on it
pointerAna(e);
if (e.button === 0 && can.aX >= o1.x && can.aX <= o1.x + o1.w && can.aY >= o1.y && can.aY <= o1.y + o1.h ) {
o1.sel = true;
can.mvX = o1.x - can.aX;
can.mvY = o1.y - can.aY;
draw();
// window.requestAnimationFrame(draw);
}
});
can.addEventListener("pointerup", (e) => { // unselect
pointerAna(e);
o1.sel = false;
draw();
});
can.addEventListener("pointermove", (e) => { // move blue square, if it is selected
pointerAna(e);
if (o1.sel) {
o1.x = can.aX + can.mvX;
o1.y = can.aY + can.mvY;
// window.requestAnimationFrame(draw);
draw();
}
});
</script>
</html>