-
Notifications
You must be signed in to change notification settings - Fork 1
added events and images back in for complete example #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| **/node_modules | ||
| **/dist | ||
| **/img | ||
| **/vite.config.ts.*.mjs | ||
| *.tgz | ||
| *.log | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,7 @@ <h1>🥑</h1> | |
| <div class="dot yellow"></div> | ||
| <div class="dot black"></div> | ||
| </div> | ||
| <img-halftone src="./img/friut.jpeg" shape="circle"></img-halftone> | ||
| <img-halftone src="./img/fruit.jpg" shape="circle"></img-halftone> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this... 🤣 |
||
|
|
||
| <!-- | ||
| <script type="module" src="https://cdn.skypack.dev/@9am/img-halftone"></script> | ||
|
|
@@ -68,8 +68,28 @@ <h1>🥑</h1> | |
| <script type="module"> | ||
| import './src/index.ts'; | ||
|
|
||
| document.querySelector('button').addEventListener('click', () => { | ||
| document.querySelector('img-halftone').src = "/img/lamp2.webp"; | ||
| const halftone = document.querySelector('img-halftone'); | ||
| const button = document.querySelector('button'); | ||
|
|
||
| // Listen for all events | ||
| halftone.addEventListener('loading', (e) => { | ||
| console.log('🎨 Image loading started'); | ||
| }); | ||
|
|
||
| halftone.addEventListener('loaded', (e) => { | ||
| console.log('✅ Image loaded', e.detail); | ||
| }); | ||
|
|
||
| halftone.addEventListener('canvasready', (e) => { | ||
| console.log('🎯 Canvas ready', e.detail); | ||
| }); | ||
|
|
||
| halftone.addEventListener('paintcomplete', (e) => { | ||
| console.log('🎉 Painting complete'); | ||
| }); | ||
|
|
||
| button.addEventListener('click', () => { | ||
| halftone.src = "/img/lamp.jpg"; | ||
| }); | ||
| </script> | ||
| </body> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,18 +58,37 @@ class ImgHalftone extends HTMLElement { | |
| } | ||
| try { | ||
| this.shadowRoot!.host.classList.add('loading'); | ||
| this.dispatchEvent(new CustomEvent('loading', { | ||
| bubbles: true, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm all good with events, but it's better to declare them in |
||
| composed: true | ||
| })); | ||
|
|
||
| const img = await ImgHalftone.loadImage(this.src); | ||
| img.setAttribute('alt', this.alt); | ||
| // replace bg | ||
| this.img!.parentNode!.replaceChild(img, this.img!); | ||
| this.img = img; | ||
|
|
||
| // Emit loaded event | ||
| this.dispatchEvent(new CustomEvent('loaded', { | ||
| bubbles: true, | ||
| composed: true, | ||
| detail: { width: img.width, height: img.height } | ||
| })); | ||
|
|
||
| // limit max pixel | ||
| const source = <HTMLImageElement>this.img.cloneNode(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the root cause of the 'img not ready' problem? Not sure how will it act, maybe |
||
| const pixels = source.width * source.height; | ||
| source.crossOrigin = 'anonymous'; // Ensure we can read the pixel data | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. source is cloned from img, i guess we already have the |
||
| const pixels = this.img.width * this.img.height; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason to use |
||
| const scale = Math.sqrt(max / pixels); | ||
| source.width = Math.ceil(source.width * scale); | ||
| source.height = Math.ceil(source.height * scale); | ||
| source.width = Math.ceil(this.img.width * scale); | ||
| source.height = Math.ceil(this.img.height * scale); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above ☝️ |
||
|
|
||
| // Wait for the cloned image to load | ||
| await new Promise((resolve) => { | ||
| source.onload = resolve; | ||
| source.src = this.img.src; | ||
| }); | ||
|
|
||
| // update | ||
| await this.update({ source }); | ||
|
|
@@ -90,10 +109,34 @@ class ImgHalftone extends HTMLElement { | |
| private async update({ source }: { source: HTMLImageElement }) { | ||
| const size = this.cellsize; | ||
| const cellSize: Pair = [size, size]; | ||
|
|
||
| // Ensure image is loaded before emitting canvas ready event | ||
| await new Promise((resolve) => { | ||
| if (source.complete) { | ||
| resolve(null); | ||
| } else { | ||
| source.onload = () => resolve(null); | ||
| } | ||
| }); | ||
|
|
||
| // Now we can safely emit canvas ready event with correct dimensions | ||
| this.dispatchEvent(new CustomEvent('canvasready', { | ||
| bubbles: true, | ||
| composed: true, | ||
| detail: { width: source.width, height: source.height } | ||
| })); | ||
|
|
||
| await Promise.all( | ||
| this.channels.map((channel) => channel.update({ source, cellSize })) | ||
| ); | ||
| this.painter.draw(this.channels, [source!.width, source!.height]); | ||
|
|
||
| await this.painter.draw(this.channels, [source.width, source.height]); | ||
|
|
||
| // Emit paint complete event | ||
| this.dispatchEvent(new CustomEvent('paintcomplete', { | ||
| bubbles: true, | ||
| composed: true | ||
| })); | ||
| } | ||
|
|
||
| connectedCallback() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you bring back the original avocado picture I removed?
That one helped me a lot during creating the component due to its simplicity, and..... it's kind of where the logo comes from : )