canvas: store user drawings in localStorage
This commit is contained in:
1 parent
dfd2166284
commit
70dcf9542d
2 files changed
+31
-1
No files matched your search
@@ -0,0 +1,139 @@
|
||||
const root = document.documentElement;
|
||||
const canvas = document.querySelector("#canvas");
|
||||
const context = canvas.getContext("2d");
|
||||
const hue = document.querySelector("#hue");
|
||||
const lightness = document.querySelector("#lightness");
|
||||
const weight = document.querySelector("#weight");
|
||||
|
||||
const updateTokens = () => {
|
||||
root.style.setProperty("--color-hue", hue.value);
|
||||
root.style.setProperty("--color-lightness", `${lightness.value}%`);
|
||||
root.style.setProperty("--font-weight", weight.value);
|
||||
};
|
||||
|
||||
hue.addEventListener("input", updateTokens);
|
||||
lightness.addEventListener("input", updateTokens);
|
||||
weight.addEventListener("input", updateTokens);
|
||||
updateTokens();
|
||||
|
||||
let scale = window.devicePixelRatio || 1;
|
||||
|
||||
function resizeCanvas() {
|
||||
const previousScale = scale;
|
||||
const previous = document.createElement("canvas");
|
||||
const hadSize = canvas.dataset.sized === "true";
|
||||
|
||||
if (hadSize) {
|
||||
previous.width = canvas.width;
|
||||
previous.height = canvas.height;
|
||||
previous.getContext("2d").drawImage(canvas, 0, 0);
|
||||
}
|
||||
|
||||
scale = window.devicePixelRatio || 1;
|
||||
canvas.width = Math.round(window.innerWidth * scale);
|
||||
canvas.height = Math.round(window.innerHeight * scale);
|
||||
canvas.style.width = `${window.innerWidth}px`;
|
||||
canvas.style.height = `${window.innerHeight}px`;
|
||||
canvas.dataset.sized = "true";
|
||||
|
||||
context.setTransform(scale, 0, 0, scale, 0, 0);
|
||||
context.lineCap = "round";
|
||||
context.lineJoin = "round";
|
||||
|
||||
if (hadSize) {
|
||||
context.drawImage(
|
||||
previous,
|
||||
0,
|
||||
0,
|
||||
previous.width / previousScale,
|
||||
previous.height / previousScale,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const ink = () => getComputedStyle(document.body).color;
|
||||
const brushSize = () => Number(weight.value) / 100;
|
||||
|
||||
let drawing = false;
|
||||
let lastPoint = null;
|
||||
|
||||
canvas.addEventListener("pointerdown", (event) => {
|
||||
if (event.button !== 0) return;
|
||||
|
||||
drawing = true;
|
||||
lastPoint = { x: event.clientX, y: event.clientY };
|
||||
canvas.setPointerCapture(event.pointerId);
|
||||
|
||||
context.fillStyle = ink();
|
||||
context.beginPath();
|
||||
context.arc(
|
||||
lastPoint.x,
|
||||
lastPoint.y,
|
||||
brushSize() / 2,
|
||||
0,
|
||||
Math.PI * 2,
|
||||
);
|
||||
context.fill();
|
||||
});
|
||||
|
||||
canvas.addEventListener("pointermove", (event) => {
|
||||
if (!drawing || !lastPoint) return;
|
||||
|
||||
const nextPoint = { x: event.clientX, y: event.clientY };
|
||||
|
||||
context.strokeStyle = ink();
|
||||
context.lineWidth = brushSize();
|
||||
context.beginPath();
|
||||
context.moveTo(lastPoint.x, lastPoint.y);
|
||||
context.lineTo(nextPoint.x, nextPoint.y);
|
||||
context.stroke();
|
||||
|
||||
lastPoint = nextPoint;
|
||||
});
|
||||
|
||||
const STORAGE_KEY = "studio-vitali-canvas";
|
||||
|
||||
const saveDrawing = () => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, canvas.toDataURL());
|
||||
} catch {
|
||||
}
|
||||
};
|
||||
|
||||
const restoreDrawing = () => {
|
||||
let saved = null;
|
||||
|
||||
try {
|
||||
saved = localStorage.getItem(STORAGE_KEY);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!saved) return;
|
||||
|
||||
const image = new Image();
|
||||
image.addEventListener("load", () => {
|
||||
context.drawImage(image, 0, 0, window.innerWidth, window.innerHeight);
|
||||
});
|
||||
image.src = saved;
|
||||
};
|
||||
|
||||
const stopDrawing = (event) => {
|
||||
if (!drawing) return;
|
||||
|
||||
drawing = false;
|
||||
lastPoint = null;
|
||||
|
||||
if (canvas.hasPointerCapture(event.pointerId)) {
|
||||
canvas.releasePointerCapture(event.pointerId);
|
||||
}
|
||||
|
||||
saveDrawing();
|
||||
};
|
||||
|
||||
canvas.addEventListener("pointerup", stopDrawing);
|
||||
canvas.addEventListener("pointercancel", stopDrawing);
|
||||
window.addEventListener("resize", resizeCanvas);
|
||||
|
||||
resizeCanvas();
|
||||
restoreDrawing();
|
||||
Reference in new issue
Block a user