Skip to content
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

SVG object loading #14

Closed
thipages opened this issue Jun 13, 2020 · 5 comments
Closed

SVG object loading #14

thipages opened this issue Jun 13, 2020 · 5 comments

Comments

@thipages
Copy link

Hi,
I tried to render an interactive svg with the following code
the issue : it loops (n loaded on console, blinking on screen).
Thank you.
I can write a code pen if needed.

const dispatch=(id, detail)=>()=>document.dispatchEvent(new CustomEvent(id, {detail:detail}))
const svg_test=(id,path)=> html`
        <object
            type="image/svg+xml"
            .data=${path}
            onload=${dispatch(id)}
        </object>
`;
const m= {
    id:'_svg_',
    path:'a valid path'
}
const update=()=> {
    render(document.body, svg_test(m.id, m.path));
}
document.addEventListener(m.id, ()=> {
    console.log("loaded");
    // do some work here like changing the svgs height/scale
    update();
});
update();
@WebReflection
Copy link
Owner

your svg_test is broken ... it doesn't close the initial object.

would this work instead?

const svg_test=(id,path)=> html`
        <object
            type="image/svg+xml"
            .data=${path}
            onload=${dispatch(id)}
        />

@thipages
Copy link
Author

Sorry, bad copy/paste.
same behaviour with the code you propose.
I write a code pen

@thipages
Copy link
Author

here it is : https://codepen.io/thipages/pen/XWXKgBw

@WebReflection
Copy link
Owner

OK, this is an expected behavior, see the following code that uses zero libraries:

document.body.innerHTML = `<object height="100px" type="image/svg+xml"></object>`;
const object = document.body.firstElementChild;
const m= {
  id: '_svg_',
  path: 'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/ruby.svg'
};
const update = () => {
  object.onload = () => {
    document.dispatchEvent(new CustomEvent(m.id, {detail: null}));
  };
  object.data = m.path;
};
document.addEventListener(m.id, () => {
  setTimeout(()=> {
    console.log("loaded");
    // do some work here like changing the svgs height/scale
    update();
  },1000);
});
update();

setting the .data over and over makes it flick, but admittedly there was no way to pass data as attribute, but nodes that have a special data meaning should have the ability to use that indeed.

So, latest version of uhtml allows you to use data=${...} instead of .data=${...} for nodes where data has a special meaning, so that the dataset helper is not triggered.

TL;DR this now works:

const svg_test=(id,path)=> html`
  <object
    type="image/svg+xml"
    data=${path}
    onload=${dispatch(id)}
  />
`;

WebReflection added a commit to WebReflection/lighterhtml that referenced this issue Jun 14, 2020
@thipages
Copy link
Author

great! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants