baztianzh/ux-interactive-image
Installation:
composer require baztianzh/ux-interactive-image
yarn install --force
yarn watch
Ensure symfony/ux is installed (this package depends on it).
First Use Case:
InteractiveImageBuilderInterface dependency.$interactiveImage = $builder->createInteractiveImage('/build/images/my-image.jpg');
TextItem (or other item types) to the image:
$textItem = new TextItem(200, 20, 'title', 'description');
$interactiveImage->addItem($textItem);
{{ render_interactive_image(interactiveImage) }}
Verify:
Controller Integration:
InteractiveImageBuilderInterface to construct images dynamically (e.g., from database records).Product entity:
$image = $builder->createInteractiveImage($product->getImagePath());
foreach ($product->getAnnotations() as $annotation) {
$image->addItem(new TextItem($annotation->getX(), $annotation->getY(), $annotation->getTitle()));
}
Twig Rendering:
InteractiveImage object to Twig and use the render_interactive_image function.{{ render_interactive_image(interactiveImage, {'class': 'interactive-product-image', 'data-id': product.id}) }}
Asset Management:
/build/images/ for Webpack-encoded assets).asset() helper in Twig for dynamic paths:
{{ render_interactive_image(interactiveImage, {'imagePath': asset('images/my-image.jpg')}) }}
Event-Driven Interactions:
itemClick) via JavaScript:
document.addEventListener('interactiveImageItemClick', (e) => {
console.log('Clicked item:', e.detail.item);
});
data-* attributes or Symfony’s Stimulus bridge.Symfony UX Compatibility:
symfony/ux-turbo for real-time updates:
{% block javascripts %}
{{ parent() }}
{{ stimulate_controller('interactive-image', {
'imagePath': interactiveImage.path,
'items': interactiveImage.items|json_encode
}) }}
{% endblock %}
Dynamic Item Types:
CircleItem, PolygonItem) by implementing the ItemInterface.Localization:
$textItem = new TextItem(100, 50, 'app.interactive.title', [], 'interactive');
Performance:
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.interactiveImage('init');
observer.unobserve(entry.target);
}
});
});
jQuery Dependency:
console.log(jQuery.fn.interactiveImage); // Should return a function
Asset Paths:
/build/images/) may break in production if Webpack Encore’s public path changes.asset() in Twig or configure Encore’s publicPath dynamically.Twig Function Missing:
render_interactive_image is undefined, clear the Twig cache:
php bin/console cache:clear
Item Coordinates:
position: absolute on the container.Stimulus Conflicts:
Console Errors:
interactiveImage is not a function (missing jQuery plugin initialization).<script src="/build/interactive-image.js"></script>
Network Tab:
Twig Debug:
InteractiveImage object to verify items are added:
{{ dump(interactiveImage) }}
Custom Items:
BaztianZh\UX\InteractiveImage\Item\ItemInterface for new item types (e.g., VideoItem):
class VideoItem implements ItemInterface {
public function getHtml(): string {
return '<video src="..."></video>';
}
// ... other methods
}
Builder Configuration:
# config/services.yaml
BaztianZh\UX\InteractiveImage\Builder\InteractiveImageBuilderInterface: '@app.custom_interactive_image_builder'
Stimulus Controller:
// assets/controllers/interactive_image_controller.js
import { Controller } from '@hotwired/stimulus';
import { InteractiveImage } from 'interactive-image';
export default class extends Controller {
connect() {
new InteractiveImage(this.element, {
items: JSON.parse(this.data.get('items')),
});
}
}
Webpack Encore:
webpack.config.js:
Encore
.addEntry('interactive-image', './vendor/baztianzh/ux-interactive-image/resources/js/interactive-image.js')
.copyFiles({
from: './vendor/baztianzh/ux-interactive-image/resources/public',
to: 'build/[path][name].[ext]',
});
Default Options:
zoom: true, pan: true. Override via:
$builder->setDefaultOptions(['zoom' => false, 'pan' => false]);
Twig Global:
render_interactive_image function is registered as a Twig global. To override it, redefine it in a custom Twig extension.Yarn Watch:
yarn watch in development to auto-rebuild assets. In production, use:
yarn build
How can I help you explore Laravel packages today?