usePannable, make canvas position absolute, add buttons in Touch

main
Shad Amethyst 2 years ago
parent 96a0847e2c
commit a9b894d066

4
package-lock.json generated

@ -1,12 +1,12 @@
{ {
"name": "@shadryx/pptk", "name": "@shadryx/pptk",
"version": "0.1.6", "version": "0.1.7",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@shadryx/pptk", "name": "@shadryx/pptk",
"version": "0.1.6", "version": "0.1.7",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"solid-js": "^1.6.2", "solid-js": "^1.6.2",

@ -1,6 +1,6 @@
{ {
"name": "@shadryx/pptk", "name": "@shadryx/pptk",
"version": "0.1.6", "version": "0.1.7",
"description": "Pixel-Perfect ToolKit, a library to help make pixel-perfect applications on high-DPI devices", "description": "Pixel-Perfect ToolKit, a library to help make pixel-perfect applications on high-DPI devices",
"keywords": [ "keywords": [
"pixel-perfect", "pixel-perfect",

@ -20,7 +20,7 @@ export const MAX_CANVAS_SIZE = 10000;
/** /**
* A function to call to unbind the event listeners attached to a canvas through `attachCanvas`. * A function to call to unbind the event listeners attached to a canvas through `attachCanvas`.
* @deprecated This type will be removed in the next major release * *DEPRECATION NOTICE:* This type will be removed in the next major release
* @callback AttachCanvasUnbind * @callback AttachCanvasUnbind
* @returns {void} * @returns {void}
**/ **/
@ -168,6 +168,7 @@ export function getContext2D(canvas) {
* @prop {number} dy * @prop {number} dy
* @prop {PointerEvent['pointerType']} type * @prop {PointerEvent['pointerType']} type
* @prop {number} id * @prop {number} id
* @prop {PointerEvent['buttons']} buttons
**/ **/
/** /**
@ -214,6 +215,7 @@ export function attachTouch(element, options = {}) {
dy: 0, dy: 0,
type: event.pointerType, type: event.pointerType,
id: event.pointerId, id: event.pointerId,
buttons: event.buttons,
}; };
touches.set(event.pointerId, touch); touches.set(event.pointerId, touch);
@ -329,6 +331,7 @@ export function attachTouch(element, options = {}) {
* @prop {number=} dy Defaults to 0 * @prop {number=} dy Defaults to 0
* @prop {number=} minScale * @prop {number=} minScale
* @prop {number=} maxScale * @prop {number=} maxScale
* @prop {number=} scale Defaults to 1
**/ **/
/** /**
@ -336,7 +339,7 @@ export function attachTouch(element, options = {}) {
**/ **/
export class Pannable { export class Pannable {
/** /**
* @param {PannableOptions} options * @param {PannableOptions=} options
**/ **/
constructor(options) { constructor(options) {
/** @type {number} **/ /** @type {number} **/
@ -344,7 +347,7 @@ export class Pannable {
/** @type {number} **/ /** @type {number} **/
this.dy = options?.dy || 0; this.dy = options?.dy || 0;
/** @type {number} **/ /** @type {number} **/
this.logScale = 0.0; this.logScale = options?.scale ? Math.log2(options.scale) : 0.0;
/** @type {number} **/ /** @type {number} **/
this.logMinScale = options?.minScale ? Math.log2(options.minScale) : -Infinity; this.logMinScale = options?.minScale ? Math.log2(options.minScale) : -Infinity;
/** @type {number} **/ /** @type {number} **/

@ -1,11 +1,11 @@
import { JSX, Component, createEffect, onCleanup } from "solid-js"; import { JSX, Component, createEffect, onCleanup } from "solid-js";
import { attachCanvas, AttachCanvasOptions } from '../index.js'; import { attachCanvas, AttachCanvasOptions } from "../index.js";
export type PixelPerfectCanvasProps = { export type PixelPerfectCanvasProps = {
/** /**
* Style to be applied to the container. * Style to be applied to the container.
**/ **/
style?: Omit<JSX.CSSProperties, 'overflow' | 'overflow-x' | 'overflow-y'>, style?: Omit<JSX.CSSProperties, "overflow" | "overflow-x" | "overflow-y">,
/** /**
* Class name(s) to give to the container. Make sure that they do not override the required * Class name(s) to give to the container. Make sure that they do not override the required
* properties (namely `overflow`) * properties (namely `overflow`)
@ -27,7 +27,7 @@ export type PixelPerfectCanvasProps = {
* Callback called after the event listeners for dimension and DPR changes are detached. * Callback called after the event listeners for dimension and DPR changes are detached.
**/ **/
onDetach?: (canvas: HTMLCanvasElement) => void, onDetach?: (canvas: HTMLCanvasElement) => void,
} & Omit<AttachCanvasOptions, 'onResize' | 'container'> } & Omit<AttachCanvasOptions, "onResize" | "container">
/** /**
* A canvas that is wrapped in a container (which will be styled with `style` or `class`), * A canvas that is wrapped in a container (which will be styled with `style` or `class`),
@ -60,12 +60,21 @@ export const PixelPerfectCanvas: Component<PixelPerfectCanvasProps> = (props) =>
return (<div return (<div
style={{ style={{
...props.style, ...props.style,
overflow: 'hidden' position: "relative"
}} }}
class={props.class} class={props.class}
ref={(div) => containerRef = div} ref={(div) => containerRef = div}
> >
<canvas ref={(canvas) => canvasRef = canvas}> <canvas
ref={(canvas) => canvasRef = canvas}
style={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
>
{props.children ?? "Sorry, your browser does not support canvases"} {props.children ?? "Sorry, your browser does not support canvases"}
</canvas> </canvas>
</div>); </div>);

@ -1,2 +1,3 @@
export * from "./PixelPerfectCanvas.jsx"; export * from "./PixelPerfectCanvas.jsx";
export * from "./PixelPerfectTouch.jsx"; export * from "./PixelPerfectTouch.jsx";
export * from "./usePannable.js";

@ -0,0 +1,26 @@
import { createSignal } from "solid-js";
import { Pannable, PannableOptions, PannableState, Touch } from "../index.js";
export function usePannable(config: PannableOptions) {
const pannable = new Pannable(config);
const [state, setState] = createSignal<PannableState>(pannable.getState());
return {
update(touches: Map<number, Touch>) {
pannable.update(touches);
},
move(touches: Map<number, Touch>) {
pannable.move(touches);
const newState = pannable.getState();
setState(newState);
return newState;
},
zoom(amount: number, clientX?: number, clientY?: number) {
pannable.zoom(amount, clientX, clientY);
const newState = pannable.getState();
setState(newState);
return newState;
},
getState: state,
};
}
Loading…
Cancel
Save