110 lines
2.8 KiB
JavaScript
110 lines
2.8 KiB
JavaScript
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 stopDrawing = (event) => {
|
|
if (!drawing) return;
|
|
|
|
drawing = false;
|
|
lastPoint = null;
|
|
|
|
if (canvas.hasPointerCapture(event.pointerId)) {
|
|
canvas.releasePointerCapture(event.pointerId);
|
|
}
|
|
};
|
|
|
|
canvas.addEventListener("pointerup", stopDrawing);
|
|
canvas.addEventListener("pointercancel", stopDrawing);
|
|
window.addEventListener("resize", resizeCanvas);
|
|
|
|
resizeCanvas();
|