2022-11-29 14:55:25 -06:00
|
|
|
// Layering
|
|
|
|
const imageCollection = layers.registerCollection(
|
|
|
|
"image",
|
2022-11-29 17:25:44 -06:00
|
|
|
{w: 2560, h: 1536},
|
2022-11-29 14:55:25 -06:00
|
|
|
{
|
|
|
|
name: "Image Layers",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const bgLayer = imageCollection.registerLayer("bg", {
|
|
|
|
name: "Background",
|
|
|
|
});
|
|
|
|
const imgLayer = imageCollection.registerLayer("image", {
|
|
|
|
name: "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-04 14:02:46 -06:00
|
|
|
ctxOptions: {desynchronized: true},
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
const ovLayer = imageCollection.registerLayer("overlay", {
|
|
|
|
name: "Overlay",
|
|
|
|
});
|
|
|
|
const debugLayer = imageCollection.registerLayer("debug", {
|
|
|
|
name: "Debug Layer",
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
debugLayer.hide(); // Hidden by default
|
|
|
|
|
|
|
|
layers.registerCollection("mask", {name: "Mask Layers", requiresActive: true});
|
|
|
|
|
|
|
|
// Where CSS and javascript magic happens to make the canvas viewport work
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2022-11-30 15:46:03 -06:00
|
|
|
// Fix because in chrome layerX and layerY simply doesnt work
|
|
|
|
/** @type {HTMLDivElement} */
|
|
|
|
const target = evn.target;
|
|
|
|
|
|
|
|
// Get element bounding rect
|
2022-12-01 15:10:30 -06:00
|
|
|
const bb = imageCollection.element.getBoundingClientRect();
|
2022-11-30 15:46:03 -06:00
|
|
|
|
|
|
|
// Get element width/height (css, cause I don't trust client sizes in chrome anymore)
|
|
|
|
const w = imageCollection.size.w;
|
|
|
|
const h = imageCollection.size.h;
|
|
|
|
|
|
|
|
// Get cursor position
|
|
|
|
const x = evn.clientX;
|
|
|
|
const y = evn.clientY;
|
|
|
|
|
|
|
|
// Map to layer space
|
|
|
|
const layerX = ((x - bb.left) / bb.width) * w;
|
|
|
|
const layerY = ((y - bb.top) / bb.height) * h;
|
|
|
|
|
|
|
|
//
|
2022-11-29 14:55:25 -06:00
|
|
|
ctx.coords.prev.x = ctx.coords.pos.x;
|
|
|
|
ctx.coords.prev.y = ctx.coords.pos.y;
|
2022-11-30 15:46:03 -06:00
|
|
|
ctx.coords.pos.x = layerX;
|
|
|
|
ctx.coords.pos.y = layerY;
|
2022-11-29 14:55:25 -06:00
|
|
|
},
|
|
|
|
{target: imageCollection.inputElement}
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
return {x, y};
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
|
|
|
|
mouse.listen.window.onwheel.on((evn) => {
|
|
|
|
if (evn.evn.ctrlKey) {
|
2022-11-29 16:47:19 -06:00
|
|
|
evn.evn.preventDefault();
|
2022-12-06 09:25:06 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const pcx = viewport.cx;
|
|
|
|
const pcy = viewport.cy;
|
|
|
|
if (evn.delta < 0) {
|
|
|
|
viewport.zoom *= 1 + Math.abs(evn.delta * 0.0002);
|
|
|
|
} else {
|
|
|
|
viewport.zoom *= 1 - Math.abs(evn.delta * 0.0002);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewport.cx = pcx;
|
|
|
|
viewport.cy = pcy;
|
|
|
|
|
|
|
|
viewport.transform(imageCollection.element);
|
|
|
|
|
2022-12-06 09:25:06 -06:00
|
|
|
toolbar.currentTool.redraw();
|
|
|
|
|
2022-12-01 15:10:30 -06:00
|
|
|
if (debug) {
|
|
|
|
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-11-29 14:55:25 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mouse.listen.window.btn.middle.onpaintstart.on((evn) => {
|
|
|
|
worldInit = {x: viewport.cx, y: viewport.cy};
|
|
|
|
});
|
|
|
|
|
|
|
|
mouse.listen.window.btn.middle.onpaint.on((evn) => {
|
|
|
|
if (worldInit) {
|
|
|
|
viewport.cx = worldInit.x + (evn.ix - evn.x) / viewport.zoom;
|
|
|
|
viewport.cy = worldInit.y + (evn.iy - evn.y) / viewport.zoom;
|
|
|
|
|
|
|
|
// Limits
|
|
|
|
viewport.cx = Math.max(Math.min(viewport.cx, imageCollection.size.w), 0);
|
|
|
|
viewport.cy = Math.max(Math.min(viewport.cy, imageCollection.size.h), 0);
|
|
|
|
|
|
|
|
// Draw Viewport location
|
|
|
|
}
|
|
|
|
|
|
|
|
viewport.transform(imageCollection.element);
|
2022-12-01 15:10:30 -06:00
|
|
|
if (debug) {
|
|
|
|
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-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
mouse.listen.window.btn.middle.onpaintend.on((evn) => {
|
|
|
|
worldInit = null;
|
|
|
|
});
|
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
|
|
|
});
|