<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvas" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.strokeRect(0,0,100,100);
ctx.fillRect(150,0,100,100);
ctx.strokeStyle = "#f00";
ctx.fillStyle = "#00f";
ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(300,0);
ctx.lineTo(300,150);
ctx.closePath();
ctx.fill();
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8" />
<title>Canvas Workshop</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="myCanvas" width="10000" height="10000"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius = 10;
var x = 600;
var y = 600;
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
}
let move = (e) => {
let rect = e.target.getBoundingClientRect() ;
x = e.clientX - rect.left;
y = e.clientY - rect.top;
draw();
}
canvas.addEventListener('load',drawBall());
canvas.addEventListener('mousedown', ()=> {
canvas.addEventListener('mousemove',move);
});
canvas.addEventListener('mouseup',()=> {
canvas.removeEventListener('mousemove',move);
});
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8" />
<title>Canvas Workshop</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="myCanvas" width="10000" height="10000"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius = 10;
var x = 600;
var y = 600;
function drawBall() {
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(300,0);
ctx.lineTo(300,150);
ctx.closePath();
ctx.fill();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
}
let move = (e) => {
let rect = e.target.getBoundingClientRect() ;
x = e.clientX - rect.left;
y = e.clientY - rect.top;
draw();
}
canvas.addEventListener('load',drawBall());
canvas.addEventListener('mousedown', ()=> {
canvas.addEventListener('mousemove',move);
});
canvas.addEventListener('mouseup',()=> {
canvas.removeEventListener('mousemove',move);
});
</script>
</body>
</html>