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
Embed into object tag #26
Comments
without this vivus won't work on an |
Yes, I launched it, and I've put two console, one in the first condition, one in the second. It always show the first, never the second. Try it var _checkElm = function(elm){
if (elm.constructor === SVGSVGElement) {
console.warn('_checkElm passed on SVGSVGElement');
return true;
}
if ( typeof elm === 'object' && typeof elm.nodeName !== 'undefined' && elm.nodeName.toLowerCase() === 'svg') {
console.warn('_checkElm passed on ObjectElement');
return true;
}
return false;
}; |
The first condition is for a |
I've made a copy of your code to show you. I'm probably wrong but I wanted to be sure because I'm missing something :-) |
Ah, now I unterstand where the problem is.... ;) Ok, the patch is for everybody who is loading vivus in the html file! To clarify: But, if you load vivus in the html and you want to show the svg within an With my patch you can do: A) Load vivus inside the html and add a graphic with C won't work without the patch. If you look at the example I wrote at http://jolic.github.io you'll see that I used example C. See the source of My intention was that I wanted vivus to work with a SVG which is loaded as external source. The JS source of the vivus example which you can see @ http://jolic.github.io is an example how you can call methods or control vivus which is loaded from the SVG inside the calling html from outside. HTH |
BTW I've added some methods inside the patch. For example
With this method you can do for exmaple
means: when the animation is finished I can change the type of the animation.
Maybe this is helpful too...
|
I'm really sorry but I can't see the difference between the example from your I set a console in http://maxwellito.github.io/drafts/vivus-embedded-2/ Are you sure to use a |
Ok. I've made a lot of changes to the vivus js class especially on the constructor that the svg is working with an The SVG https://github.com/jolic/vivus/blob/master/_PATCH/svg/hi-there.svg is the original SVG from your source as a external file. So, now it's possible to call the vivus as you want:
or
see https://github.com/jolic/vivus/blob/master/_PATCH/js/vivus-patched2.js and the example https://github.com/jolic/vivus/blob/master/_PATCH/patch-test2.html To include the vivus file inside the SVG file is working as i described in the previous example. Hope that explains it... ;) |
Oh, btw... it would be better to do:
than
|
If you use a external svg file you must set The browser throws an error if an empty attribute is omitted. See svg specs. Fyi. |
Thank you so much for this example. This is CRAZY!! A shame you didn't use Gulp to generate the patched file, but don't worry I will take care of that. I just have a simple question, how could we manage to make it working if the |
Thank you. ;) According to http://en.wikipedia.org/wiki/DOM_events the The only problem i see in the current state of the concstructor is that in case of Maybe it would be better to add a queue like mechanism in the public methods to call the methods when Vivus is ready to use them. Consider following example: <html>
<object id="mySVG" data="pathToMySVGFile">...</object>
...
a = new Vivus("mySVG",...);
a.play();
...
</html>
However, I would recommend to implement the object stuff so that everybody could use it. BTW: The patch i wrote was a fast example, here is a better one: var isReady = false;
function Vivus (element, options, callback) {
var self = this;
this.setElement(element);
var _init = function(){
// Setup
self.setOptions(options);
self.setCallback(callback);
// Set object variables
self.frameLength = 0;
self.currentFrame = 0;
self.map = [];
new Pathformer(element);
self.mapping();
self.starter();
isReady = true;
if ( typeof options.onReady !== 'undefined' && typeof options.onReady === 'function'){
options.onReady(this);
}
};
if ( this.el ) {
switch ( this.el.nodeName.toLowerCase() ){
case 'object':
this.el.addEventListener('load', function(e){
element = e.target.contentDocument;
var svgElements = element.getElementsByTagName('svg');
if ( 0 === svgElements.length ) {
throw new Error('Vivus: there is no <svg> element in this <object>.');
}
else {
element = self.el = svgElements[0];
_init();
}
});
break;
case 'svg':
_init();
break;
default: // no supported element
throw new Error('Vivus can\'t work on this type of element.');
}
}
}
Vivus.prototype.isReady = function(){
return isReady;
} So, if somebody want to use an var myCallbackOnReady = function(_vivus){
// use _vivus or a and do some stuff
};
var a = new Vivus( ...,{ onReady: myCallbackOnReady },... ); A better callback and/or queue management system inside Vivus would be great, for example:
Here is a nice article about the technique: The promise stuff is a neat thing but not necessary as this would blow up Vivus and is not in focus for the functionality. Hope this helps |
Oh...one thing: You could write ...
if ( typeof options.onReady !== 'undefined' && typeof options.onReady === 'function'){
options.onReady.call(this);
}
... in the html js code var myCallbackOnReady = function(){
// "this" is the Vivus instance!
this.play(); // for example
};
var a = new Vivus( ...,{ onReady: myCallbackOnReady },... ); Maybe this is better... |
Actually I was wondering how to access the document when the content is already loaded, simply via The loaded thing is not really a problem, we can implement a I have to admit this is quite tricky, because until here I didn't have to manage any asynchronous logic. I quite like the The only thing, we have to specify this tricks only works when the SVG is on the same domain. But that shouldn't be a problem. |
this is really nice. I've been planning to build a R |
Oh there was an error: options.onReady.call(this); have to be options.onReady.call(self); now "this" refers in the callback function to the Vivus instance. And I've added another feature. ;) Now you can refer to an element where no With this we can use Vivus as we want - we have 3 possibilities to use Vivus: A) a placed here is an example how we can call Vivus in case of C): ...
<div id="showMySVGInsideThisElement"></div>
...
var myCallbackOnReady = function(){
console.log('**** myCallbackOnReady on created <object> ****');
this.play(); // "this" is the Vivus instance
};
externSVGviaJS = new Vivus(
'showMySVGInsideThisElement'
,{
type :'delayed'
,start :'manual'
,duration: 200
,file :'svg/hi-there.svg'
,onReady: myCallbackOnReady
}
,myCallback3
); In this case Vivus checks if options.file is set and if so Vivus creates an Please have a look at the source (especially the constructor): https://github.com/jolic/vivus/blob/master/_PATCH/js/vivus-patched3.js and the example https://github.com/jolic/vivus/blob/master/_PATCH/patch-test3.html ;) @timelyportfolio |
Don't worry about the same domain policy. Everbody who wants to use this have to know about it. My intention to include the logic to support That's all. And on the other side I don't like to blow the html with graphics like Simple as that. ;) HTH |
@jolic I like all your efforts on this topic :-) Everytime you write a big response with code examples, that's appreciable. I will implement the feature on the dev branch tonight, I would be happy if you could review it. I'll mention you. @timelyportfolio Thanks :-) I was thinking to turn it into a WebComponent (or Polymer). Even if at the moment I'm more into React. I would be happy to see nice components. Now I focus on the next release which should be soon (v0.2 : animation function, npm, object tag compatible). But feel free to open a ticket and start the conversation. The most important to me is to make it accessible and interoperable, like WebComponents. |
Thank you. ;) Now here is my last patch [ for today? ;) ]: I've added a 4th possibility to use Vivus: Use Vivus on an element that is created via js directly: ...
var myElement = document.createElement('span');
var appendMyElementToThisElm = document.getElementById('showMySVGInsideThisElement2');
appendMyElementToThisElm.appendChild( myElement );
/* myElement is a created DOM element with js */
externSVGviaJS2 = new Vivus(
myElement
,{
type :'delayed'
,start :'manual'
,duration: 200
,file :'svg/hi-there.svg'
,onReady: myCallbackOnReady2
}
,myCallback4
);
... With this you could use Vivus without any DOM element inside the html. see and example D at I've just changed the _checkElm function and added. ...
if ( elm instanceof HTMLElement ) {
console.warn('_checkElm passed on HTMLElement');
return true;
}
... I've created the _checkElm function because Vivus and Pathformer are looking for a valid element. I've also added a reference to the Pathformer instance: ...
... in constructor:
self._PathformerInstance = new Pathformer(element);
...
Vivus.prototype._PathformerInstance = null;
... Please let me know when you've merged the code that i can see it. In fact you need to take my test page https://github.com/jolic/vivus/blob/master/_PATCH/patch-test4.html and import your Vivus js file. There are all current available possibilities shown to use Vivus. |
and please use https://github.com/jolic/vivus/blob/master/_PATCH/svg/hi-there.svg as I've replaced data-async with data-async="" |
here's the current code of the constructor, fyi: ...
var isReady = false;
function Vivus (element, options, callback) {
var self = this;
this.setElement(element);
var _init = function(){
// Setup
self.setOptions(options);
self.setCallback(callback);
// Set object variables
self.frameLength = 0;
self.currentFrame = 0;
self.map = [];
self._PathformerInstance = new Pathformer(element);
self.mapping();
self.starter();
isReady = true;
if ( typeof options.onReady !== 'undefined' && typeof options.onReady === 'function'){
options.onReady.call(self);
}
};
var addLoadEventOnElement = function(){
self.el.addEventListener('load', function(e){
element = e.target.contentDocument;
var svgElements = element.getElementsByTagName('svg');
if ( 0 === svgElements.length ) {
throw new Error('Vivus: there is no <svg> element in this <object>.');
}
else {
element = self.el = svgElements[0];
_init();
}
});
};
if ( this.el ) {
switch ( this.el.nodeName.toLowerCase() ){
case 'object':
addLoadEventOnElement();
break;
case 'svg':
_init();
break;
default: // no supported element or create one
if ( typeof options.file !== 'undefined' ) {
var objElm = document.createElement('object');
objElm.setAttribute('type', 'image/svg+xml');
objElm.setAttribute('data', options.file);
this.el.appendChild(objElm);
this.el = objElm;
addLoadEventOnElement();
}
else {
throw new Error('Vivus can\'t work on this type of element.');
}
}
}
};
Vivus.prototype.isReady = function(){
return isReady;
};
Vivus.prototype._PathformerInstance = null;
... and the _checkElm function: ...
var _checkElm = function(elm){
if (elm.constructor === SVGSVGElement) {
return true;
}
if ( typeof elm === 'object' && typeof elm.nodeName !== 'undefined' && elm.nodeName.toLowerCase() === 'svg') {
return true;
}
if ( elm instanceof HTMLElement ) {
return true;
}
return false;
};
... |
So, now we are able to call Vivus in the following ways: A) placed svg inside the html (with
|
I pushed my code on But for this kind of case I would love a WebComponent. At the moment I just quickly implemented the original |
Why not? ...
<span id="putSVGhere"></span>
...
new Vivus( 'putSVGhere', {...file:'svg/hi-there.svg'...});
... The code to handle this is a snap and if you need to style the object that's easy: #putSVGhere object{
background:red;
} If you patched the file with the listener for the var objElm = document.createElement('object');
objElm.setAttribute('type', 'image/svg+xml');
objElm.setAttribute('data', options.file);
this.el.appendChild(objElm);
this.el = objElm;
addLoadEventOnElement(); And you'll give the developers the freedom to decide how they want to use Vivus... My 2 cents. |
I understand your point of view, I'm just scared about how it will end. For now it looks great until people want to implement an option to add a class name, then one to set the object content (for fallback)... Let me try that tomorrow night. |
Nothing to be afraid of. ;) I wouldn't go into this so deep. if ( elm instanceof HTMLElement ) {
return true;
} Thats all. With this we could do var myElement = document.createElement('div');
// ...code to append myElement anywhere you like
var v = new Vivus( myElement ,{ file:'svg/hi-there.svg' } );
// "<object>" is appended to myElement which loads the svg And now every developer could add and do whatever he likes. |
All I'm writing about the code is for the view to be as flexible as we can without to blow up the code. The adiitions I wrote gives all of us the flexibility to use Vivus in any circumstances. A Webdesigner would prefer to add the graphic as a These examples are only a couple of possible usage scenarios. But all this is possible without to write spaghetti code or a workaround to use Vivus in their environment. |
And for the fallback thing, there are simple solutions for this: A) add a or B) don't add A) and do in css div.insideThisVivusAddsAnObject {
background: url( path/to/imageToShowIfNotSupported.jpg );
}
... that's all |
I've made a patch on 0.2.0 that everything is working with the features of above: Example: js files: patched version 0.2.0: diff: Hope, that's ok... please have a look... |
Better view of the diff: |
Hey @jolic Sorry I never closed this issue even if now Vivus accepts dynamically loaded SVGs. I wanted to give you a big and massive thanks because without you this would have never happen (I didn't know that was possible). However, there's two cases I haven't mentioned in the documentation which are the point E and F of your previous comment. Because these use-cases are rares and are more a 'hacky' way of doing things. But it remain very interesting. This is why I created the Anyway, thanks again for your contribution, you're star! |
Thanks! ;) Will look at the hacks to show a way where we could use a js created DOM elm to append a SVG when I have some time. ...would be nice to mention all the guys who helped to get Vivus better on https://github.com/maxwellito/vivus under a section "Thanks" at the bottom. ;) |
Yes definitely! |
;) I would like to add a patch where we can use Vivus also without getElementById(). Will make it when I have some time... cu |
No problem :) For now https://github.com/maxwellito/vivus/tree/dev#special-thanks |
Very nice. Thanks! |
On the main page @ section "Debug" you write "...If you're using Google Chrome, I recommend you use console.table...". I would write "...If you're using a modern browser, I..." as on Safari and Firefox console.table is available too. |
Oh finally!!! Hooray! |
Hey @jolic
I continue the topic here.
I wanted to implement your idea but after looking at your code, can you tell me why you need this:
Because when I look at the way you implement it, you still provide an
SVG
object to Vivus. I tried your fork with the unpatched version and it works fine.Can you give me more details about it please?
Thanks :)
The text was updated successfully, but these errors were encountered: