2022-11-29 14:55:25 -06:00
|
|
|
// Layering
|
|
|
|
const imageCollection = layers.registerCollection(
|
|
|
|
"image",
|
2022-12-07 21:26:37 -06:00
|
|
|
{
|
|
|
|
w: parseInt(
|
2022-12-19 12:45:09 -06:00
|
|
|
(localStorage &&
|
|
|
|
localStorage.getItem("openoutpaint/settings.canvas-width")) ||
|
|
|
|
2048
|
2022-12-07 21:26:37 -06:00
|
|
|
),
|
|
|
|
h: parseInt(
|
2022-12-19 12:45:09 -06:00
|
|
|
(localStorage &&
|
|
|
|
localStorage.getItem("openoutpaint/settings.canvas-height")) ||
|
|
|
|
2048
|
2022-12-07 21:26:37 -06:00
|
|
|
),
|
|
|
|
},
|
2022-11-29 14:55:25 -06:00
|
|
|
{
|
|
|
|
name: "Image Layers",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const bgLayer = imageCollection.registerLayer("bg", {
|
|
|
|
name: "Background",
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "background",
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
2022-12-27 15:50:00 -06:00
|
|
|
|
|
|
|
bgLayer.canvas.classList.add("pixelated");
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const imgLayer = imageCollection.registerLayer("image", {
|
|
|
|
name: "Image",
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "image",
|
2022-12-04 14:02:46 -06:00
|
|
|
ctxOptions: {desynchronized: true},
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
const maskPaintLayer = imageCollection.registerLayer("mask", {
|
|
|
|
name: "Mask Paint",
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "mask",
|
2022-12-04 14:02:46 -06:00
|
|
|
ctxOptions: {desynchronized: true},
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
const ovLayer = imageCollection.registerLayer("overlay", {
|
|
|
|
name: "Overlay",
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "display",
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
const debugLayer = imageCollection.registerLayer("debug", {
|
|
|
|
name: "Debug Layer",
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "display",
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
const imgCanvas = imgLayer.canvas; // where dreams go
|
|
|
|
const imgCtx = imgLayer.ctx;
|
|
|
|
|
|
|
|
const maskPaintCanvas = maskPaintLayer.canvas; // where mouse cursor renders
|
|
|
|
const maskPaintCtx = maskPaintLayer.ctx;
|
|
|
|
|
|
|
|
maskPaintCanvas.classList.add("mask-canvas");
|
|
|
|
|
|
|
|
const ovCanvas = ovLayer.canvas; // where mouse cursor renders
|
|
|
|
const ovCtx = ovLayer.ctx;
|
|
|
|
|
|
|
|
const debugCanvas = debugLayer.canvas; // where mouse cursor renders
|
|
|
|
const debugCtx = debugLayer.ctx;
|
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
/* WIP: Most cursors shouldn't need a zoomable canvas */
|
|
|
|
/** @type {HTMLCanvasElement} */
|
|
|
|
const uiCanvas = document.getElementById("layer-overlay"); // where mouse cursor renders
|
|
|
|
uiCanvas.width = uiCanvas.clientWidth;
|
|
|
|
uiCanvas.height = uiCanvas.clientHeight;
|
2022-12-04 14:02:46 -06:00
|
|
|
const uiCtx = uiCanvas.getContext("2d", {desynchronized: true});
|
2022-12-03 17:00:10 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
/**
|
2022-12-16 19:57:28 -06:00
|
|
|
* Here we setup canvas dynamic scaling
|
2022-11-29 14:55:25 -06:00
|
|
|
*/
|
2022-12-16 19:57:28 -06:00
|
|
|
(() => {
|
2022-12-19 12:45:09 -06:00
|
|
|
let expandSize = localStorage.getItem("openoutpaint/expand-size") || 1024;
|
2022-12-16 19:57:28 -06:00
|
|
|
expandSize = parseInt(expandSize, 10);
|
2022-12-07 15:45:51 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
const askSize = () => {
|
|
|
|
const by = prompt("How much do you want to expand by?", expandSize);
|
2022-11-30 15:46:03 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
if (!by) return null;
|
|
|
|
else {
|
|
|
|
const len = parseInt(by, 10);
|
2022-12-19 12:45:09 -06:00
|
|
|
localStorage.setItem("openoutpaint/expand-size", len);
|
2022-12-16 19:57:28 -06:00
|
|
|
expandSize = len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
};
|
2022-11-30 15:46:03 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
const leftButton = makeElement("button", -64, 0);
|
|
|
|
leftButton.classList.add("expand-button", "left");
|
|
|
|
leftButton.style.width = "64px";
|
|
|
|
leftButton.style.height = `${imageCollection.size.h}px`;
|
|
|
|
leftButton.addEventListener("click", () => {
|
|
|
|
let size = null;
|
|
|
|
if ((size = askSize())) {
|
|
|
|
imageCollection.expand(size, 0, 0, 0);
|
|
|
|
drawBackground();
|
|
|
|
const newLeft = -imageCollection.inputOffset.x - imageCollection.origin.x;
|
|
|
|
leftButton.style.left = newLeft - 64 + "px";
|
|
|
|
topButton.style.left = newLeft + "px";
|
|
|
|
bottomButton.style.left = newLeft + "px";
|
|
|
|
topButton.style.width = imageCollection.size.w + "px";
|
|
|
|
bottomButton.style.width = imageCollection.size.w + "px";
|
|
|
|
}
|
|
|
|
});
|
2022-11-30 15:46:03 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
const rightButton = makeElement("button", imageCollection.size.w, 0);
|
|
|
|
rightButton.classList.add("expand-button", "right");
|
|
|
|
rightButton.style.width = "64px";
|
|
|
|
rightButton.style.height = `${imageCollection.size.h}px`;
|
|
|
|
rightButton.addEventListener("click", () => {
|
|
|
|
let size = null;
|
|
|
|
if ((size = askSize())) {
|
|
|
|
imageCollection.expand(0, 0, size, 0);
|
|
|
|
drawBackground();
|
|
|
|
rightButton.style.left =
|
|
|
|
parseInt(rightButton.style.left, 10) + size + "px";
|
|
|
|
topButton.style.width = imageCollection.size.w + "px";
|
|
|
|
bottomButton.style.width = imageCollection.size.w + "px";
|
|
|
|
}
|
|
|
|
});
|
2022-11-30 15:46:03 -06:00
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
const topButton = makeElement("button", 0, -64);
|
|
|
|
topButton.classList.add("expand-button", "top");
|
|
|
|
topButton.style.height = "64px";
|
|
|
|
topButton.style.width = `${imageCollection.size.w}px`;
|
|
|
|
topButton.addEventListener("click", () => {
|
|
|
|
let size = null;
|
|
|
|
if ((size = askSize())) {
|
|
|
|
imageCollection.expand(0, size, 0, 0);
|
|
|
|
drawBackground();
|
|
|
|
const newTop = -imageCollection.inputOffset.y - imageCollection.origin.y;
|
|
|
|
topButton.style.top = newTop - 64 + "px";
|
|
|
|
leftButton.style.top = newTop + "px";
|
|
|
|
rightButton.style.top = newTop + "px";
|
|
|
|
leftButton.style.height = imageCollection.size.h + "px";
|
|
|
|
rightButton.style.height = imageCollection.size.h + "px";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const bottomButton = makeElement("button", 0, imageCollection.size.h);
|
|
|
|
bottomButton.classList.add("expand-button", "bottom");
|
|
|
|
bottomButton.style.height = "64px";
|
|
|
|
bottomButton.style.width = `${imageCollection.size.w}px`;
|
|
|
|
bottomButton.addEventListener("click", () => {
|
|
|
|
let size = null;
|
|
|
|
if ((size = askSize())) {
|
|
|
|
imageCollection.expand(0, 0, 0, size);
|
|
|
|
drawBackground();
|
|
|
|
bottomButton.style.top =
|
|
|
|
parseInt(bottomButton.style.top, 10) + size + "px";
|
|
|
|
leftButton.style.height = imageCollection.size.h + "px";
|
|
|
|
rightButton.style.height = imageCollection.size.h + "px";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
debugLayer.hide(); // Hidden by default
|
|
|
|
|
|
|
|
// Where CSS and javascript magic happens to make the canvas viewport work
|
2022-11-29 14:55:25 -06:00
|
|
|
/**
|
|
|
|
* The global viewport object (may be modularized in the future). All
|
|
|
|
* coordinates given are of the center of the viewport
|
|
|
|
*
|
|
|
|
* cx and cy are the viewport's world coordinates, scaled to zoom level.
|
|
|
|
* _x and _y are actual coordinates in the DOM space
|
|
|
|
*
|
|
|
|
* The transform() function does some transforms and writes them to the
|
|
|
|
* provided element.
|
|
|
|
*/
|
|
|
|
const viewport = {
|
|
|
|
get cx() {
|
|
|
|
return this._x * this.zoom;
|
|
|
|
},
|
|
|
|
|
|
|
|
set cx(v) {
|
|
|
|
return (this._x = v / this.zoom);
|
|
|
|
},
|
|
|
|
_x: 0,
|
|
|
|
get cy() {
|
|
|
|
return this._y * this.zoom;
|
|
|
|
},
|
|
|
|
set cy(v) {
|
|
|
|
return (this._y = v / this.zoom);
|
|
|
|
},
|
|
|
|
_y: 0,
|
|
|
|
zoom: 1,
|
|
|
|
rotation: 0,
|
|
|
|
get w() {
|
|
|
|
return (window.innerWidth * 1) / this.zoom;
|
|
|
|
},
|
|
|
|
get h() {
|
|
|
|
return (window.innerHeight * 1) / this.zoom;
|
|
|
|
},
|
2022-12-04 13:22:35 -06:00
|
|
|
viewToCanvas(x, y) {
|
2022-12-14 11:02:36 -06:00
|
|
|
return {
|
|
|
|
x: this.cx + this.w * (x / window.innerWidth - 0.5),
|
|
|
|
y: this.cy + this.h * (y / window.innerHeight - 0.5),
|
|
|
|
};
|
2022-12-04 13:22:35 -06:00
|
|
|
},
|
|
|
|
canvasToView(x, y) {
|
|
|
|
return {
|
|
|
|
x: window.innerWidth * ((x - this.cx) / this.w) + window.innerWidth / 2,
|
|
|
|
y: window.innerHeight * ((y - this.cy) / this.h) + window.innerHeight / 2,
|
|
|
|
};
|
|
|
|
},
|
2022-11-29 14:55:25 -06:00
|
|
|
/**
|
|
|
|
* Apply transformation
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} el Element to apply CSS transform to
|
|
|
|
*/
|
|
|
|
transform(el) {
|
|
|
|
el.style.transformOrigin = `${this.cx}px ${this.cy}px`;
|
|
|
|
el.style.transform = `scale(${this.zoom}) translate(${-(
|
|
|
|
this._x -
|
|
|
|
this.w / 2
|
|
|
|
)}px, ${-(this._y - this.h / 2)}px)`;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-30 15:46:03 -06:00
|
|
|
viewport.cx = imageCollection.size.w / 2;
|
|
|
|
viewport.cy = imageCollection.size.h / 2;
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
let worldInit = null;
|
|
|
|
|
|
|
|
viewport.transform(imageCollection.element);
|
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
/**
|
|
|
|
* Ended up using a CSS transforms approach due to more flexibility on transformations
|
|
|
|
* and capability to automagically translate input coordinates to layer space.
|
|
|
|
*/
|
|
|
|
mouse.registerContext(
|
|
|
|
"world",
|
|
|
|
(evn, ctx) => {
|
|
|
|
// Fix because in chrome layerX and layerY simply doesnt work
|
|
|
|
ctx.coords.prev.x = ctx.coords.pos.x;
|
|
|
|
ctx.coords.prev.y = ctx.coords.pos.y;
|
|
|
|
|
|
|
|
// Get cursor position
|
|
|
|
const x = evn.clientX;
|
|
|
|
const y = evn.clientY;
|
|
|
|
|
|
|
|
// Map to layer space
|
|
|
|
const layerCoords = viewport.viewToCanvas(x, y);
|
|
|
|
|
|
|
|
// Set coords
|
|
|
|
ctx.coords.pos.x = Math.round(layerCoords.x);
|
|
|
|
ctx.coords.pos.y = Math.round(layerCoords.y);
|
|
|
|
},
|
2022-12-21 15:29:11 -06:00
|
|
|
{
|
|
|
|
target: imageCollection.inputElement,
|
|
|
|
validate: (evn) => {
|
2022-12-26 22:04:56 -06:00
|
|
|
if ((!global.hasActiveInput && !evn.ctrlKey) || evn.type === "mousemove")
|
|
|
|
return true;
|
2022-12-21 15:29:11 -06:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
}
|
2022-12-16 19:57:28 -06:00
|
|
|
);
|
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
mouse.registerContext(
|
|
|
|
"camera",
|
|
|
|
(evn, ctx) => {
|
|
|
|
ctx.coords.prev.x = ctx.coords.pos.x;
|
|
|
|
ctx.coords.prev.y = ctx.coords.pos.y;
|
|
|
|
|
|
|
|
// Set coords
|
|
|
|
ctx.coords.pos.x = evn.x;
|
|
|
|
ctx.coords.pos.y = evn.y;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
validate: (evn) => {
|
|
|
|
return !!evn.ctrlKey;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-12-21 15:29:11 -06:00
|
|
|
// Redraw on active input state change
|
|
|
|
(() => {
|
|
|
|
mouse.listen.window.onany.on((evn) => {
|
|
|
|
const activeInput = DOM.hasActiveInput();
|
|
|
|
if (global.hasActiveInput !== activeInput) {
|
|
|
|
global.hasActiveInput = activeInput;
|
|
|
|
toolbar.currentTool &&
|
|
|
|
toolbar.currentTool.state.redraw &&
|
|
|
|
toolbar.currentTool.state.redraw();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
mouse.listen.camera.onwheel.on((evn) => {
|
|
|
|
evn.evn.preventDefault();
|
2022-12-06 09:25:06 -06:00
|
|
|
|
2022-12-27 15:38:36 -06:00
|
|
|
// Get cursor world position
|
|
|
|
const cursorPosition = viewport.viewToCanvas(evn.x, evn.y);
|
|
|
|
|
|
|
|
// Get viewport center
|
2022-12-26 22:04:56 -06:00
|
|
|
const pcx = viewport.cx;
|
|
|
|
const pcy = viewport.cy;
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-27 15:38:36 -06:00
|
|
|
// Apply zoom
|
|
|
|
viewport.zoom *= 1 - evn.delta * 0.0002;
|
|
|
|
|
|
|
|
// Apply normal zoom (center of viewport)
|
2022-12-26 22:04:56 -06:00
|
|
|
viewport.cx = pcx;
|
|
|
|
viewport.cy = pcy;
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
viewport.transform(imageCollection.element);
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-27 15:38:36 -06:00
|
|
|
// Calculate new viewport center and move
|
|
|
|
const newCursorPosition = viewport.viewToCanvas(evn.x, evn.y);
|
|
|
|
viewport.cx = pcx - (newCursorPosition.x - cursorPosition.x);
|
|
|
|
viewport.cy = pcy - (newCursorPosition.y - cursorPosition.y);
|
|
|
|
|
|
|
|
viewport.transform(imageCollection.element);
|
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
toolbar.currentTool.redraw();
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
const cameraPaintStart = (evn) => {
|
|
|
|
worldInit = {x: viewport.cx, y: viewport.cy};
|
|
|
|
};
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
const cameraPaint = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
if (worldInit) {
|
|
|
|
viewport.cx = worldInit.x + (evn.ix - evn.x) / viewport.zoom;
|
|
|
|
viewport.cy = worldInit.y + (evn.iy - evn.y) / viewport.zoom;
|
|
|
|
|
|
|
|
// Limits
|
2022-12-16 22:07:02 -06:00
|
|
|
viewport.cx = Math.max(
|
|
|
|
Math.min(viewport.cx, imageCollection.size.w - imageCollection.origin.x),
|
|
|
|
-imageCollection.origin.x
|
|
|
|
);
|
|
|
|
viewport.cy = Math.max(
|
|
|
|
Math.min(viewport.cy, imageCollection.size.h - imageCollection.origin.y),
|
|
|
|
-imageCollection.origin.y
|
|
|
|
);
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
// Draw Viewport location
|
|
|
|
}
|
|
|
|
|
|
|
|
viewport.transform(imageCollection.element);
|
2022-12-24 08:56:30 -06:00
|
|
|
if (global.debug) {
|
2022-12-01 15:10:30 -06:00
|
|
|
debugCtx.clearRect(0, 0, debugCanvas.width, debugCanvas.height);
|
|
|
|
debugCtx.fillStyle = "#F0F";
|
|
|
|
debugCtx.beginPath();
|
|
|
|
debugCtx.arc(viewport.cx, viewport.cy, 5, 0, Math.PI * 2);
|
|
|
|
debugCtx.fill();
|
|
|
|
}
|
2022-12-26 22:04:56 -06:00
|
|
|
};
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-26 22:04:56 -06:00
|
|
|
const cameraPaintEnd = (evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
worldInit = null;
|
2022-12-26 22:04:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
mouse.listen.camera.btn.middle.onpaintstart.on(cameraPaintStart);
|
|
|
|
mouse.listen.camera.btn.left.onpaintstart.on(cameraPaintStart);
|
|
|
|
|
|
|
|
mouse.listen.camera.btn.middle.onpaint.on(cameraPaint);
|
|
|
|
mouse.listen.camera.btn.left.onpaint.on(cameraPaint);
|
|
|
|
|
|
|
|
mouse.listen.window.btn.middle.onpaintend.on(cameraPaintEnd);
|
|
|
|
mouse.listen.window.btn.left.onpaintend.on(cameraPaintEnd);
|
2022-11-30 20:50:00 -06:00
|
|
|
|
|
|
|
window.addEventListener("resize", () => {
|
|
|
|
viewport.transform(imageCollection.element);
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCanvas.width = uiCanvas.clientWidth;
|
|
|
|
uiCanvas.height = uiCanvas.clientHeight;
|
2022-11-30 20:50:00 -06:00
|
|
|
});
|