|
|
@ -12,6 +12,7 @@ var buffer = []; // Contains the last positions of the mouse cursor
|
|
|
|
|
|
|
|
|
|
|
|
const startDrawing = (e) => {
|
|
|
|
const startDrawing = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// console.log("start");
|
|
|
|
bufferSize = 2;
|
|
|
|
bufferSize = 2;
|
|
|
|
path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
|
|
path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
|
|
path.setAttribute("fill", "none");
|
|
|
|
path.setAttribute("fill", "none");
|
|
|
@ -28,6 +29,7 @@ const startDrawing = (e) => {
|
|
|
|
const draw = (e) => {
|
|
|
|
const draw = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.preventDefault();
|
|
|
|
if (path) {
|
|
|
|
if (path) {
|
|
|
|
|
|
|
|
// console.log("draw");
|
|
|
|
appendToBuffer(getMousePosition(e));
|
|
|
|
appendToBuffer(getMousePosition(e));
|
|
|
|
updateSvgPath();
|
|
|
|
updateSvgPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -35,14 +37,15 @@ const draw = (e) => {
|
|
|
|
|
|
|
|
|
|
|
|
const stopDrawing = () => {
|
|
|
|
const stopDrawing = () => {
|
|
|
|
if (path) {
|
|
|
|
if (path) {
|
|
|
|
|
|
|
|
// console.log("stop");
|
|
|
|
path = null;
|
|
|
|
path = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var getMousePosition = function (e) {
|
|
|
|
var getMousePosition = function (e) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
x: e.pageX - rect.left,
|
|
|
|
x: (e.pageX || e?.changedTouches[0]?.pageX) - rect.left,
|
|
|
|
y: e.pageY - rect.top,
|
|
|
|
y: (e.pageY || e?.changedTouches[0]?.pageY) - rect.top,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -96,12 +99,11 @@ var updateSvgPath = function () {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
svgElement.addEventListener("mousedown", (e) => startDrawing(e));
|
|
|
|
svgElement.addEventListener("mousedown", (e) => startDrawing(e));
|
|
|
|
svgElement.addEventListener("touchstart", (e) => startDrawing(e));
|
|
|
|
svgElement.addEventListener("touchstart", (e) => startDrawing(e), { passive: false });
|
|
|
|
|
|
|
|
|
|
|
|
svgElement.addEventListener("mousemove", (e) => draw(e));
|
|
|
|
svgElement.addEventListener("mousemove", (e) => draw(e));
|
|
|
|
svgElement.addEventListener("touchmove", (e) => draw(e));
|
|
|
|
svgElement.addEventListener("touchmove", (e) => draw(e), { passive: false });
|
|
|
|
|
|
|
|
|
|
|
|
svgElement.addEventListener("mouseup", () => stopDrawing(), { passive: false });
|
|
|
|
svgElement.addEventListener("mouseup", () => stopDrawing());
|
|
|
|
svgElement.addEventListener("mouseleave", () => stopDrawing(), { passive: false });
|
|
|
|
svgElement.addEventListener("mouseleave", () => stopDrawing());
|
|
|
|
svgElement.addEventListener("touchend", () => stopDrawing(), { passive: false });
|
|
|
|
svgElement.addEventListener("touchend", () => stopDrawing());
|
|
|
|
svgElement.addEventListener("touchcancel", () => stopDrawing(), { passive: false });
|
|
|
|
|
|
|
|