magic-canvas-yonava 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,35 +0,0 @@
1
- /**
2
- * a registry for all localStorage keys this application uses
3
- */
4
- export declare const localKeys: {
5
- /** camera `panX` state in magic canvas - {@link Camera.state} */
6
- readonly cameraPanX: (key: string) => `camera-pan-x-${string}`;
7
- /** camera `panY` state in magic canvas - {@link Camera.state} */
8
- readonly cameraPanY: (key: string) => `camera-pan-y-${string}`;
9
- /** camera `zoom` state in magic canvas - {@link Camera.state} */
10
- readonly cameraZoom: (key: string) => `camera-zoom-${string}`;
11
- };
12
- /**
13
- * all return values of localStorage are, by default, string.
14
- * this type allows string to be narrowed to types such as 'true' | 'false'
15
- */
16
- type TypeOverride = {};
17
- type LocalObj = typeof localKeys;
18
- /**
19
- * @example
20
- * type T = TypeOrReturnType<number> // number
21
- * type TFunc = TypeOrReturnType<() => number> // number
22
- */
23
- type TypeOrReturnType<T> = T extends (...args: any[]) => infer U ? U : T;
24
- type LocalKeys = TypeOrReturnType<LocalObj[keyof LocalObj]>;
25
- type LocalType<T extends LocalKeys> = T extends keyof TypeOverride ? TypeOverride[T] : string;
26
- /**
27
- * perform **type safe** localStorage actions
28
- */
29
- export declare const local: {
30
- get: <T extends LocalKeys>(key: T) => string | null;
31
- set: <T extends LocalKeys, K extends LocalType<T>>(key: T, value: K) => void;
32
- remove: <T extends LocalKeys>(key: T) => void;
33
- clear: () => void;
34
- };
35
- export {};
@@ -1,20 +0,0 @@
1
- /**
2
- * a registry for all localStorage keys this application uses
3
- */
4
- export const localKeys = {
5
- /** camera `panX` state in magic canvas - {@link Camera.state} */
6
- cameraPanX: (key) => `camera-pan-x-${key}`,
7
- /** camera `panY` state in magic canvas - {@link Camera.state} */
8
- cameraPanY: (key) => `camera-pan-y-${key}`,
9
- /** camera `zoom` state in magic canvas - {@link Camera.state} */
10
- cameraZoom: (key) => `camera-zoom-${key}`,
11
- };
12
- /**
13
- * perform **type safe** localStorage actions
14
- */
15
- export const local = {
16
- get: (key) => localStorage.getItem(key),
17
- set: (key, value) => localStorage.setItem(key, value),
18
- remove: (key) => localStorage.removeItem(key),
19
- clear: localStorage.clear,
20
- };
package/dist/types.d.ts DELETED
@@ -1,29 +0,0 @@
1
- import type { Ref } from 'vue';
2
- import type { DrawPattern } from './backgroundPattern';
3
- import type { Camera } from './camera';
4
- export type Coordinate = {
5
- x: number;
6
- y: number;
7
- };
8
- export type DrawContent = (ctx: CanvasRenderingContext2D) => void;
9
- export type DrawFns = {
10
- content: Ref<DrawContent>;
11
- backgroundPattern: Ref<DrawPattern>;
12
- };
13
- export type MagicCanvasProps = {
14
- canvas: Ref<HTMLCanvasElement | undefined>;
15
- camera: Omit<Camera, 'cleanup'>;
16
- cursorCoordinates: Ref<Coordinate>;
17
- ref: {
18
- canvasRef: (canvas: HTMLCanvasElement) => void;
19
- cleanup: (canvas: HTMLCanvasElement) => void;
20
- };
21
- draw: DrawFns;
22
- };
23
- export type MagicCanvasOptions = {
24
- /**
25
- * a key that is used to track the camera state in localStorage
26
- */
27
- storageKey?: string;
28
- };
29
- export type UseMagicCanvas = (options?: MagicCanvasOptions) => MagicCanvasProps;
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import type { UseMagicCanvas } from "./types";
2
- export declare const useMagicCanvas: UseMagicCanvas;
@@ -1,54 +0,0 @@
1
- import { useElementSize } from "@vueuse/core";
2
- import { onMounted, ref, watch } from "vue";
3
- import { useBackgroundPattern } from "./backgroundPattern";
4
- import { useCamera } from "./camera";
5
- import { getDevicePixelRatio } from "./camera/utils";
6
- import { useMagicCoordinates } from "./coordinates";
7
- import { getCtx } from "magic-utils-yonava";
8
- const REPAINT_FPS = 60;
9
- const initCanvasWidthHeight = (canvas) => {
10
- if (!canvas)
11
- throw new Error("Canvas not found in DOM. Check ref link.");
12
- const dpr = getDevicePixelRatio();
13
- const rect = canvas.getBoundingClientRect();
14
- canvas.width = rect.width * dpr;
15
- canvas.height = rect.height * dpr;
16
- };
17
- export const useMagicCanvas = (options = {}) => {
18
- const canvas = ref();
19
- const canvasBoxSize = useElementSize(canvas);
20
- const drawContent = ref(() => { });
21
- const drawBackgroundPattern = ref(() => { });
22
- let repaintInterval;
23
- onMounted(() => {
24
- initCanvasWidthHeight(canvas.value);
25
- repaintInterval = setInterval(repaintCanvas, 1000 / REPAINT_FPS);
26
- });
27
- watch([canvasBoxSize.width, canvasBoxSize.height], () => initCanvasWidthHeight(canvas.value));
28
- const { cleanup: cleanupCamera, ...camera } = useCamera(canvas, options?.storageKey ?? "[default-storage-key]");
29
- const { coordinates: cursorCoordinates, cleanup: cleanupCoords } = useMagicCoordinates(canvas);
30
- const pattern = useBackgroundPattern(camera.state, drawBackgroundPattern);
31
- const repaintCanvas = () => {
32
- const ctx = getCtx(canvas);
33
- camera.transformAndClear(ctx);
34
- pattern.draw(ctx);
35
- drawContent.value(ctx);
36
- };
37
- return {
38
- canvas,
39
- camera,
40
- cursorCoordinates,
41
- ref: {
42
- canvasRef: (ref) => (canvas.value = ref),
43
- cleanup: (ref) => {
44
- cleanupCoords(ref);
45
- cleanupCamera(ref);
46
- clearInterval(repaintInterval);
47
- },
48
- },
49
- draw: {
50
- content: drawContent,
51
- backgroundPattern: drawBackgroundPattern,
52
- },
53
- };
54
- };