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.

124 lines
3.0 KiB
JavaScript

3 years ago
// Great resource from https://stackoverflow.com/a/40700068
// Thank you ConnorFan
5 months ago
var strokeWidth = 8;
3 years ago
var bufferSize;
var svgElement = document.getElementById("svgElement");
var rect = svgElement.getBoundingClientRect();
var path = null;
var strPath;
var buffer = []; // Contains the last positions of the mouse cursor
5 months ago
const startDrawing = (e) => {
e.preventDefault();
// console.log("start");
bufferSize = 2;
3 years ago
path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("fill", "none");
3 years ago
path.setAttribute("stroke", "currentColor");
3 years ago
path.setAttribute("stroke-width", strokeWidth);
buffer = [];
var pt = getMousePosition(e);
appendToBuffer(pt);
strPath = "M" + pt.x + " " + pt.y;
path.setAttribute("d", strPath);
svgElement.appendChild(path);
5 months ago
};
3 years ago
5 months ago
const draw = (e) => {
e.preventDefault();
3 years ago
if (path) {
5 months ago
// console.log("draw");
3 years ago
appendToBuffer(getMousePosition(e));
updateSvgPath();
}
5 months ago
};
3 years ago
5 months ago
const stopDrawing = () => {
3 years ago
if (path) {
5 months ago
// console.log("stop");
3 years ago
path = null;
}
5 months ago
};
3 years ago
3 years ago
var getMousePosition = function (e) {
return {
5 months ago
x: (e.pageX || e?.changedTouches[0]?.pageX) - rect.left,
y: (e.pageY || e?.changedTouches[0]?.pageY) - rect.top,
3 years ago
};
};
var appendToBuffer = function (pt) {
buffer.push(pt);
while (buffer.length > bufferSize) {
buffer.shift();
}
};
// Calculate the average point, starting at offset in the buffer
var getAveragePoint = function (offset) {
var len = buffer.length;
if (len % 2 === 1 || len >= bufferSize) {
var totalX = 0;
var totalY = 0;
var pt, i;
var count = 0;
for (i = offset; i < len; i++) {
count++;
pt = buffer[i];
totalX += pt.x;
totalY += pt.y;
}
return {
x: totalX / count,
y: totalY / count,
};
}
return null;
};
var updateSvgPath = function () {
var pt = getAveragePoint(0);
if (pt) {
// Get the smoothed part of the path that will not change
strPath += " L" + pt.x + " " + pt.y;
// Get the last part of the path (close to the current mouse position)
// This part will change if the mouse moves again
var tmpPath = "";
for (var offset = 2; offset < buffer.length; offset += 2) {
pt = getAveragePoint(offset);
tmpPath += " L" + pt.x + " " + pt.y;
}
// Set the complete current path coordinates
path.setAttribute("d", strPath + tmpPath);
}
};
5 months ago
svgElement.addEventListener("mousedown", (e) => startDrawing(e));
svgElement.addEventListener("touchstart", (e) => startDrawing(e), { passive: false });
svgElement.addEventListener("mousemove", (e) => draw(e));
svgElement.addEventListener("touchmove", (e) => draw(e), { passive: false });
svgElement.addEventListener("mouseup", () => stopDrawing());
svgElement.addEventListener("mouseleave", () => stopDrawing());
svgElement.addEventListener("touchend", () => stopDrawing());
3 years ago
//
//
//
// SAVE THE BRANCH
3 years ago
const form = document.querySelector("form");
3 years ago
3 years ago
form.addEventListener("submit", () => {
3 years ago
let wrapper = document.createElement("div");
wrapper.appendChild(svgElement);
3 years ago
form["content"].value = wrapper.innerHTML;
return true;
});