画点
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>画板</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="canvas"></div>
<script>
canvas.onclick = (e) => {
console.log(e)//console.log调试大法
console.log(e.clientX)
console.log(e.clientY)
let div = document.createElement('div')
div.style.position = 'absolute'
div.style.left = e.clientX + 'px'
div.style.top = e.clientY + 'px'
div.style.width = '6px'
div.style.height = '6px'
div.style.marginLeft = '-3px'
div.style.marginRight = '-3px'
div.style.borderRadius = '50%'
div.style.backgroundColor = 'black'
canvas.appendChild(div)
}
</script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#canvas {
height: 100vh;
border: 1px solid red;
}
画线
注:canvas的宽高从一开就确定好的,所以要用JS获取用户宽高
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>画板</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas"></canvas>
<script>
let canvas = document.getElementById("canvas");
canvas.width = document.documentElement.clientWidth//获取用户屏幕参数
canvas.height = document.documentElement.clientHeight
let ctx = canvas.getContext("2d");
let painting = false
ctx.fillStyle = "blue";
canvas.onmousedown = () => {
painting = true
}
canvas.onmousemove = (e) => {
if (painting === true) {
ctx.fillRect(e.clientX - 5, e.clientY - 5, 10, 10);
}
else {
console.log('什么都不做')
}
}
canvas.onmouseup = () => {
painting = false
}
</script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#canvas {
display: block;
}
更流畅的画线
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>画板</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas"></canvas>
<script>
let canvas = document.getElementById("canvas");
canvas.width = document.documentElement.clientWidth//获取用户屏幕参数
canvas.height = document.documentElement.clientHeight
let ctx = canvas.getContext("2d");
ctx.fillStyle = 'black';
ctx.strokeStyle = 'black';
ctx.lineWidth = 20;
ctx.lineCap = "round";
let painting = false
let last
var isTouchDevice = 'ontouchstart' in document.documentElement;
if (isTouchDevice) {
canvas.ontouchmove = (e) => {
let x = e.touches[0].clientX
let y = e.touches[0].clientY
ctx.beginPath();
ctx.arc(x, y, 10, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
}
}
else {
canvas.onmousedown = (e) => {
painting = true
last = [e.clientX, e.clientY]
console.log(last)
}
canvas.onmousemove = (e) => {
if (painting === true) {
console.log(last)
drawline(last[0], last[1], e.clientX, e.clientY)
last = [e.clientX, e.clientY]
}
}
canvas.onmouseup = () => {
painting = false
}
}
function drawline(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
</script>
</body>
</html>