To achieve scaling and offsetting your Spine character based on the percentage of the div dimensions using the ts-overlay-widget
, you can use CSS to control the size and position of the widget itself. The scale
attribute in the widget scales based on the skeleton dimensions, not the div dimensions. Here's how you can achieve your desired effect:
Scaling based on div height:
Use CSS to set the height of the widget to a percentage of the div height, and then use thescale
attribute to adjust the size of the character within that height.Offsetting x and y by a percentage of div width/height:
Use CSS to position the widget within the div.
Here's an example of how you can achieve this:
<div id="spine-container" style="position: relative; width: 100%; height: 100%;">
<spine-widget
identifier={identifier}
atlas="spine-dynamic-nopremult/char.atlas"
skeleton="spine-dynamic-nopremult/char.skel"
animation={animation}
skin={"skin"}
clip="true"
mode="origin"
scale="1" <!-- Set scale to 1 and control size with CSS -->
style="position: absolute; width: 50%; height: 50%; top: 25%; left: 25%;" <!-- Adjust width, height, top, and left to control size and position -->
debug="true"
/>
</div>
In this example:
- The
spine-widget
is set to 50% of the div's width and height. - The
top
andleft
properties are set to 25% to center the widget within the div. - The
scale
attribute is set to1
to use the original skeleton dimensions, and the size is controlled by the CSS.
This approach ensures that the widget scales and positions itself based on the div dimensions.
If you need more precise control over the scaling and positioning, you might need to use JavaScript to dynamically calculate and set the scale
, top
, and left
properties based on the div's dimensions.
const container = document.getElementById('spine-container');
const widget = container.querySelector('spine-widget');
const updateWidgetSize = () => {
const containerHeight = container.clientHeight;
const containerWidth = container.clientWidth;
// Set the widget size to 50% of the container height
widget.style.height = `${containerHeight * 0.5}px`;
widget.style.width = `${containerWidth * 0.5}px`;
// Center the widget
widget.style.top = `${containerHeight * 0.25}px`;
widget.style.left = `${containerWidth * 0.25}px`;
};
window.addEventListener('resize', updateWidgetSize);
updateWidgetSize();
This script dynamically updates the size and position of the spine-widget
based on the container's dimensions whenever the window is resized.