@carterduong/p-img 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 carterduong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # p-img
2
+
3
+ A lightweight web component that adds pinch-to-zoom to images on mobile.
4
+
5
+ [Demo](https://carterduong.github.io/pimg/)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install p-img
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```html
16
+ <script type="module">
17
+ import "p-img";
18
+ </script>
19
+
20
+ <p-img src="photo.jpg" alt="A zoomable photo"></p-img>
21
+ ```
22
+
23
+ The `<p-img>` element accepts the same attributes as a standard `<img>`: `src`, `alt`, `srcset`, `sizes`, `loading`, and `crossorigin`.
24
+
25
+ ## Behavior
26
+
27
+ - Two-finger pinch scales the image up to 5x
28
+ - The zoomed image overflows its container to the full viewport and renders above all other content
29
+ - Releasing the pinch animates the image back to its original size and position
30
+ - Re-pinching mid-animation picks up from the current state
31
+
32
+ ## Styling
33
+
34
+ The internal `<img>` is exposed via a CSS `::part`:
35
+
36
+ ```css
37
+ p-img::part(img) {
38
+ border-radius: 8px;
39
+ }
40
+ ```
41
+
42
+ The host element has `overflow: hidden` by default and gains a `[zooming]` attribute during a pinch gesture, which you can target:
43
+
44
+ ```css
45
+ p-img[zooming] {
46
+ /* styles applied while zooming */
47
+ }
48
+ ```
49
+
50
+ ## Development
51
+
52
+ ```bash
53
+ npm run dev # start dev server
54
+ npm run build # build the library
55
+ npm run build:pages # build and update GitHub Pages demo
56
+ ```
57
+
58
+ ## License
59
+
60
+ MIT
@@ -0,0 +1,29 @@
1
+ export declare class PImg extends HTMLElement {
2
+ private img;
3
+ private attrObserver;
4
+ private scale;
5
+ private translateX;
6
+ private translateY;
7
+ private initialDistance;
8
+ private initialScale;
9
+ private initialMidpoint;
10
+ private initialTranslate;
11
+ constructor();
12
+ connectedCallback(): void;
13
+ disconnectedCallback(): void;
14
+ /** Forward a single attribute from the host to the inner <img>, unless it's host-only. */
15
+ private forwardAttribute;
16
+ private onImgLoad;
17
+ private onImgError;
18
+ private onTouchStart;
19
+ private onTouchMove;
20
+ private onTouchEnd;
21
+ private getTouchDistance;
22
+ private getTouchMidpoint;
23
+ private applyTransform;
24
+ private animateReset;
25
+ /** Read the current mid-animation transform back into our state. */
26
+ private readTransform;
27
+ }
28
+
29
+ export { }
package/dist/p-img.js ADDED
@@ -0,0 +1,117 @@
1
+ const o = document.createElement("template");
2
+ o.innerHTML = `
3
+ <style>
4
+ :host {
5
+ display: inline-block;
6
+ overflow: hidden;
7
+ touch-action: none;
8
+ }
9
+ :host([zooming]) {
10
+ overflow: visible;
11
+ z-index: 2147483647;
12
+ position: relative;
13
+ }
14
+ img {
15
+ display: block;
16
+ width: 100%;
17
+ height: 100%;
18
+ object-fit: inherit;
19
+ object-position: inherit;
20
+ border-radius: inherit;
21
+ filter: inherit;
22
+ opacity: inherit;
23
+ transform-origin: 0 0;
24
+ }
25
+ img.returning {
26
+ transition: transform 0.3s ease-out;
27
+ }
28
+ </style>
29
+ <img part="img" />
30
+ `;
31
+ const m = /* @__PURE__ */ new Set([
32
+ "zooming",
33
+ "style",
34
+ "class",
35
+ "id",
36
+ "slot",
37
+ "part",
38
+ "is",
39
+ "tabindex"
40
+ ]);
41
+ class d extends HTMLElement {
42
+ constructor() {
43
+ super(), this.scale = 1, this.translateX = 0, this.translateY = 0, this.initialDistance = 0, this.initialScale = 1, this.initialMidpoint = { x: 0, y: 0 }, this.initialTranslate = { x: 0, y: 0 }, this.onImgLoad = () => {
44
+ this.dispatchEvent(new Event("load"));
45
+ }, this.onImgError = () => {
46
+ this.dispatchEvent(new Event("error"));
47
+ }, this.onTouchStart = (t) => {
48
+ if (t.touches.length === 2) {
49
+ t.preventDefault(), this.img.classList.remove("returning");
50
+ const i = getComputedStyle(this.img).transform;
51
+ i && i !== "none" && (this.img.style.transform = i), this.readTransform(), this.setAttribute("zooming", ""), this.initialDistance = this.getTouchDistance(t.touches), this.initialScale = this.scale, this.initialMidpoint = this.getTouchMidpoint(t.touches), this.initialTranslate = { x: this.translateX, y: this.translateY };
52
+ }
53
+ }, this.onTouchMove = (t) => {
54
+ if (t.touches.length === 2) {
55
+ t.preventDefault();
56
+ const i = this.getTouchDistance(t.touches), e = Math.min(Math.max(this.initialScale * (i / this.initialDistance), 1), 5), s = this.getTouchMidpoint(t.touches), n = this.getBoundingClientRect(), r = this.initialMidpoint.x - n.left, h = this.initialMidpoint.y - n.top, a = e / this.initialScale, l = this.initialTranslate.x - r * (a - 1) + (s.x - this.initialMidpoint.x), c = this.initialTranslate.y - h * (a - 1) + (s.y - this.initialMidpoint.y);
57
+ this.scale = e, this.translateX = l, this.translateY = c, this.applyTransform();
58
+ }
59
+ }, this.onTouchEnd = (t) => {
60
+ t.touches.length < 2 && this.animateReset();
61
+ }, this.attachShadow({ mode: "open" }), this.shadowRoot.appendChild(o.content.cloneNode(!0)), this.img = this.shadowRoot.querySelector("img"), this.attrObserver = new MutationObserver((t) => {
62
+ for (const i of t)
63
+ i.type === "attributes" && i.attributeName && this.forwardAttribute(i.attributeName);
64
+ });
65
+ }
66
+ connectedCallback() {
67
+ for (const t of this.attributes)
68
+ this.forwardAttribute(t.name);
69
+ this.attrObserver.observe(this, { attributes: !0 }), this.img.addEventListener("load", this.onImgLoad), this.img.addEventListener("error", this.onImgError), this.addEventListener("touchstart", this.onTouchStart, { passive: !1 }), this.addEventListener("touchmove", this.onTouchMove, { passive: !1 }), this.addEventListener("touchend", this.onTouchEnd);
70
+ }
71
+ disconnectedCallback() {
72
+ this.attrObserver.disconnect(), this.img.removeEventListener("load", this.onImgLoad), this.img.removeEventListener("error", this.onImgError), this.removeEventListener("touchstart", this.onTouchStart), this.removeEventListener("touchmove", this.onTouchMove), this.removeEventListener("touchend", this.onTouchEnd);
73
+ }
74
+ /** Forward a single attribute from the host to the inner <img>, unless it's host-only. */
75
+ forwardAttribute(t) {
76
+ if (m.has(t)) return;
77
+ const i = this.getAttribute(t);
78
+ i === null ? this.img.removeAttribute(t) : this.img.setAttribute(t, i);
79
+ }
80
+ getTouchDistance(t) {
81
+ const i = t[0].clientX - t[1].clientX, e = t[0].clientY - t[1].clientY;
82
+ return Math.hypot(i, e);
83
+ }
84
+ getTouchMidpoint(t) {
85
+ return {
86
+ x: (t[0].clientX + t[1].clientX) / 2,
87
+ y: (t[0].clientY + t[1].clientY) / 2
88
+ };
89
+ }
90
+ applyTransform() {
91
+ this.img.style.transform = `translate(${this.translateX}px, ${this.translateY}px) scale(${this.scale})`;
92
+ }
93
+ animateReset() {
94
+ this.img.classList.add("returning"), this.img.style.transform = "";
95
+ const t = () => {
96
+ this.img.removeEventListener("transitionend", t), this.img.classList.remove("returning"), this.scale = 1, this.translateX = 0, this.translateY = 0, this.removeAttribute("zooming");
97
+ };
98
+ this.img.addEventListener("transitionend", t);
99
+ }
100
+ /** Read the current mid-animation transform back into our state. */
101
+ readTransform() {
102
+ const t = getComputedStyle(this.img).transform;
103
+ if (!t || t === "none") {
104
+ this.scale = 1, this.translateX = 0, this.translateY = 0;
105
+ return;
106
+ }
107
+ const i = t.match(/matrix\((.+)\)/);
108
+ if (i) {
109
+ const e = i[1].split(",").map(Number);
110
+ this.scale = e[0], this.translateX = e[4], this.translateY = e[5];
111
+ }
112
+ }
113
+ }
114
+ customElements.define("p-img", d);
115
+ export {
116
+ d as PImg
117
+ };
@@ -0,0 +1,29 @@
1
+ (function(s,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(s=typeof globalThis<"u"?globalThis:s||self,n(s["p-img"]={}))})(this,(function(s){"use strict";const n=document.createElement("template");n.innerHTML=`
2
+ <style>
3
+ :host {
4
+ display: inline-block;
5
+ overflow: hidden;
6
+ touch-action: none;
7
+ }
8
+ :host([zooming]) {
9
+ overflow: visible;
10
+ z-index: 2147483647;
11
+ position: relative;
12
+ }
13
+ img {
14
+ display: block;
15
+ width: 100%;
16
+ height: 100%;
17
+ object-fit: inherit;
18
+ object-position: inherit;
19
+ border-radius: inherit;
20
+ filter: inherit;
21
+ opacity: inherit;
22
+ transform-origin: 0 0;
23
+ }
24
+ img.returning {
25
+ transition: transform 0.3s ease-out;
26
+ }
27
+ </style>
28
+ <img part="img" />
29
+ `;const l=new Set(["zooming","style","class","id","slot","part","is","tabindex"]);class o extends HTMLElement{constructor(){super(),this.scale=1,this.translateX=0,this.translateY=0,this.initialDistance=0,this.initialScale=1,this.initialMidpoint={x:0,y:0},this.initialTranslate={x:0,y:0},this.onImgLoad=()=>{this.dispatchEvent(new Event("load"))},this.onImgError=()=>{this.dispatchEvent(new Event("error"))},this.onTouchStart=t=>{if(t.touches.length===2){t.preventDefault(),this.img.classList.remove("returning");const i=getComputedStyle(this.img).transform;i&&i!=="none"&&(this.img.style.transform=i),this.readTransform(),this.setAttribute("zooming",""),this.initialDistance=this.getTouchDistance(t.touches),this.initialScale=this.scale,this.initialMidpoint=this.getTouchMidpoint(t.touches),this.initialTranslate={x:this.translateX,y:this.translateY}}},this.onTouchMove=t=>{if(t.touches.length===2){t.preventDefault();const i=this.getTouchDistance(t.touches),e=Math.min(Math.max(this.initialScale*(i/this.initialDistance),1),5),a=this.getTouchMidpoint(t.touches),r=this.getBoundingClientRect(),c=this.initialMidpoint.x-r.left,d=this.initialMidpoint.y-r.top,h=e/this.initialScale,m=this.initialTranslate.x-c*(h-1)+(a.x-this.initialMidpoint.x),u=this.initialTranslate.y-d*(h-1)+(a.y-this.initialMidpoint.y);this.scale=e,this.translateX=m,this.translateY=u,this.applyTransform()}},this.onTouchEnd=t=>{t.touches.length<2&&this.animateReset()},this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(n.content.cloneNode(!0)),this.img=this.shadowRoot.querySelector("img"),this.attrObserver=new MutationObserver(t=>{for(const i of t)i.type==="attributes"&&i.attributeName&&this.forwardAttribute(i.attributeName)})}connectedCallback(){for(const t of this.attributes)this.forwardAttribute(t.name);this.attrObserver.observe(this,{attributes:!0}),this.img.addEventListener("load",this.onImgLoad),this.img.addEventListener("error",this.onImgError),this.addEventListener("touchstart",this.onTouchStart,{passive:!1}),this.addEventListener("touchmove",this.onTouchMove,{passive:!1}),this.addEventListener("touchend",this.onTouchEnd)}disconnectedCallback(){this.attrObserver.disconnect(),this.img.removeEventListener("load",this.onImgLoad),this.img.removeEventListener("error",this.onImgError),this.removeEventListener("touchstart",this.onTouchStart),this.removeEventListener("touchmove",this.onTouchMove),this.removeEventListener("touchend",this.onTouchEnd)}forwardAttribute(t){if(l.has(t))return;const i=this.getAttribute(t);i===null?this.img.removeAttribute(t):this.img.setAttribute(t,i)}getTouchDistance(t){const i=t[0].clientX-t[1].clientX,e=t[0].clientY-t[1].clientY;return Math.hypot(i,e)}getTouchMidpoint(t){return{x:(t[0].clientX+t[1].clientX)/2,y:(t[0].clientY+t[1].clientY)/2}}applyTransform(){this.img.style.transform=`translate(${this.translateX}px, ${this.translateY}px) scale(${this.scale})`}animateReset(){this.img.classList.add("returning"),this.img.style.transform="";const t=()=>{this.img.removeEventListener("transitionend",t),this.img.classList.remove("returning"),this.scale=1,this.translateX=0,this.translateY=0,this.removeAttribute("zooming")};this.img.addEventListener("transitionend",t)}readTransform(){const t=getComputedStyle(this.img).transform;if(!t||t==="none"){this.scale=1,this.translateX=0,this.translateY=0;return}const i=t.match(/matrix\((.+)\)/);if(i){const e=i[1].split(",").map(Number);this.scale=e[0],this.translateX=e[4],this.translateY=e[5]}}}customElements.define("p-img",o),s.PImg=o,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@carterduong/p-img",
3
+ "version": "0.1.0",
4
+ "description": "A web component that adds pinch-to-zoom to images on mobile",
5
+ "type": "module",
6
+ "main": "dist/p-img.umd.cjs",
7
+ "module": "dist/p-img.js",
8
+ "types": "dist/p-img.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/p-img.d.ts",
12
+ "import": "./dist/p-img.js",
13
+ "require": "./dist/p-img.umd.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "dev": "vite",
21
+ "build": "tsc && vite build",
22
+ "build:pages": "npm run build && cp dist/p-img.js docs/p-img.js",
23
+ "preview": "vite preview",
24
+ "test": "vitest run",
25
+ "test:watch": "vitest",
26
+ "prepublishOnly": "npm test && npm run build"
27
+ },
28
+ "keywords": [
29
+ "web-component",
30
+ "pinch-to-zoom",
31
+ "image",
32
+ "mobile",
33
+ "touch"
34
+ ],
35
+ "license": "MIT",
36
+ "author": "carterduong",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/carterduong/pimg.git"
40
+ },
41
+ "homepage": "https://github.com/carterduong/pimg",
42
+ "bugs": {
43
+ "url": "https://github.com/carterduong/pimg/issues"
44
+ },
45
+ "devDependencies": {
46
+ "jsdom": "^28.0.0",
47
+ "typescript": "^5.7.0",
48
+ "vite": "^6.1.0",
49
+ "vite-plugin-dts": "^4.5.0",
50
+ "vitest": "^4.0.18"
51
+ }
52
+ }