aboutsummaryrefslogtreecommitdiff
path: root/views/partials/canvas.html
blob: d8323f2beb290b66ce03fb61d5fc83d29f337538 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		<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>
			<button type="button" id="magentabtn">Magenta</button> <button type="button" id="cyanbtn">Cyan</button>
			<button type="button" id="yellowbtn">Yellow</button> <button type="button" id="greenbtn">Green</button><br>
			pen size : <input type="number" id="pensize" value="1" min="1" max="99"><br>
			<button type="button" id="sendbtn">Send</button>
		</div>
		<p style="max-width: 400px;margin: auto;margin-bottom: 20px;">Pro tip: it case it ain't obvious you can use white as an eraser<br>
			Librewolf users have to enable canvas access before sending</p>
		<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="#000000";
	});

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

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

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

	const greenbtn = document.getElementById('greenbtn');
	greenbtn.addEventListener("click", () => {
		ctx.strokeStyle="#00ff00";
	});

	const magentabtn = document.getElementById('magentabtn');
	magentabtn.addEventListener("click", () => {
		ctx.strokeStyle="#ff00ff";
	});

	const cyanbtn = document.getElementById('cyanbtn');
	cyanbtn.addEventListener("click", () => {
		ctx.strokeStyle="#00ffff";
	});

	const yellowbtn = document.getElementById('yellowbtn');
	yellowbtn.addEventListener("click", () => {
		ctx.strokeStyle="#ffff00";
	});

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

</html>