You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.9 KiB
HTML

11 months ago
<head>
11 months ago
<title>drawing</title>
11 months ago
<style>
11 months ago
.center{
text-align: center;
}
#drawing{
border: 1px solid #000;
box-shadow: 0px 0px 10px #000;
margin-left: 10px;
}
11 months ago
</style>
</head>
<body>
11 months ago
<h1 class="center">drawing editor</h1>
<canvas id="drawing" width="400" height="400"></canvas><br>
Stroke width: <input type="range" min="0.5" max="50" value="1" id="width"><br>
Color: <input type="color" id="color">
Brush: <select id="linecap">
<option value="square">square</option>
<option value="round">round</option>
<option value="butt">butt</option>
</select>
11 months ago
</body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
11 months ago
var lineWidth = 1;
var color = "#000000";
var brush = "square";
document.getElementById("linecap").addEventListener('change', function(){
brush = this.value;
});
document.getElementById("color").addEventListener('change', function(){
color = this.value;
});
document.getElementById("width").addEventListener('change', function(){
lineWidth = this.value;
});
11 months ago
var canvas = document.getElementById("drawing");
var ctx = canvas.getContext('2d');
11 months ago
var pos = {x: 0, y: 0};
11 months ago
document.addEventListener('mousemove', draw);
document.addEventListener('mousedown', setPosition);
11 months ago
function setPosition(e){
11 months ago
pos.x = e.clientX - canvas.offsetLeft;
pos.y = e.clientY - canvas.offsetTop;
}
11 months ago
function draw(e){
if(e.buttons !== 1) return;
11 months ago
ctx.lineWidth = lineWidth;
11 months ago
ctx.lineCap = brush;
11 months ago
ctx.strokeStyle = color;
11 months ago
ctx.beginPath();
ctx.moveTo(pos.x,pos.y);
var startPos = {x: pos.x, y: pos.y};
11 months ago
setPosition(e);
11 months ago
ctx.lineTo(pos.x,pos.y);
ctx.stroke();
socket.emit("picture", {
startPos,
pos,
color,
lineWidth
});
11 months ago
}
11 months ago
socket.on("dingdong", (line) => {
console.log(line);
ctx.lineWidth = line.lineWidth;
ctx.strokeStyle = line.color;
ctx.beginPath();
ctx.moveTo(line.startPos.x, line.startPos.y);
ctx.lineTo(line.pos.x, line.pos.y);
ctx.stroke();
11 months ago
});
</script>