内容
<canvas id="draw" width="800" height="800"></canvas>
<script>
let canvas = document.querySelector('#draw');
let ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let isDrawing = false;
let lastX = 0;
let lastY = 0;
ctx.lineWidth = 90;
ctx.lineJoin = 'round';
ctx.fillStyle = 'f00';
ctx.lineCap = 'round';
ctx.strokeStyle = 'f00'
let hue = 0;
let direction = true;
let x = 0;
let y = 0;
function draw(e) {
if (!isDrawing) return;
if (e.type == "mousemove") {
x = e.offsetX;
y = e.offsetY;
}
ctx.strokeStyle = `hsl(${hue},90%,50%`;
if (hue >= 360)
hue = 0;
hue++;
if (ctx.lineWidth > 120 || ctx.lineWidth < 10) {
direction = !direction;
}
if (direction) {
ctx.lineWidth++;
} else {
ctx.lineWidth--
}
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.stroke();
[lastX, lastY] = [x, y]
}
canvas.addEventListener('mousedown',(e)=>{
isDrawing = true;
[lastX,lastY] = [e.offsetX,e.offsetY]
})
canvas.addEventListener('mousemove',draw);
canvas.addEventListener('mouseup',()=>isDrawing = false);
canvas.addEventListener('mouseout',()=>isDrawing = false)
</script>