aboutsummaryrefslogtreecommitdiff
path: root/views/partials/canvas.html
blob: 5be9effc102d5d20acdba74c22186244f795c0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
		<div class="editor">
			<canvas id="canvas" style="background:#fff; border:1px inset #888" width="400" height="200"></canvas><br>
			<button type="button" id="blackbtn">Black</button> <button type="button" id="whitebtn">White</button>
			<button type="button" id="redbtn">Red</button> <button type="button" id="bluebtn">Blue</button><br>
			pen size : <input type="number" id="pensize" value="1" min="1" max="99"><br>
			<button type="button" id="sendbtn">Send</button>
		</div>
		<footer><a href="/about.html">About this site</a></footer>
	</main>
</body>
<script type="text/javascript">
	const canvas = document.getElementById('canvas');
	const ctx = canvas.getContext('2d');
	ctx.getImageData(0, 0, 1, 1); // to trigger librewolf's protection (and allow users to enable canvas before publishing)
	//ctx.fillStyle = "black";
	let isDrawing = false;

	function startDrawing(event) {
 		event.preventDefault();
 		isDrawing = true;
 		draw(event);
	}
	function startDrawingTouch(event) {
 		event.preventDefault();
 		isDrawing = true;
 		drawTouch(event);
	}
	function draw(event) {
		event.preventDefault();
		if (!isDrawing) return;
		const x = event.clientX - canvas.offsetLeft + window.pageXOffset;
		const y = event.clientY - canvas.offsetTop + window.pageYOffset;
		ctx.lineTo(x, y);
		ctx.stroke();
	}
	function drawTouch(event) {
		event.preventDefault();
		if (!isDrawing) return;
		const x = event.touches[0].clientX - canvas.offsetLeft + window.pageXOffset;
		const y = event.touches[0].clientY - canvas.offsetTop + window.pageYOffset;
		ctx.lineTo(x, y);
		ctx.stroke();
	}
	function stopDrawing() {
 		event.preventDefault();
 		isDrawing = false;
 		ctx.beginPath();
	}
	canvas.addEventListener("mousedown", startDrawing);
	canvas.addEventListener("mousemove", draw);
	canvas.addEventListener("mouseup", stopDrawing);
	canvas.addEventListener("mouseout", stopDrawing);
	canvas.addEventListener("touchstart", startDrawingTouch);
	canvas.addEventListener("touchmove", drawTouch);
	canvas.addEventListener("touchend", stopDrawing);
	canvas.addEventListener("touchcancel", stopDrawing);

	function sendPic() {
		fetch("/api/post", {
			method: "POST",
			body: JSON.stringify({ {{ if .threadId }}
				thread: "{{.threadId}}",{{ end }}
				picture: canvas.toDataURL("image/png")
			}),
			headers: {
				"Content-type": "application/json; charset=UTF-8"
			}
		})
			.then((response) => response.json())
  			.then((json) => {
  				if (json.status == "ok") {
  					location.reload();
  				}
  			});
	}
	const sendbtn = document.getElementById('sendbtn');
	sendbtn.addEventListener("click", sendPic);

	const blackbtn = document.getElementById('blackbtn');
	blackbtn.addEventListener("click", () => {
		ctx.strokeStyle="black";
	});

	const whitebtn = document.getElementById('whitebtn');
	whitebtn.addEventListener("click", () => {
		ctx.strokeStyle="white";
	});

	const redbtn = document.getElementById('redbtn');
	redbtn.addEventListener("click", () => {
		ctx.strokeStyle="red";
	});

	const bluebtn = document.getElementById('bluebtn');
	bluebtn.addEventListener("click", () => {
		ctx.strokeStyle="blue";
	});

	const pensize = document.getElementById('pensize');
	pensize.addEventListener("change", () => {
		ctx.lineWidth=pensize.value;
		console.log(pensize.value)
	});
</script>

</html>