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

10 months ago
<head>
10 months ago
<title>drawing</title>
10 months ago
<style>
10 months ago
.center{
text-align: center;
}
#drawing{
border: 1px solid #000;
box-shadow: 0px 0px 10px #000;
margin-left: 10px;
}
10 months ago
</style>
</head>
<body>
10 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>
10 months ago
</body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
10 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;
});
10 months ago
var canvas = document.getElementById("drawing");
var ctx = canvas.getContext('2d');
10 months ago
var pos = {x: 0, y: 0};
10 months ago
document.addEventListener('mousemove', draw);
document.addEventListener('mousedown', setPosition);
10 months ago
function setPosition(e){
10 months ago
pos.x = e.clientX - canvas.offsetLeft;
pos.y = e.clientY - canvas.offsetTop;
}
10 months ago
function draw(e){
if(e.buttons !== 1) return;
10 months ago
ctx.lineWidth = lineWidth;
10 months ago
ctx.lineCap = brush;
10 months ago
ctx.strokeStyle = color;
10 months ago
ctx.beginPath();
ctx.moveTo(pos.x,pos.y);
var startPos = {x: pos.x, y: pos.y};
10 months ago
setPosition(e);
10 months ago
ctx.lineTo(pos.x,pos.y);
ctx.stroke();
socket.emit("picture", {
startPos,
pos,
color,
lineWidth
});
10 months ago
}
10 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();
10 months ago
});
</script>