Spaces:
Runtime error
Runtime error
File size: 5,048 Bytes
70b8e47 2fe0733 66ed450 03bb246 d6e3c15 423b87b 8c259b0 423b87b 32561d8 c687c71 560b99e 66ed450 560b99e 423b87b 8c259b0 423b87b aa3e783 560b99e 2fe0733 93b70aa 66ed450 2fe0733 e5b9b7e 2fe0733 e5b9b7e a392773 66ed450 d6e3c15 560b99e 2fe0733 a392773 66ed450 d6e3c15 c2163fa 93b70aa 2fe0733 c2163fa 70b8e47 6a839c1 423b87b 5fc8a26 423b87b 6a839c1 5fc8a26 6a839c1 be26971 423b87b 5fc8a26 40e1c88 5fc8a26 423b87b be26971 423b87b 03bb246 5fc8a26 03bb246 aa3e783 560b99e 66ed450 d6e3c15 66ed450 32561d8 4df0c49 be26971 423b87b 32561d8 423b87b 66ed450 32561d8 7d4e291 70b8e47 6a839c1 27cf1bb 6a839c1 27cf1bb be26971 560b99e 70b8e47 560b99e 70b8e47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
<script lang="ts">
import { zoom, zoomIdentity } from 'd3-zoom';
import { select } from 'd3-selection';
import { onMount } from 'svelte';
import { PUBLIC_UPLOADS } from '$env/static/public';
import { currZoomTransform, canvasEl } from '$lib/store';
import { useMyPresence, useObject } from '$lib/liveblocks';
import type { PromptImgObject } from '$lib/types';
import { CANVAS_SIZE, FRAME_SIZE, GRID_SIZE } from '$lib/constants';
const myPresence = useMyPresence();
const promptImgStorage = useObject('promptImgStorage');
const height = CANVAS_SIZE.height;
const width = CANVAS_SIZE.width;
let containerEl: HTMLDivElement;
let canvasCtx: CanvasRenderingContext2D;
const imagesOnCanvas = new Set();
function getpromptImgList(promptImgList: PromptImgObject[]): PromptImgObject[] {
if (promptImgList) {
//sorted by last updated
const canvasPixels = new Map();
for (const x of Array.from(Array(width / GRID_SIZE).keys())) {
for (const y of Array.from(Array(height / GRID_SIZE).keys())) {
canvasPixels.set(`${x * GRID_SIZE}_${y * GRID_SIZE}`, null);
}
}
const list: PromptImgObject[] = Object.values(promptImgList).sort((a, b) => b.date - a.date);
// init
for (const promptImg of list) {
const x = promptImg.position.x;
const y = promptImg.position.y;
for (const i of [...Array(FRAME_SIZE / GRID_SIZE).keys()]) {
for (const j of [...Array(FRAME_SIZE / GRID_SIZE).keys()]) {
const key = `${x + i * GRID_SIZE}_${y + j * GRID_SIZE}`;
if (!canvasPixels.get(key)) {
canvasPixels.set(key, promptImg.id);
}
}
}
}
const ids = new Set([...canvasPixels.values()]);
const filteredImages = list.filter((promptImg) => ids.has(promptImg.id));
return filteredImages.reverse().filter((promptImg) => !imagesOnCanvas.has(promptImg.id));
}
return [];
}
let promptImgList: PromptImgObject[] = [];
$: promptImgList = getpromptImgList($promptImgStorage?.toObject());
$: if (promptImgList) {
renderImages(promptImgList);
}
onMount(() => {
const padding = 200;
const scale =
(width + padding * 2) /
(containerEl.clientHeight > containerEl.clientWidth
? containerEl.clientWidth
: containerEl.clientHeight);
const zoomHandler = zoom()
.scaleExtent([1 / scale / 2, 3])
.translateExtent([
[-padding, -padding],
[width + padding, height + padding]
])
.tapDistance(10)
.on('zoom', zoomed);
const selection = select($canvasEl.parentElement)
.call(zoomHandler as any)
.call(zoomHandler.transform as any, zoomIdentity)
.call(zoomHandler.scaleTo as any, 1 / scale)
.on('pointermove', handlePointerMove)
.on('pointerleave', handlePointerLeave);
canvasCtx = $canvasEl.getContext('2d') as CanvasRenderingContext2D;
function zoomReset() {
const scale =
(width + padding * 2) /
(containerEl.clientHeight > containerEl.clientWidth
? containerEl.clientWidth
: containerEl.clientHeight);
selection.call(zoomHandler.transform as any, zoomIdentity);
selection.call(zoomHandler.scaleTo as any, 1 / scale);
}
window.addEventListener('resize', zoomReset);
return () => {
window.removeEventListener('resize', zoomReset);
};
});
type ImageRendered = {
img: HTMLImageElement;
position: { x: number; y: number };
id: string;
};
function renderImages(promptImgList: PromptImgObject[]) {
Promise.all(
promptImgList.map(
({ imgURL, position, id }) =>
new Promise<ImageRendered>((resolve) => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
const res: ImageRendered = { img, position, id };
canvasCtx.drawImage(img, position.x, position.y, img.width, img.height);
resolve(res);
};
img.src = `${PUBLIC_UPLOADS}/${imgURL}`;
})
)
).then((images) => {
images.forEach(({ img, position, id }) => {
// keep track of images already rendered
//re draw in order
imagesOnCanvas.add(id);
canvasCtx.drawImage(img, position.x, position.y, img.width, img.height);
});
});
}
function zoomed(e: Event) {
const transform = ($currZoomTransform = e.transform);
$canvasEl.style.transform = `translate(${transform.x}px, ${transform.y}px) scale(${transform.k})`;
}
// Update cursor presence to current pointer location
function handlePointerMove(event: PointerEvent) {
event.preventDefault();
const x = $currZoomTransform.invertX(event.clientX);
const y = $currZoomTransform.invertY(event.clientY);
myPresence.update({
cursor: {
x,
y
}
});
}
// When the pointer leaves the page, set cursor presence to null
function handlePointerLeave() {
myPresence.update({
cursor: null
});
}
</script>
<div
bind:this={containerEl}
class="absolute top-0 left-0 right-0 bottom-0 overflow-hidden z-0 bg-gray-200"
>
<canvas
bind:this={$canvasEl}
{width}
{height}
class="absolute top-0 left-0 bg-white shadow-2xl"
/>
<slot />
</div>
<style lang="postcss" scoped>
canvas {
transform-origin: 0 0;
}
</style>
|