Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: refactor layer name building and group number functions for improved performance; add media mask utilities

CristianOlivera149dff83e600a
diff --git a/lib/layers.utils.ts b/lib/layers.utils.ts
index b627678..896684d 100644
--- a/lib/layers.utils.ts
+++ b/lib/layers.utils.ts
@@ -8,10 +8,9 @@ export function buildLayerNames(elements: CanvasElement[]): Map<string, string>
typeCounts.set(key, (typeCounts.get(key) ?? 0) + 1);
}
}
- const sorted = [...elements].sort((a, b) => a.zIndex - b.zIndex);
const typeIndexes = new Map<string, number>();
const names = new Map<string, string>();
- for (const el of sorted) {
+ for (const el of elements) {
if (el.type === "text") {
names.set(el.id, (el as TextElement).content?.slice(0, 24) || "Texto");
} else if (el.type === "image") {
@@ -31,10 +30,9 @@ export function buildLayerNames(elements: CanvasElement[]): Map<string, string>
}
export function buildGroupNumbers(elements: CanvasElement[]): Map<string, number> {
- const sorted = [...elements].sort((a, b) => a.zIndex - b.zIndex);
const map = new Map<string, number>();
let n = 0;
- for (const el of sorted) {
+ for (const el of elements) {
if (el.groupId && !map.has(el.groupId)) map.set(el.groupId, ++n);
}
return map;
diff --git a/lib/media-mask.utils.ts b/lib/media-mask.utils.ts
new file mode 100644
index 0000000..136a08f
--- /dev/null
+++ b/lib/media-mask.utils.ts
@@ -0,0 +1,96 @@
+import { ImageMaskConfigLike } from "./phone3d.utils";
+
+export interface MaskOverflowBox {
+ inset?: number;
+ insetX?: number;
+ insetY?: number;
+ deviceWidth: number;
+ deviceHeight: number;
+}
+
+function remapAxis(pct: number, inset: number, deviceSize: number): number {
+ const totalSize = deviceSize + inset * 2;
+ if (totalSize <= 0) return pct;
+ return ((inset + (pct / 100) * deviceSize) / totalSize) * 100;
+}
+
+export function GetMediaMaskStyles(
+ maskConfig?: ImageMaskConfigLike | null,
+ overflowBox?: MaskOverflowBox
+): React.CSSProperties {
+ if (!maskConfig || !maskConfig.enabled) return {};
+
+ const insetX = overflowBox?.insetX ?? overflowBox?.inset ?? 0;
+ const insetY = overflowBox?.insetY ?? overflowBox?.inset ?? 0;
+
+ const remapY = (pct: number) => overflowBox ? remapAxis(pct, insetY, overflowBox.deviceHeight) : pct;
+ const remapX = (pct: number) => overflowBox ? remapAxis(pct, insetX, overflowBox.deviceWidth) : pct;
+
+ const masks: string[] = [];
+
+ if (maskConfig.top) {
+ masks.push(`linear-gradient(180deg, transparent ${remapY(maskConfig.top.from)}%, black ${remapY(maskConfig.top.to ?? 100)}%)`);
+ }
+ if (maskConfig.bottom) {
+ masks.push(`linear-gradient(0deg, transparent ${remapY(maskConfig.bottom.from)}%, black ${remapY(maskConfig.bottom.to ?? 100)}%)`);
+ }
+ if (maskConfig.left) {
+ masks.push(`linear-gradient(90deg, transparent ${remapX(maskConfig.left.from)}%, black ${remapX(maskConfig.left.to ?? 100)}%)`);
+ }
+ if (maskConfig.right) {
+ masks.push(`linear-gradient(270deg, transparent ${remapX(maskConfig.right.from)}%, black ${remapX(maskConfig.right.to ?? 100)}%)`);
+ }
+ if (maskConfig.angle !== undefined) {
+ masks.push(`linear-gradient(${maskConfig.angle}deg, transparent ${maskConfig.angleFrom ?? 0}%, black ${maskConfig.angleTo ?? 100}%)`);
+ }
+
+ if (masks.length === 0) return {};
+
+ return {
+ WebkitMaskImage: masks.join(', '),
+ WebkitMaskComposite: 'source-in',
+ maskImage: masks.join(', '),
+ maskComposite: 'intersect',
+ };
+}
+
+export function applyGradientMaskToRegion(
+ ctx: CanvasRenderingContext2D,
+ x: number,
+ y: number,
+ width: number,
+ height: number,
+ maskConfig: ImageMaskConfigLike
+) {
+ if (!maskConfig?.enabled) return;
+
+ const drawGradient = (cssAngleDeg: number, from: number, to: number) => {
+ const a = (cssAngleDeg * Math.PI) / 180;
+ const cx = x + width / 2;
+ const cy = y + height / 2;
+ const diag = Math.sqrt(width * width + height * height);
+ const dx = Math.sin(a) * diag;
+ const dy = -Math.cos(a) * diag;
+ const g = ctx.createLinearGradient(cx - dx / 2, cy - dy / 2, cx + dx / 2, cy + dy / 2);
+ g.addColorStop(0, "rgba(0,0,0,1)");
+ g.addColorStop(Math.max(0, Math.min(1, from / 100)), "rgba(0,0,0,1)");
+ g.addColorStop(Math.max(0, Math.min(1, (to ?? 100) / 100)), "rgba(0,0,0,0)");
+
+ ctx.save();
+ ctx.beginPath();
+ ctx.rect(x, y, width, height);
+ ctx.clip();
+ ctx.globalCompositeOperation = "destination-out";
+ ctx.fillStyle = g;
+ ctx.fillRect(x, y, width, height);
+ ctx.restore();
+ };
+
+ if (maskConfig.top) drawGradient(180, maskConfig.top.from, maskConfig.top.to ?? 100);
+ if (maskConfig.bottom) drawGradient(0, maskConfig.bottom.from, maskConfig.bottom.to ?? 100);
+ if (maskConfig.left) drawGradient(90, maskConfig.left.from, maskConfig.left.to ?? 100);
+ if (maskConfig.right) drawGradient(270, maskConfig.right.from, maskConfig.right.to ?? 100);
+ if (maskConfig.angle !== undefined) {
+ drawGradient(maskConfig.angle, maskConfig.angleFrom ?? 0, maskConfig.angleTo ?? 100);
+ }
+}
\ No newline at end of file
diff --git a/lib/phone3d.utils.ts b/lib/phone3d.utils.ts
index 5717559..4e13621 100644
--- a/lib/phone3d.utils.ts
+++ b/lib/phone3d.utils.ts
@@ -82,15 +82,15 @@ export function getDeviceFromModelUrl(modelUrl: string | undefined): DeviceKey {
if (modelUrl.includes("iphone")) return "iphone";
return "phone";
}
-
export function createCoverScreenCanvas(
source: HTMLImageElement | HTMLVideoElement | HTMLCanvasElement,
targetW: number,
targetH: number,
cornerRadius: number,
maskConfig?: ImageMaskConfigLike | null
-): HTMLCanvasElement {
+): HTMLCanvasElement | OffscreenCanvas {
let srcW: number, srcH: number;
+
if (source instanceof HTMLImageElement) {
srcW = source.naturalWidth || source.width || 1;
srcH = source.naturalHeight || source.height || 1;
@@ -108,14 +108,14 @@ export function createCoverScreenCanvas(
const offsetX = (targetW - drawW) / 2;
const offsetY = (targetH - drawH) / 2;
- const canvas = document.createElement("canvas");
- canvas.width = targetW;
- canvas.height = targetH;
- const ctx = canvas.getContext("2d")!;
+ const canvas = createOptimalCanvas(targetW, targetH);
+ const ctx = canvas.getContext("2d") as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
+
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
const r = Math.min(cornerRadius, Math.min(targetW, targetH) / 2);
+
ctx.beginPath();
ctx.moveTo(r, 0);
ctx.lineTo(targetW - r, 0);
@@ -128,7 +128,7 @@ export function createCoverScreenCanvas(
ctx.quadraticCurveTo(0, 0, r, 0);
ctx.closePath();
ctx.clip();
-
+
ctx.drawImage(source, offsetX, offsetY, drawW, drawH);
if (maskConfig?.enabled) {
@@ -139,23 +139,25 @@ export function createCoverScreenCanvas(
const diag = Math.sqrt(targetW * targetW + targetH * targetH);
const dx = Math.sin(a) * diag;
const dy = -Math.cos(a) * diag;
+
const g = ctx.createLinearGradient(cx - dx / 2, cy - dy / 2, cx + dx / 2, cy + dy / 2);
g.addColorStop(0, "rgba(0,0,0,1)");
g.addColorStop(Math.max(0, Math.min(1, from / 100)), "rgba(0,0,0,1)");
g.addColorStop(Math.max(0, Math.min(1, (to ?? 100) / 100)), "rgba(0,0,0,0)");
+
ctx.globalCompositeOperation = "destination-out";
ctx.fillStyle = g;
ctx.fillRect(0, 0, targetW, targetH);
ctx.globalCompositeOperation = "source-over";
};
+
if (maskConfig.top) drawGradient(180, maskConfig.top.from, maskConfig.top.to ?? 100);
if (maskConfig.bottom) drawGradient(0, maskConfig.bottom.from, maskConfig.bottom.to ?? 100);
if (maskConfig.left) drawGradient(90, maskConfig.left.from, maskConfig.left.to ?? 100);
if (maskConfig.right) drawGradient(270, maskConfig.right.from, maskConfig.right.to ?? 100);
- if (maskConfig.angle !== undefined) {
- drawGradient(maskConfig.angle, maskConfig.angleFrom ?? 0, maskConfig.angleTo ?? 100);
- }
+ if (maskConfig.angle !== undefined) drawGradient(maskConfig.angle, maskConfig.angleFrom ?? 0, maskConfig.angleTo ?? 100);
}
+
return canvas;
}
@@ -259,15 +261,48 @@ export function applyCropToImage(
return canvas;
}
-export function easeInOutCubic(t: number): number {
- return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
+export function applyTextureCover(
+ texture: THREE.Texture,
+ sourceWidth: number,
+ sourceHeight: number,
+ targetWidth: number,
+ targetHeight: number
+) {
+ if (sourceWidth === 0 || sourceHeight === 0) return;
+
+ const videoAspect = sourceWidth / sourceHeight;
+ const screenAspect = targetWidth / targetHeight;
+
+ if (videoAspect > screenAspect) {
+ texture.repeat.set(screenAspect / videoAspect, 1);
+ texture.offset.set((1 - texture.repeat.x) / 2, 0);
+ } else {
+ texture.repeat.set(1, videoAspect / screenAspect);
+ texture.offset.set(0, (1 - texture.repeat.y) / 2);
+ }
+ texture.needsUpdate = true;
+}
+
+function createOptimalCanvas(width: number, height: number) {
+ if (typeof OffscreenCanvas !== "undefined") {
+ return new OffscreenCanvas(width, height);
+ }
+ const canvas = document.createElement("canvas");
+ canvas.width = width;
+ canvas.height = height;
+ return canvas;
}
-export interface CameraFlyAnimation {
- fromPos: THREE.Vector3;
- toPos: THREE.Vector3;
- fromTarget: THREE.Vector3;
- toTarget: THREE.Vector3;
- startTime: number;
- duration: number;
+export function getOutlineFilter(isSelected: boolean = false, isHovered: boolean = false): string {
+ const showOutline = isSelected || isHovered;
+
+ if (!showOutline) return "none";
+
+ const outlineColor = isSelected ? "#3b82f6" : "#ffffff";
+
+ if (isSelected) {
+ return `drop-shadow(1.5px 0 0.1px ${outlineColor}) drop-shadow(-1.5px 0 0.1px ${outlineColor}) drop-shadow(0 1.5px 0.1px ${outlineColor}) drop-shadow(0 -1.5px 0.1px ${outlineColor})`;
+ } else {
+ return `drop-shadow(0.75px 0 0px ${outlineColor}) drop-shadow(-0.75px 0 0px ${outlineColor}) drop-shadow(0 0.75px 0px ${outlineColor}) drop-shadow(0 -0.75px 0px ${outlineColor})`;
+ }
}
\ No newline at end of file
diff --git a/types/layers.types.ts b/types/layers.types.ts
index bd59af1..b1f0d4e 100644
--- a/types/layers.types.ts
+++ b/types/layers.types.ts
@@ -14,7 +14,7 @@ export interface LayersPanelProps {
onSelect: (id: string | null) => void;
onMultiSelect?: (ids: string[]) => void;
onDelete: (id: string | string[]) => void;
- onReorder: (orderedIds: string[]) => void;
+ onReorder: (frontIds: string[], backIds: string[]) => void;
onToggleVisible: (id: string, visible: boolean) => void;
onToggleLock: (id: string, locked: boolean) => void;
onBringToFront: (id: string) => void;